Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow support for Intergas InComfort integration."""
2 
3 from typing import Any
4 
5 from aiohttp import ClientResponseError
6 from incomfortclient import IncomfortError, InvalidHeaterList
7 import voluptuous as vol
8 
9 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
10 from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
11 from homeassistant.core import HomeAssistant
13  TextSelector,
14  TextSelectorConfig,
15  TextSelectorType,
16 )
17 
18 from .const import DOMAIN
19 from .coordinator import async_connect_gateway
20 
21 TITLE = "Intergas InComfort/Intouch Lan2RF gateway"
22 
23 CONFIG_SCHEMA = vol.Schema(
24  {
25  vol.Required(CONF_HOST): TextSelector(
26  TextSelectorConfig(type=TextSelectorType.TEXT)
27  ),
28  vol.Optional(CONF_USERNAME): TextSelector(
29  TextSelectorConfig(type=TextSelectorType.TEXT, autocomplete="admin")
30  ),
31  vol.Optional(CONF_PASSWORD): TextSelector(
32  TextSelectorConfig(type=TextSelectorType.PASSWORD)
33  ),
34  }
35 )
36 
37 ERROR_STATUS_MAPPING: dict[int, tuple[str, str]] = {
38  401: (CONF_PASSWORD, "auth_error"),
39  404: ("base", "not_found"),
40 }
41 
42 
44  hass: HomeAssistant, config: dict[str, Any]
45 ) -> dict[str, str] | None:
46  """Try to connect to the Lan2RF gateway."""
47  try:
48  await async_connect_gateway(hass, config)
49  except InvalidHeaterList:
50  return {"base": "no_heaters"}
51  except IncomfortError as exc:
52  if isinstance(exc.message, ClientResponseError):
53  scope, error = ERROR_STATUS_MAPPING.get(
54  exc.message.status, ("base", "unknown")
55  )
56  return {scope: error}
57  return {"base": "unknown"}
58  except TimeoutError:
59  return {"base": "timeout_error"}
60  except Exception: # noqa: BLE001
61  return {"base": "unknown"}
62 
63  return None
64 
65 
66 class InComfortConfigFlow(ConfigFlow, domain=DOMAIN):
67  """Config flow to set up an Intergas InComfort boyler and thermostats."""
68 
69  async def async_step_user(
70  self, user_input: dict[str, Any] | None = None
71  ) -> ConfigFlowResult:
72  """Handle the initial step."""
73  errors: dict[str, str] | None = None
74  if user_input is not None:
75  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
76  if (
77  errors := await async_try_connect_gateway(self.hass, user_input)
78  ) is None:
79  return self.async_create_entryasync_create_entryasync_create_entry(title=TITLE, data=user_input)
80 
81  return self.async_show_formasync_show_formasync_show_form(
82  step_id="user", data_schema=CONFIG_SCHEMA, errors=errors
83  )
84 
85  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
86  """Import `incomfort` config entry from configuration.yaml."""
87  errors: dict[str, str] | None = None
88  if (errors := await async_try_connect_gateway(self.hass, import_data)) is None:
89  return self.async_create_entryasync_create_entryasync_create_entry(title=TITLE, data=import_data)
90  reason = next(iter(errors.items()))[1]
91  return self.async_abortasync_abortasync_abort(reason=reason)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:71
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
dict[str, str]|None async_try_connect_gateway(HomeAssistant hass, dict[str, Any] config)
Definition: config_flow.py:45
InComfortData async_connect_gateway(HomeAssistant hass, dict[str, Any] entry_data)
Definition: coordinator.py:37
config_entries.ConfigFlowResult async_step_import(self, dict[str, Any]|None _)