Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Withings."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any
8 
9 from aiowithings import AuthScope
10 
11 from homeassistant.components.webhook import async_generate_id
12 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
13 from homeassistant.const import CONF_NAME, CONF_TOKEN, CONF_WEBHOOK_ID
14 from homeassistant.helpers import config_entry_oauth2_flow
15 
16 from .const import DEFAULT_TITLE, DOMAIN
17 
18 
20  config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
21 ):
22  """Handle a config flow."""
23 
24  DOMAIN = DOMAIN
25 
26  @property
27  def logger(self) -> logging.Logger:
28  """Return logger."""
29  return logging.getLogger(__name__)
30 
31  @property
32  def extra_authorize_data(self) -> dict[str, str]:
33  """Extra data that needs to be appended to the authorize url."""
34  return {
35  "scope": f"{AuthScope.USER_INFO},{AuthScope.USER_METRICS},"
36  f"{AuthScope.USER_ACTIVITY},{AuthScope.USER_SLEEP_EVENTS}"
37  }
38 
39  async def async_step_reauth(
40  self, entry_data: Mapping[str, Any]
41  ) -> ConfigFlowResult:
42  """Perform reauth upon an API authentication error."""
43  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
44 
46  self, user_input: dict[str, Any] | None = None
47  ) -> ConfigFlowResult:
48  """Confirm reauth dialog."""
49  if user_input is None:
50  return self.async_show_form(
51  step_id="reauth_confirm",
52  description_placeholders={CONF_NAME: self._get_reauth_entry().title},
53  )
54  return await self.async_step_user()
55 
56  async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
57  """Create an entry for the flow, or update existing entry."""
58  user_id = str(data[CONF_TOKEN]["userid"])
59  await self.async_set_unique_id(user_id)
60  if self.source != SOURCE_REAUTH:
61  self._abort_if_unique_id_configured()
62 
63  return self.async_create_entry(
64  title=DEFAULT_TITLE,
65  data={**data, CONF_WEBHOOK_ID: async_generate_id()},
66  )
67 
68  self._abort_if_unique_id_mismatch(reason="wrong_account")
69  return self.async_update_reload_and_abort(
70  self._get_reauth_entry(), data_updates=data
71  )
ConfigFlowResult async_oauth_create_entry(self, dict[str, Any] data)
Definition: config_flow.py:56
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:47
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:41