Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for tolo."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from tololib import ToloClient, ToloCommunicationError
9 import voluptuous as vol
10 
11 from homeassistant.components import dhcp
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_HOST
14 from homeassistant.helpers.device_registry import format_mac
15 
16 from .const import DEFAULT_NAME, DOMAIN
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 
21 class ToloSaunaConfigFlow(ConfigFlow, domain=DOMAIN):
22  """ConfigFlow for TOLO Sauna."""
23 
24  VERSION = 1
25 
26  _discovered_host: str
27 
28  @staticmethod
29  def _check_device_availability(host: str) -> bool:
30  client = ToloClient(host)
31  try:
32  result = client.get_status()
33  except ToloCommunicationError:
34  return False
35  return result is not None
36 
37  async def async_step_user(
38  self, user_input: dict[str, Any] | None = None
39  ) -> ConfigFlowResult:
40  """Handle a flow initialized by the user."""
41  errors = {}
42 
43  if user_input is not None:
44  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
45 
46  device_available = await self.hass.async_add_executor_job(
47  self._check_device_availability_check_device_availability, user_input[CONF_HOST]
48  )
49 
50  if not device_available:
51  errors["base"] = "cannot_connect"
52  else:
53  return self.async_create_entryasync_create_entryasync_create_entry(
54  title=DEFAULT_NAME, data={CONF_HOST: user_input[CONF_HOST]}
55  )
56 
57  return self.async_show_formasync_show_formasync_show_form(
58  step_id="user",
59  data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
60  errors=errors,
61  )
62 
63  async def async_step_dhcp(
64  self, discovery_info: dhcp.DhcpServiceInfo
65  ) -> ConfigFlowResult:
66  """Handle a flow initialized by discovery."""
67  await self.async_set_unique_idasync_set_unique_id(format_mac(discovery_info.macaddress))
68  self._abort_if_unique_id_configured_abort_if_unique_id_configured({CONF_HOST: discovery_info.ip})
69  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: discovery_info.ip})
70 
71  device_available = await self.hass.async_add_executor_job(
72  self._check_device_availability_check_device_availability, discovery_info.ip
73  )
74 
75  if device_available:
76  self._discovered_host_discovered_host = discovery_info.ip
77  return await self.async_step_confirmasync_step_confirm()
78  return self.async_abortasync_abortasync_abort(reason="not_tolo_device")
79 
80  async def async_step_confirm(
81  self, user_input: dict[str, Any] | None = None
82  ) -> ConfigFlowResult:
83  """Handle user-confirmation of discovered node."""
84  if user_input is not None:
85  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: self._discovered_host_discovered_host})
86  return self.async_create_entryasync_create_entryasync_create_entry(
87  title=DEFAULT_NAME, data={CONF_HOST: self._discovered_host_discovered_host}
88  )
89 
90  return self.async_show_formasync_show_formasync_show_form(
91  step_id="confirm",
92  description_placeholders={CONF_HOST: self._discovered_host_discovered_host},
93  )
ConfigFlowResult async_step_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:82
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:39
ConfigFlowResult async_step_dhcp(self, dhcp.DhcpServiceInfo discovery_info)
Definition: config_flow.py:65
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)
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)