Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Monzo."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any
8 
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
12 from homeassistant.const import CONF_TOKEN
13 from homeassistant.helpers import config_entry_oauth2_flow
14 
15 from .const import DOMAIN
16 
17 
19  config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
20 ):
21  """Handle a config flow."""
22 
23  DOMAIN = DOMAIN
24 
25  oauth_data: dict[str, Any]
26 
27  @property
28  def logger(self) -> logging.Logger:
29  """Return logger."""
30  return logging.getLogger(__name__)
31 
33  self, user_input: dict[str, Any] | None = None
34  ) -> ConfigFlowResult:
35  """Wait for the user to confirm in-app approval."""
36  if user_input is not None:
37  if self.source != SOURCE_REAUTH:
38  return self.async_create_entry(title=DOMAIN, data=self.oauth_dataoauth_data)
39  return self.async_update_reload_and_abort(
40  self._get_reauth_entry(),
41  data_updates=self.oauth_dataoauth_data,
42  )
43 
44  data_schema = vol.Schema({vol.Required("confirm"): bool})
45 
46  return self.async_show_form(
47  step_id="await_approval_confirmation", data_schema=data_schema
48  )
49 
50  async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
51  """Create an entry for the flow."""
52  self.oauth_dataoauth_data = data
53  user_id = data[CONF_TOKEN]["user_id"]
54  await self.async_set_unique_id(user_id)
55  if self.source != SOURCE_REAUTH:
56  self._abort_if_unique_id_configured()
57  else:
58  self._abort_if_unique_id_mismatch(reason="wrong_account")
59 
60  return await self.async_step_await_approval_confirmationasync_step_await_approval_confirmation()
61 
62  async def async_step_reauth(
63  self, entry_data: Mapping[str, Any]
64  ) -> ConfigFlowResult:
65  """Perform reauth upon an API authentication error."""
66  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
67 
69  self, user_input: dict[str, Any] | None = None
70  ) -> ConfigFlowResult:
71  """Confirm reauth dialog."""
72  if user_input is None:
73  return self.async_show_form(step_id="reauth_confirm")
74  return await self.async_step_user()
ConfigFlowResult async_oauth_create_entry(self, dict[str, Any] data)
Definition: config_flow.py:50
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:70
ConfigFlowResult async_step_await_approval_confirmation(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:34
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:64