11"""Hub for Immich integration."""
22from __future__ import annotations
33
4- import aiohttp
54import logging
65from urllib .parse import urljoin
7- import random
6+
7+ import aiohttp
88
99from homeassistant .exceptions import HomeAssistantError
1010
@@ -33,9 +33,9 @@ async def authenticate(self) -> bool:
3333 _LOGGER .error ("Error from API: body=%s" , raw_result )
3434 return False
3535
36- json_result = await response .json ()
36+ auth_result = await response .json ()
3737
38- if not json_result .get ("authStatus" ):
38+ if not auth_result .get ("authStatus" ):
3939 raw_result = await response .text ()
4040 _LOGGER .error ("Error from API: body=%s" , raw_result )
4141 return False
@@ -45,21 +45,25 @@ async def authenticate(self) -> bool:
4545 _LOGGER .error ("Error connecting to the API: %s" , exception )
4646 raise CannotConnect from exception
4747
48- async def get_random_picture (self ) -> dict | None :
49- """Get a random picture from the API."""
50- assets = [
51- asset for asset in await self ._list_favorites () if asset ["type" ] == "IMAGE"
52- ]
48+ async def get_my_user_info (self ) -> dict :
49+ """Get user info."""
50+ try :
51+ async with aiohttp .ClientSession () as session :
52+ url = urljoin (self .host , "/api/user/me" )
53+ headers = {"Accept" : "application/json" , _HEADER_API_KEY : self .api_key }
5354
54- if not assets :
55- _LOGGER .error ("No assets found in favorites" )
56- return None
55+ async with session .get (url = url , headers = headers ) as response :
56+ if response .status != 200 :
57+ raw_result = await response .text ()
58+ _LOGGER .error ("Error from API: body=%s" , raw_result )
59+ raise ApiError ()
5760
58- # Select random item in list
59- random_asset = random .choice (assets )
61+ user_info : dict = await response .json ()
6062
61- _LOGGER .debug ("Random asset: %s" , random_asset )
62- return random_asset
63+ return user_info
64+ except aiohttp .ClientError as exception :
65+ _LOGGER .error ("Error connecting to the API: %s" , exception )
66+ raise CannotConnect from exception
6367
6468 async def download_asset (self , asset_id : str ) -> bytes :
6569 """Download the asset."""
@@ -78,7 +82,8 @@ async def download_asset(self, asset_id: str) -> bytes:
7882 _LOGGER .error ("Error connecting to the API: %s" , exception )
7983 raise CannotConnect from exception
8084
81- async def _list_favorites (self ) -> list [dict ]:
85+ async def list_favorite_images (self ) -> list [dict ]:
86+ """List all favorite images."""
8287 try :
8388 async with aiohttp .ClientSession () as session :
8489 url = urljoin (self .host , "/api/asset?isFavorite=true" )
@@ -90,9 +95,58 @@ async def _list_favorites(self) -> list[dict]:
9095 _LOGGER .error ("Error from API: body=%s" , raw_result )
9196 raise ApiError ()
9297
93- json_result = await response .json ()
98+ assets : list [dict ] = await response .json ()
99+
100+ filtered_assets : list [dict ] = [
101+ asset for asset in assets if asset ["type" ] == "IMAGE"
102+ ]
103+
104+ return filtered_assets
105+ except aiohttp .ClientError as exception :
106+ _LOGGER .error ("Error connecting to the API: %s" , exception )
107+ raise CannotConnect from exception
108+
109+ async def list_all_albums (self ) -> list [dict ]:
110+ """List all albums."""
111+ try :
112+ async with aiohttp .ClientSession () as session :
113+ url = urljoin (self .host , "/api/album" )
114+ headers = {"Accept" : "application/json" , _HEADER_API_KEY : self .api_key }
115+
116+ async with session .get (url = url , headers = headers ) as response :
117+ if response .status != 200 :
118+ raw_result = await response .text ()
119+ _LOGGER .error ("Error from API: body=%s" , raw_result )
120+ raise ApiError ()
121+
122+ album_list : list [dict ] = await response .json ()
123+
124+ return album_list
125+ except aiohttp .ClientError as exception :
126+ _LOGGER .error ("Error connecting to the API: %s" , exception )
127+ raise CannotConnect from exception
128+
129+ async def list_album_images (self , album_id : str ) -> list [dict ]:
130+ """List all images in an album."""
131+ try :
132+ async with aiohttp .ClientSession () as session :
133+ url = urljoin (self .host , f"/api/album/{ album_id } " )
134+ headers = {"Accept" : "application/json" , _HEADER_API_KEY : self .api_key }
135+
136+ async with session .get (url = url , headers = headers ) as response :
137+ if response .status != 200 :
138+ raw_result = await response .text ()
139+ _LOGGER .error ("Error from API: body=%s" , raw_result )
140+ raise ApiError ()
141+
142+ album_info : dict = await response .json ()
143+ assets : list [dict ] = album_info ["assets" ]
144+
145+ filtered_assets : list [dict ] = [
146+ asset for asset in assets if asset ["type" ] == "IMAGE"
147+ ]
94148
95- return json_result
149+ return filtered_assets
96150 except aiohttp .ClientError as exception :
97151 _LOGGER .error ("Error connecting to the API: %s" , exception )
98152 raise CannotConnect from exception
0 commit comments