Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Google Photos integration."""
2 
3 from __future__ import annotations
4 
5 from aiohttp import ClientError, ClientResponseError
6 from google_photos_library_api.api import GooglePhotosLibraryApi
7 
8 from homeassistant.core import HomeAssistant
9 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
10 from homeassistant.helpers import config_entry_oauth2_flow
11 from homeassistant.helpers.aiohttp_client import async_get_clientsession
12 
13 from . import api
14 from .const import DOMAIN
15 from .coordinator import GooglePhotosUpdateCoordinator
16 from .services import async_register_services
17 from .types import GooglePhotosConfigEntry
18 
19 __all__ = [
20  "DOMAIN",
21 ]
22 
23 
25  hass: HomeAssistant, entry: GooglePhotosConfigEntry
26 ) -> bool:
27  """Set up Google Photos from a config entry."""
28  implementation = (
29  await config_entry_oauth2_flow.async_get_config_entry_implementation(
30  hass, entry
31  )
32  )
33  web_session = async_get_clientsession(hass)
34  oauth_session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
35  auth = api.AsyncConfigEntryAuth(web_session, oauth_session)
36  try:
37  await auth.async_get_access_token()
38  except ClientResponseError as err:
39  if 400 <= err.status < 500:
41  "OAuth session is not valid, reauth required"
42  ) from err
43  raise ConfigEntryNotReady from err
44  except ClientError as err:
45  raise ConfigEntryNotReady from err
46  coordinator = GooglePhotosUpdateCoordinator(hass, GooglePhotosLibraryApi(auth))
47  await coordinator.async_config_entry_first_refresh()
48  entry.runtime_data = coordinator
49 
51 
52  return True
53 
54 
56  hass: HomeAssistant, entry: GooglePhotosConfigEntry
57 ) -> bool:
58  """Unload a config entry."""
59  return True
None async_register_services(HomeAssistant hass)
Definition: services.py:80
bool async_unload_entry(HomeAssistant hass, GooglePhotosConfigEntry entry)
Definition: __init__.py:57
bool async_setup_entry(HomeAssistant hass, GooglePhotosConfigEntry entry)
Definition: __init__.py:26
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)