Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding any_image entity #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions custom_components/immich/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,34 @@ async def download_asset(self, asset_id: str) -> bytes | None:
_LOGGER.error("Error connecting to the API: %s", exception)
raise CannotConnect from exception

async def list_all_images(self) -> list[dict]:
"""List all images."""
try:
async with aiohttp.ClientSession() as session:
url = urljoin(self.host, "/api/search/metadata")
headers = {"Accept": "application/json", _HEADER_API_KEY: self.api_key}
data = {"type": "IMAGE"}

async with session.post(url=url, headers=headers, data=data) as response:
if response.status != 200:
raw_result = await response.text()
_LOGGER.error("Error from API: body=%s", raw_result)
raise ApiError()

images = await response.json()
assets: list[dict] = images["assets"]["items"]
return assets
except aiohttp.ClientError as exception:
_LOGGER.error("Error connecting to the API: %s", exception)
raise CannotConnect from exception

async def list_favorite_images(self) -> list[dict]:
"""List all favorite images."""
try:
async with aiohttp.ClientSession() as session:
url = urljoin(self.host, "/api/search/metadata")
headers = {"Accept": "application/json", _HEADER_API_KEY: self.api_key}
data = {"isFavorite": "true"}
data = {"isFavorite": "true", "type": "IMAGE"}

async with session.post(url=url, headers=headers, data=data) as response:
if response.status != 200:
Expand All @@ -126,12 +147,7 @@ async def list_favorite_images(self) -> list[dict]:

favorites = await response.json()
assets: list[dict] = favorites["assets"]["items"]

filtered_assets: list[dict] = [
asset for asset in assets if asset["type"] == "IMAGE"
]

return filtered_assets
return assets
except aiohttp.ClientError as exception:
_LOGGER.error("Error connecting to the API: %s", exception)
raise CannotConnect from exception
Expand Down
13 changes: 12 additions & 1 deletion custom_components/immich/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def async_setup_entry(
)

# Create entity for random favorite image
async_add_entities([ImmichImageFavorite(hass, hub)])
async_add_entities([ImmichImageFavorite(hass, hub), ImmichImageAll(hass, hub)])

# Create entities for random image from each watched album
watched_albums = config_entry.options.get(CONF_WATCHED_ALBUMS, [])
Expand Down Expand Up @@ -158,6 +158,17 @@ async def _refresh_available_asset_ids(self) -> list[str] | None:
return [image["id"] for image in await self.hub.list_favorite_images()]


class ImmichImageAll(BaseImmichImage):
"""Image entity for Immich that displays a random image from all available images"""

_attr_unique_id = "any_image"
_attr_name = "Immich: Random image"

async def _refresh_available_asset_ids(self) -> list[str] | None:
"""Refresh the list of available asset IDs."""
return [image["id"] for image in await self.hub.list_all_images()]


class ImmichImageAlbum(BaseImmichImage):
"""Image entity for Immich that displays a random image from a specific album."""

Expand Down