Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for TechnoVE."""
2 
3 from typing import Any
4 
5 from technove import Station as TechnoVEStation, TechnoVE, TechnoVEConnectionError
6 import voluptuous as vol
7 
8 from homeassistant.components import onboarding, zeroconf
9 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
10 from homeassistant.const import CONF_HOST, CONF_MAC
11 from homeassistant.helpers.aiohttp_client import async_get_clientsession
12 
13 from .const import DOMAIN
14 
15 
16 class TechnoVEConfigFlow(ConfigFlow, domain=DOMAIN):
17  """Handle a config flow for TechnoVE."""
18 
19  VERSION = 1
20  discovered_host: str
21  discovered_station: TechnoVEStation
22 
23  async def async_step_user(
24  self, user_input: dict[str, Any] | None = None
25  ) -> ConfigFlowResult:
26  """Handle a flow initiated by the user."""
27  errors = {}
28  if user_input is not None:
29  try:
30  station = await self._async_get_station_async_get_station(user_input[CONF_HOST])
31  except TechnoVEConnectionError:
32  errors["base"] = "cannot_connect"
33  else:
34  await self.async_set_unique_idasync_set_unique_id(station.info.mac_address)
35  self._abort_if_unique_id_configured_abort_if_unique_id_configured(
36  updates={CONF_HOST: user_input[CONF_HOST]}
37  )
38  return self.async_create_entryasync_create_entryasync_create_entry(
39  title=station.info.name,
40  data={
41  CONF_HOST: user_input[CONF_HOST],
42  },
43  )
44 
45  return self.async_show_formasync_show_formasync_show_form(
46  step_id="user",
47  data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
48  errors=errors,
49  )
50 
52  self, discovery_info: zeroconf.ZeroconfServiceInfo
53  ) -> ConfigFlowResult:
54  """Handle zeroconf discovery."""
55  # Abort quick if the device with provided mac is already configured
56  if mac := discovery_info.properties.get(CONF_MAC):
57  await self.async_set_unique_idasync_set_unique_id(mac)
58  self._abort_if_unique_id_configured_abort_if_unique_id_configured(
59  updates={CONF_HOST: discovery_info.host}
60  )
61 
62  self.discovered_hostdiscovered_host = discovery_info.host
63  try:
64  self.discovered_stationdiscovered_station = await self._async_get_station_async_get_station(discovery_info.host)
65  except TechnoVEConnectionError:
66  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
67 
68  await self.async_set_unique_idasync_set_unique_id(self.discovered_stationdiscovered_station.info.mac_address)
69  self._abort_if_unique_id_configured_abort_if_unique_id_configured(updates={CONF_HOST: discovery_info.host})
70 
71  self.context.update(
72  {
73  "title_placeholders": {"name": self.discovered_stationdiscovered_station.info.name},
74  }
75  )
76  return await self.async_step_zeroconf_confirmasync_step_zeroconf_confirm()
77 
79  self, user_input: dict[str, Any] | None = None
80  ) -> ConfigFlowResult:
81  """Handle a flow initiated by zeroconf."""
82  if user_input is not None or not onboarding.async_is_onboarded(self.hass):
83  return self.async_create_entryasync_create_entryasync_create_entry(
84  title=self.discovered_stationdiscovered_station.info.name,
85  data={
86  CONF_HOST: self.discovered_hostdiscovered_host,
87  },
88  )
89 
90  return self.async_show_formasync_show_formasync_show_form(
91  step_id="zeroconf_confirm",
92  description_placeholders={"name": self.discovered_stationdiscovered_station.info.name},
93  )
94 
95  async def _async_get_station(self, host: str) -> TechnoVEStation:
96  """Get information from a TechnoVE station."""
97  api = TechnoVE(host, session=async_get_clientsession(self.hass))
98  return await api.update()
ConfigFlowResult async_step_zeroconf(self, zeroconf.ZeroconfServiceInfo discovery_info)
Definition: config_flow.py:53
ConfigFlowResult async_step_zeroconf_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:80
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:25
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)
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)