Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Google Photos."""
2 
3 from collections.abc import Mapping
4 import logging
5 from typing import Any
6 
7 from google_photos_library_api.api import GooglePhotosLibraryApi
8 from google_photos_library_api.exceptions import GooglePhotosApiError
9 
10 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
11 from homeassistant.const import CONF_ACCESS_TOKEN, CONF_TOKEN
12 from homeassistant.helpers import aiohttp_client, config_entry_oauth2_flow
13 
14 from . import api
15 from .const import DOMAIN, OAUTH2_SCOPES
16 
17 
19  config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
20 ):
21  """Config flow to handle Google Photos OAuth2 authentication."""
22 
23  DOMAIN = DOMAIN
24 
25  @property
26  def logger(self) -> logging.Logger:
27  """Return logger."""
28  return logging.getLogger(__name__)
29 
30  @property
31  def extra_authorize_data(self) -> dict[str, Any]:
32  """Extra data that needs to be appended to the authorize url."""
33  return {
34  "scope": " ".join(OAUTH2_SCOPES),
35  # Add params to ensure we get back a refresh token
36  "access_type": "offline",
37  "prompt": "consent",
38  }
39 
40  async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
41  """Create an entry for the flow."""
42  session = aiohttp_client.async_get_clientsession(self.hass)
43  auth = api.AsyncConfigFlowAuth(session, data[CONF_TOKEN][CONF_ACCESS_TOKEN])
44  client = GooglePhotosLibraryApi(auth)
45 
46  try:
47  user_resource_info = await client.get_user_info()
48  await client.list_media_items(page_size=1)
49  except GooglePhotosApiError as ex:
50  return self.async_abort(
51  reason="access_not_configured",
52  description_placeholders={"message": str(ex)},
53  )
54  except Exception:
55  self.loggerlogger.exception("Unknown error occurred")
56  return self.async_abort(reason="unknown")
57  user_id = user_resource_info.id
58 
59  await self.async_set_unique_id(user_id)
60  if self.sourcesource == SOURCE_REAUTH:
61  self._abort_if_unique_id_mismatch(reason="wrong_account")
62  return self.async_update_reload_and_abort(
63  self._get_reauth_entry(), data=data
64  )
65 
66  self._abort_if_unique_id_configured()
67  return self.async_create_entry(title=user_resource_info.name, data=data)
68 
69  async def async_step_reauth(
70  self, entry_data: Mapping[str, Any]
71  ) -> ConfigFlowResult:
72  """Perform reauth upon an API authentication error."""
73  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
74 
76  self, user_input: Mapping[str, Any] | None = None
77  ) -> ConfigFlowResult:
78  """Confirm reauth dialog."""
79  if user_input is None:
80  return self.async_show_form(step_id="reauth_confirm")
81  return await self.async_step_user()
ConfigFlowResult async_oauth_create_entry(self, dict[str, Any] data)
Definition: config_flow.py:40
ConfigFlowResult async_step_reauth_confirm(self, Mapping[str, Any]|None user_input=None)
Definition: config_flow.py:77
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:71