Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure Renault component."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 import aiohttp
9 from renault_api.const import AVAILABLE_LOCALES
10 from renault_api.gigya.exceptions import GigyaException
11 import voluptuous as vol
12 
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
15 
16 from .const import CONF_KAMEREON_ACCOUNT_ID, CONF_LOCALE, DOMAIN
17 from .renault_hub import RenaultHub
18 
19 USER_SCHEMA = vol.Schema(
20  {
21  vol.Required(CONF_LOCALE): vol.In(AVAILABLE_LOCALES.keys()),
22  vol.Required(CONF_USERNAME): str,
23  vol.Required(CONF_PASSWORD): str,
24  }
25 )
26 REAUTH_SCHEMA = vol.Schema({vol.Required(CONF_PASSWORD): str})
27 
28 
29 class RenaultFlowHandler(ConfigFlow, domain=DOMAIN):
30  """Handle a Renault config flow."""
31 
32  renault_hub: RenaultHub
33 
34  def __init__(self) -> None:
35  """Initialize the Renault config flow."""
36  self.renault_config: dict[str, Any] = {}
37 
38  async def async_step_user(
39  self, user_input: dict[str, Any] | None = None
40  ) -> ConfigFlowResult:
41  """Handle a Renault config flow start.
42 
43  Ask the user for API keys.
44  """
45  errors: dict[str, str] = {}
46  if user_input:
47  locale = user_input[CONF_LOCALE]
48  self.renault_config.update(user_input)
49  self.renault_config.update(AVAILABLE_LOCALES[locale])
50  self.renault_hubrenault_hub = RenaultHub(self.hass, locale)
51  try:
52  login_success = await self.renault_hubrenault_hub.attempt_login(
53  user_input[CONF_USERNAME], user_input[CONF_PASSWORD]
54  )
55  except (aiohttp.ClientConnectionError, GigyaException):
56  errors["base"] = "cannot_connect"
57  except Exception: # noqa: BLE001
58  errors["base"] = "unknown"
59  else:
60  if login_success:
61  return await self.async_step_kamereonasync_step_kamereon()
62  errors["base"] = "invalid_credentials"
63  return self.async_show_formasync_show_formasync_show_form(
64  step_id="user",
65  data_schema=USER_SCHEMA,
66  errors=errors,
67  )
68 
70  self, user_input: dict[str, Any] | None = None
71  ) -> ConfigFlowResult:
72  """Select Kamereon account."""
73  if user_input:
74  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_KAMEREON_ACCOUNT_ID])
75  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
76 
77  self.renault_config.update(user_input)
78  return self.async_create_entryasync_create_entryasync_create_entry(
79  title=user_input[CONF_KAMEREON_ACCOUNT_ID], data=self.renault_config
80  )
81 
82  accounts = await self.renault_hubrenault_hub.get_account_ids()
83  if len(accounts) == 0:
84  return self.async_abortasync_abortasync_abort(reason="kamereon_no_account")
85  if len(accounts) == 1:
86  return await self.async_step_kamereonasync_step_kamereon(
87  user_input={CONF_KAMEREON_ACCOUNT_ID: accounts[0]}
88  )
89 
90  return self.async_show_formasync_show_formasync_show_form(
91  step_id="kamereon",
92  data_schema=vol.Schema(
93  {vol.Required(CONF_KAMEREON_ACCOUNT_ID): vol.In(accounts)}
94  ),
95  )
96 
97  async def async_step_reauth(
98  self, entry_data: Mapping[str, Any]
99  ) -> ConfigFlowResult:
100  """Perform reauth upon an API authentication error."""
101  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
102 
104  self, user_input: dict[str, Any] | None = None
105  ) -> ConfigFlowResult:
106  """Dialog that informs the user that reauth is required."""
107  errors: dict[str, str] = {}
108  reauth_entry = self._get_reauth_entry_get_reauth_entry()
109  if user_input:
110  # Check credentials
111  self.renault_hubrenault_hub = RenaultHub(self.hass, reauth_entry.data[CONF_LOCALE])
112  if await self.renault_hubrenault_hub.attempt_login(
113  reauth_entry.data[CONF_USERNAME], user_input[CONF_PASSWORD]
114  ):
115  return self.async_update_reload_and_abortasync_update_reload_and_abort(
116  reauth_entry,
117  data_updates={CONF_PASSWORD: user_input[CONF_PASSWORD]},
118  )
119  errors = {"base": "invalid_credentials"}
120 
121  return self.async_show_formasync_show_formasync_show_form(
122  step_id="reauth_confirm",
123  data_schema=REAUTH_SCHEMA,
124  errors=errors,
125  description_placeholders={CONF_USERNAME: reauth_entry.data[CONF_USERNAME]},
126  )
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:105
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:99
ConfigFlowResult async_step_kamereon(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:71
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:40
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_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
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)
IssData update(pyiss.ISS iss)
Definition: __init__.py:33