Home Assistant Unofficial Reference 2024.12.1
api.py
Go to the documentation of this file.
1 """API for Google Photos bound to Home Assistant OAuth."""
2 
3 from typing import cast
4 
5 import aiohttp
6 from google_photos_library_api import api
7 
8 from homeassistant.const import CONF_ACCESS_TOKEN
9 from homeassistant.helpers import config_entry_oauth2_flow
10 
11 
12 class AsyncConfigEntryAuth(api.AbstractAuth):
13  """Provide Google Photos authentication tied to an OAuth2 based config entry."""
14 
15  def __init__(
16  self,
17  websession: aiohttp.ClientSession,
18  oauth_session: config_entry_oauth2_flow.OAuth2Session,
19  ) -> None:
20  """Initialize AsyncConfigEntryAuth."""
21  super().__init__(websession)
22  self._session_session = oauth_session
23 
24  async def async_get_access_token(self) -> str:
25  """Return a valid access token."""
26  await self._session_session.async_ensure_token_valid()
27  return cast(str, self._session_session.token[CONF_ACCESS_TOKEN])
28 
29 
30 class AsyncConfigFlowAuth(api.AbstractAuth):
31  """An API client used during the config flow with a fixed token."""
32 
33  def __init__(
34  self,
35  websession: aiohttp.ClientSession,
36  token: str,
37  ) -> None:
38  """Initialize ConfigFlowAuth."""
39  super().__init__(websession)
40  self._token_token = token
41 
42  async def async_get_access_token(self) -> str:
43  """Return a valid access token."""
44  return self._token_token
None __init__(self, aiohttp.ClientSession websession, config_entry_oauth2_flow.OAuth2Session oauth_session)
Definition: api.py:19
None __init__(self, aiohttp.ClientSession websession, str token)
Definition: api.py:37