Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Modern Forms."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aiomodernforms import ModernFormsConnectionError, ModernFormsDevice
8 import voluptuous as vol
9 
10 from homeassistant.components import zeroconf
11 from homeassistant.config_entries import SOURCE_ZEROCONF, ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_HOST, CONF_MAC
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 
15 from .const import DOMAIN
16 
17 USER_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
18 
19 
20 class ModernFormsFlowHandler(ConfigFlow, domain=DOMAIN):
21  """Handle a ModernForms config flow."""
22 
23  VERSION = 1
24 
25  host: str | None = None
26  mac: str | None = None
27  name: str
28 
29  async def async_step_user(
30  self, user_input: dict[str, Any] | None = None
31  ) -> ConfigFlowResult:
32  """Handle setup by user for Modern Forms integration."""
33  return await self._handle_config_flow(user_input)
34 
35  async def async_step_zeroconf(
36  self, discovery_info: zeroconf.ZeroconfServiceInfo
37  ) -> ConfigFlowResult:
38  """Handle zeroconf discovery."""
39  host = discovery_info.hostname.rstrip(".")
40  name, _ = host.rsplit(".")
41 
42  self.context["title_placeholders"] = {"name": name}
43  self.hosthost = discovery_info.host
44  self.macmac = discovery_info.properties.get(CONF_MAC)
45  self.namename = name
46 
47  # Prepare configuration flow
48  return await self._handle_config_flow({}, True)
49 
50  async def async_step_zeroconf_confirm(
51  self, user_input: dict[str, Any] | None = None
52  ) -> ConfigFlowResult:
53  """Handle a flow initiated by zeroconf."""
54  return await self._handle_config_flow(user_input)
55 
56  async def _handle_config_flow(
57  self, user_input: dict[str, Any] | None = None, prepare: bool = False
58  ) -> ConfigFlowResult:
59  """Config flow handler for ModernForms."""
60  # Request user input, unless we are preparing discovery flow
61  if user_input is None:
62  user_input = {}
63  if not prepare:
64  if self.sourcesourcesourcesource == SOURCE_ZEROCONF:
65  return self.async_show_formasync_show_formasync_show_form(
66  step_id="zeroconf_confirm",
67  description_placeholders={"name": self.namename},
68  )
69  return self.async_show_formasync_show_formasync_show_form(
70  step_id="user",
71  data_schema=USER_SCHEMA,
72  )
73 
74  if self.sourcesourcesourcesource == SOURCE_ZEROCONF:
75  user_input[CONF_HOST] = self.hosthost
76  user_input[CONF_MAC] = self.macmac
77 
78  if user_input.get(CONF_MAC) is None or not prepare:
79  session = async_get_clientsession(self.hass)
80  device = ModernFormsDevice(user_input[CONF_HOST], session=session)
81  try:
82  device = await device.update()
83  except ModernFormsConnectionError:
84  if self.sourcesourcesourcesource == SOURCE_ZEROCONF:
85  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
86  return self.async_show_formasync_show_formasync_show_form(
87  step_id="user",
88  data_schema=USER_SCHEMA,
89  errors={"base": "cannot_connect"},
90  )
91  user_input[CONF_MAC] = device.info.mac_address
92 
93  # Check if already configured
94  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_MAC])
95  self._abort_if_unique_id_configured_abort_if_unique_id_configured(updates={CONF_HOST: user_input[CONF_HOST]})
96 
97  title = device.info.device_name
98  if self.sourcesourcesourcesource == SOURCE_ZEROCONF:
99  title = self.namename
100 
101  if prepare:
102  return await self.async_step_zeroconf_confirm()
103 
104  return self.async_create_entryasync_create_entryasync_create_entry(
105  title=title,
106  data={CONF_HOST: user_input[CONF_HOST], CONF_MAC: user_input[CONF_MAC]},
107  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:31
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_step_zeroconf(self, ZeroconfServiceInfo discovery_info)
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)
str|None source(self)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
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)