1 """Coordinator for fetching data from Google Photos API.
3 This coordinator fetches the list of Google Photos albums that were created by
4 Home Assistant, which for large libraries may take some time. The list of album
5 ids and titles is cached and this provides a method to refresh urls since they
12 from typing
import Final
14 from google_photos_library_api.api
import GooglePhotosLibraryApi
15 from google_photos_library_api.exceptions
import GooglePhotosApiError
16 from google_photos_library_api.model
import Album, NewAlbum
21 _LOGGER = logging.getLogger(__name__)
23 UPDATE_INTERVAL: Final = datetime.timedelta(hours=24)
28 """Coordinator for fetching Google Photos albums.
30 The `data` object is a dict from Album ID to Album title.
33 def __init__(self, hass: HomeAssistant, client: GooglePhotosLibraryApi) ->
None:
34 """Initialize TaskUpdateCoordinator."""
39 update_interval=UPDATE_INTERVAL,
44 """Fetch albums from API endpoint."""
45 albums: dict[str, str] = {}
48 page_size=ALBUM_PAGE_SIZE
50 for album
in album_result.albums:
51 albums[album.id] = album.title
52 except GooglePhotosApiError
as err:
53 _LOGGER.debug(
"Error listing albums: %s", err)
54 raise UpdateFailed(f
"Error listing albums: {err}")
from err
58 """Return Albums with refreshed URLs based on the cached list of album ids."""
59 return await asyncio.gather(
60 *(self.
clientclient.get_album(album_id)
for album_id
in self.
datadata)
64 """Return an existing album id or create a new one."""
65 for album_id, album_title
in self.
datadata.items():
66 if album_title == album:
68 new_album = await self.
clientclient.create_album(NewAlbum(title=album))
69 _LOGGER.debug(
"Created new album: %s", new_album)
70 self.
datadata[new_album.id] = new_album.title
list[Album] list_albums(self)
None __init__(self, HomeAssistant hass, GooglePhotosLibraryApi client)
dict[str, str] _async_update_data(self)
str get_or_create_album(self, str album)