-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Google Photos to have a DataUpdateCoordinator for loading albu…
…ms (#126443) * Update Google Photos to have a data update coordiantor for loading albums * Remove album from services * Remove action string changes * Revert services.yaml change * Simplify integration by blocking startup on album loading * Update homeassistant/components/google_photos/coordinator.py --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
- Loading branch information
1 parent
741b025
commit 27bed0c
Showing
9 changed files
with
101 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""Coordinator for fetching data from Google Photos API. | ||
This coordinator fetches the list of Google Photos albums that were created by | ||
Home Assistant, which for large libraries may take some time. The list of album | ||
ids and titles is cached and this provides a method to refresh urls since they | ||
are short lived. | ||
""" | ||
|
||
import asyncio | ||
import datetime | ||
import logging | ||
from typing import Final | ||
|
||
from google_photos_library_api.api import GooglePhotosLibraryApi | ||
from google_photos_library_api.exceptions import GooglePhotosApiError | ||
from google_photos_library_api.model import Album | ||
|
||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
UPDATE_INTERVAL: Final = datetime.timedelta(hours=24) | ||
ALBUM_PAGE_SIZE = 50 | ||
|
||
|
||
class GooglePhotosUpdateCoordinator(DataUpdateCoordinator[dict[str, str]]): | ||
"""Coordinator for fetching Google Photos albums. | ||
The `data` object is a dict from Album ID to Album title. | ||
""" | ||
|
||
def __init__(self, hass: HomeAssistant, client: GooglePhotosLibraryApi) -> None: | ||
"""Initialize TaskUpdateCoordinator.""" | ||
super().__init__( | ||
hass, | ||
_LOGGER, | ||
name="Google Photos", | ||
update_interval=UPDATE_INTERVAL, | ||
) | ||
self.client = client | ||
|
||
async def _async_update_data(self) -> dict[str, str]: | ||
"""Fetch albums from API endpoint.""" | ||
albums: dict[str, str] = {} | ||
try: | ||
async for album_result in await self.client.list_albums( | ||
page_size=ALBUM_PAGE_SIZE | ||
): | ||
for album in album_result.albums: | ||
albums[album.id] = album.title | ||
except GooglePhotosApiError as err: | ||
_LOGGER.debug("Error listing albums: %s", err) | ||
raise UpdateFailed(f"Error listing albums: {err}") from err | ||
return albums | ||
|
||
async def list_albums(self) -> list[Album]: | ||
"""Return Albums with refreshed URLs based on the cached list of album ids.""" | ||
return await asyncio.gather( | ||
*(self.client.get_album(album_id) for album_id in self.data) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
"""Google Photos types.""" | ||
|
||
from google_photos_library_api.api import GooglePhotosLibraryApi | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
|
||
type GooglePhotosConfigEntry = ConfigEntry[GooglePhotosLibraryApi] | ||
from .coordinator import GooglePhotosUpdateCoordinator | ||
|
||
type GooglePhotosConfigEntry = ConfigEntry[GooglePhotosUpdateCoordinator] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters