Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Ridwell integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import TYPE_CHECKING, Any
7 
8 from aioridwell import async_get_client
9 from aioridwell.errors import InvalidCredentialsError, RidwellError
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
14 from homeassistant.helpers import aiohttp_client, config_validation as cv
15 
16 from .const import DOMAIN, LOGGER
17 
18 STEP_REAUTH_CONFIRM_DATA_SCHEMA = vol.Schema(
19  {
20  vol.Required(CONF_PASSWORD): cv.string,
21  }
22 )
23 
24 STEP_USER_DATA_SCHEMA = vol.Schema(
25  {
26  vol.Required(CONF_USERNAME): cv.string,
27  vol.Required(CONF_PASSWORD): cv.string,
28  }
29 )
30 
31 
32 class RidwellConfigFlow(ConfigFlow, domain=DOMAIN):
33  """Handle a config flow for Ridwell."""
34 
35  VERSION = 2
36 
37  def __init__(self) -> None:
38  """Initialize."""
39  self._password_password: str | None = None
40  self._username_username: str | None = None
41 
42  async def _async_validate(
43  self, error_step_id: str, error_schema: vol.Schema
44  ) -> ConfigFlowResult:
45  """Validate input credentials and proceed accordingly."""
46  errors = {}
47  session = aiohttp_client.async_get_clientsession(self.hass)
48 
49  if TYPE_CHECKING:
50  assert self._password_password
51  assert self._username_username
52 
53  try:
54  await async_get_client(self._username_username, self._password_password, session=session)
55  except InvalidCredentialsError:
56  errors["base"] = "invalid_auth"
57  except RidwellError as err:
58  LOGGER.error("Unknown Ridwell error: %s", err)
59  errors["base"] = "unknown"
60 
61  if errors:
62  return self.async_show_formasync_show_formasync_show_form(
63  step_id=error_step_id,
64  data_schema=error_schema,
65  errors=errors,
66  description_placeholders={CONF_USERNAME: self._username_username},
67  )
68 
69  if existing_entry := await self.async_set_unique_idasync_set_unique_id(self._username_username):
70  self.hass.config_entries.async_update_entry(
71  existing_entry,
72  data={**existing_entry.data, CONF_PASSWORD: self._password_password},
73  )
74  self.hass.async_create_task(
75  self.hass.config_entries.async_reload(existing_entry.entry_id)
76  )
77  return self.async_abortasync_abortasync_abort(reason="reauth_successful")
78 
79  return self.async_create_entryasync_create_entryasync_create_entry(
80  title=self._username_username,
81  data={CONF_USERNAME: self._username_username, CONF_PASSWORD: self._password_password},
82  )
83 
84  async def async_step_reauth(
85  self, entry_data: Mapping[str, Any]
86  ) -> ConfigFlowResult:
87  """Handle configuration by re-auth."""
88  self._username_username = entry_data[CONF_USERNAME]
89  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
90 
92  self, user_input: dict[str, Any] | None = None
93  ) -> ConfigFlowResult:
94  """Handle re-auth completion."""
95  if not user_input:
96  if TYPE_CHECKING:
97  assert self._username_username
98 
99  return self.async_show_formasync_show_formasync_show_form(
100  step_id="reauth_confirm",
101  data_schema=STEP_REAUTH_CONFIRM_DATA_SCHEMA,
102  description_placeholders={CONF_USERNAME: self._username_username},
103  )
104 
105  self._password_password = user_input[CONF_PASSWORD]
106 
107  return await self._async_validate_async_validate(
108  "reauth_confirm", STEP_REAUTH_CONFIRM_DATA_SCHEMA
109  )
110 
111  async def async_step_user(
112  self, user_input: dict[str, Any] | None = None
113  ) -> ConfigFlowResult:
114  """Handle the initial step."""
115  if not user_input:
116  return self.async_show_formasync_show_formasync_show_form(
117  step_id="user", data_schema=STEP_USER_DATA_SCHEMA
118  )
119 
120  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_USERNAME].lower())
121  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
122 
123  self._username_username = user_input[CONF_USERNAME]
124  self._password_password = user_input[CONF_PASSWORD]
125 
126  return await self._async_validate_async_validate("user", STEP_USER_DATA_SCHEMA)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:86
ConfigFlowResult _async_validate(self, str error_step_id, vol.Schema error_schema)
Definition: config_flow.py:44
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:113
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:93
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)