Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Fujitsu HVAC (based on Ayla IOT) integration."""
2 
3 from collections.abc import Mapping
4 import logging
5 from typing import Any
6 
7 from ayla_iot_unofficial import AylaAuthError, new_ayla_api
8 from ayla_iot_unofficial.fujitsu_consts import FGLAIR_APP_CREDENTIALS
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
13 from homeassistant.helpers import aiohttp_client
14 from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig
15 
16 from .const import API_TIMEOUT, CONF_REGION, DOMAIN, REGION_DEFAULT, REGION_EU
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 
21 STEP_USER_DATA_SCHEMA = vol.Schema(
22  {
23  vol.Required(CONF_USERNAME): str,
24  vol.Required(CONF_PASSWORD): str,
25  vol.Required(CONF_REGION, default=REGION_DEFAULT): SelectSelector(
27  options=[region.lower() for region in FGLAIR_APP_CREDENTIALS],
28  translation_key=CONF_REGION,
29  )
30  ),
31  }
32 )
33 STEP_REAUTH_DATA_SCHEMA = vol.Schema(
34  {
35  vol.Required(CONF_PASSWORD): str,
36  }
37 )
38 
39 
40 class FGLairConfigFlow(ConfigFlow, domain=DOMAIN):
41  """Handle a config flow for Fujitsu HVAC (based on Ayla IOT)."""
42 
43  MINOR_VERSION = 2
44 
46  self, user_input: dict[str, Any]
47  ) -> dict[str, str]:
48  errors: dict[str, str] = {}
49  app_id, app_secret = FGLAIR_APP_CREDENTIALS[user_input[CONF_REGION]]
50  api = new_ayla_api(
51  user_input[CONF_USERNAME],
52  user_input[CONF_PASSWORD],
53  app_id,
54  app_secret,
55  europe=user_input[CONF_REGION] == REGION_EU,
56  websession=aiohttp_client.async_get_clientsession(self.hass),
57  timeout=API_TIMEOUT,
58  )
59  try:
60  await api.async_sign_in()
61  except TimeoutError:
62  errors["base"] = "cannot_connect"
63  except AylaAuthError:
64  errors["base"] = "invalid_auth"
65  except Exception: # pylint: disable=broad-except
66  _LOGGER.exception("Unexpected exception")
67  errors["base"] = "unknown"
68 
69  return errors
70 
71  async def async_step_user(
72  self, user_input: dict[str, Any] | None = None
73  ) -> ConfigFlowResult:
74  """Handle the initial step."""
75  errors: dict[str, str] = {}
76  if user_input:
77  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_USERNAME].lower())
78  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
79 
80  errors = await self._async_validate_credentials_async_validate_credentials(user_input)
81  if not errors:
82  return self.async_create_entryasync_create_entryasync_create_entry(
83  title=f"FGLair ({user_input[CONF_USERNAME]})",
84  data=user_input,
85  )
86 
87  return self.async_show_formasync_show_formasync_show_form(
88  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
89  )
90 
91  async def async_step_reauth(
92  self, entry_data: Mapping[str, Any]
93  ) -> ConfigFlowResult:
94  """Perform reauth upon an API authentication error."""
95  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
96 
98  self, user_input: dict[str, Any] | None = None
99  ) -> ConfigFlowResult:
100  """Dialog that informs the user that reauth is required."""
101  errors: dict[str, str] = {}
102 
103  reauth_entry = self._get_reauth_entry_get_reauth_entry()
104  if user_input:
105  errors = await self._async_validate_credentials_async_validate_credentials(
106  reauth_entry.data | user_input
107  )
108 
109  if not errors:
110  return self.async_update_reload_and_abortasync_update_reload_and_abort(
111  reauth_entry, data_updates=user_input
112  )
113 
114  return self.async_show_formasync_show_formasync_show_form(
115  step_id="reauth_confirm",
116  data_schema=STEP_REAUTH_DATA_SCHEMA,
117  description_placeholders={
118  CONF_USERNAME: reauth_entry.data[CONF_USERNAME],
119  **self.context["title_placeholders"],
120  },
121  errors=errors,
122  )
dict[str, str] _async_validate_credentials(self, dict[str, Any] user_input)
Definition: config_flow.py:47
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:73
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:93
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:99
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_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)