diff --git a/custom_components/immich/hub.py b/custom_components/immich/hub.py index 6c58360..82b3f18 100644 --- a/custom_components/immich/hub.py +++ b/custom_components/immich/hub.py @@ -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: @@ -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 diff --git a/custom_components/immich/image.py b/custom_components/immich/image.py index 2bc73f5..8d5c758 100644 --- a/custom_components/immich/image.py +++ b/custom_components/immich/image.py @@ -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, []) @@ -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."""