Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Yale integration."""
2 
3 from collections.abc import Mapping
4 import logging
5 from typing import Any
6 
7 import jwt
8 
9 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
10 from homeassistant.helpers import config_entry_oauth2_flow
11 
12 from .const import DOMAIN
13 
14 _LOGGER = logging.getLogger(__name__)
15 
16 
17 class YaleConfigFlow(config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN):
18  """Handle a config flow for Yale."""
19 
20  VERSION = 1
21  DOMAIN = DOMAIN
22 
23  @property
24  def logger(self) -> logging.Logger:
25  """Return logger."""
26  return _LOGGER
27 
28  async def async_step_reauth(
29  self, entry_data: Mapping[str, Any]
30  ) -> ConfigFlowResult:
31  """Handle configuration by re-auth."""
32  return await self.async_step_user()
33 
34  def _async_get_user_id_from_access_token(self, encoded: str) -> str:
35  """Get user ID from access token."""
36  decoded = jwt.decode(
37  encoded,
38  "",
39  verify=False,
40  options={"verify_signature": False},
41  algorithms=["HS256"],
42  )
43  return decoded["userId"]
44 
45  async def async_oauth_create_entry(self, data: dict) -> ConfigFlowResult:
46  """Create an entry for the flow."""
47  user_id = self._async_get_user_id_from_access_token_async_get_user_id_from_access_token(
48  data["token"]["access_token"]
49  )
50  await self.async_set_unique_id(user_id)
51  if self.sourcesource == SOURCE_REAUTH:
52  self._abort_if_unique_id_mismatch(reason="reauth_invalid_user")
53  return self.async_update_reload_and_abort(
54  self._get_reauth_entry(), data=data
55  )
56  self._abort_if_unique_id_configured()
57  return await super().async_oauth_create_entry(data)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:30
ConfigFlowResult async_oauth_create_entry(self, dict data)
Definition: config_flow.py:45