Skip to content

Commit

Permalink
feat: move data retrieval to data coordinator for future features
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanoz committed Feb 16, 2023
1 parent 9eb8f33 commit 5d38483
Show file tree
Hide file tree
Showing 5 changed files with 350 additions and 262 deletions.
10 changes: 8 additions & 2 deletions custom_components/google_photos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Google Photos from a config entry."""
entry.async_on_unload(entry.add_update_listener(update_listener))
implementation = await async_get_config_entry_implementation(hass, entry)
session = OAuth2Session(hass, entry, implementation)
auth = AsyncConfigEntryAuth(async_get_clientsession(hass), session)
Expand All @@ -53,7 +52,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
raise ConfigEntryNotReady from err
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = auth

hass.config_entries.async_setup_platforms(entry, PLATFORMS)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(update_listener))

return True

Expand All @@ -77,3 +77,9 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.services.async_remove(DOMAIN, service_name)

return unload_ok


async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Reload config entry."""
await async_unload_entry(hass, entry)
await async_setup_entry(hass, entry)
16 changes: 10 additions & 6 deletions custom_components/google_photos/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""API for Google Photos bound to Home Assistant OAuth."""
from typing import cast
from aiohttp import ClientSession
from google.auth.exceptions import RefreshError
from google.oauth2.credentials import Credentials
Expand Down Expand Up @@ -46,12 +47,15 @@ async def get_resource(self, hass: HomeAssistant) -> PhotosLibraryService:
raise ex

def get_photoslibrary() -> PhotosLibraryService:
return build(
"photoslibrary",
"v1",
credentials=credentials,
cache=self.discovery_cache,
static_discovery=False,
return cast(
build(
"photoslibrary",
"v1",
credentials=credentials,
cache=self.discovery_cache,
static_discovery=False,
),
PhotosLibraryService,
)

return await hass.async_add_executor_job(get_photoslibrary)
Expand Down
Loading

0 comments on commit 5d38483

Please sign in to comment.