Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the Twinkly integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from aiohttp import ClientError
9 from ttls.client import Twinkly
10 from voluptuous import Required, Schema
11 
12 from homeassistant.components import dhcp
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_HOST, CONF_ID, CONF_MODEL, CONF_NAME
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 
17 from .const import DEV_ID, DEV_MODEL, DEV_NAME, DOMAIN
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
22 class TwinklyConfigFlow(ConfigFlow, domain=DOMAIN):
23  """Handle twinkly config flow."""
24 
25  VERSION = 1
26 
27  def __init__(self) -> None:
28  """Initialize the config flow."""
29  self._discovered_device_discovered_device: tuple[dict[str, Any], str] | None = None
30 
31  async def async_step_user(
32  self, user_input: dict[str, Any] | None = None
33  ) -> ConfigFlowResult:
34  """Handle config steps."""
35  host = user_input[CONF_HOST] if user_input else None
36 
37  schema = {Required(CONF_HOST, default=host): str}
38  errors = {}
39 
40  if host is not None:
41  try:
42  device_info = await Twinkly(
43  host, async_get_clientsession(self.hass)
44  ).get_details()
45  except (TimeoutError, ClientError):
46  errors[CONF_HOST] = "cannot_connect"
47  else:
48  await self.async_set_unique_idasync_set_unique_id(device_info[DEV_ID])
49  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
50 
51  return self._create_entry_from_device_create_entry_from_device(device_info, host)
52 
53  return self.async_show_formasync_show_formasync_show_form(
54  step_id="user", data_schema=Schema(schema), errors=errors
55  )
56 
57  async def async_step_dhcp(
58  self, discovery_info: dhcp.DhcpServiceInfo
59  ) -> ConfigFlowResult:
60  """Handle dhcp discovery for twinkly."""
61  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: discovery_info.ip})
62  device_info = await Twinkly(
63  discovery_info.ip, async_get_clientsession(self.hass)
64  ).get_details()
65  await self.async_set_unique_idasync_set_unique_id(device_info[DEV_ID])
66  self._abort_if_unique_id_configured_abort_if_unique_id_configured(updates={CONF_HOST: discovery_info.ip})
67 
68  self._discovered_device_discovered_device = (device_info, discovery_info.ip)
69  return await self.async_step_discovery_confirmasync_step_discovery_confirm()
70 
71  async def async_step_discovery_confirm(self, user_input=None) -> ConfigFlowResult:
72  """Confirm discovery."""
73  assert self._discovered_device_discovered_device is not None
74  device_info, host = self._discovered_device_discovered_device
75 
76  if user_input is not None:
77  return self._create_entry_from_device_create_entry_from_device(device_info, host)
78 
79  self._set_confirm_only_set_confirm_only()
80  placeholders = {
81  "model": device_info[DEV_MODEL],
82  "name": device_info[DEV_NAME],
83  "host": host,
84  }
85  return self.async_show_formasync_show_formasync_show_form(
86  step_id="discovery_confirm", description_placeholders=placeholders
87  )
88 
90  self, device_info: dict[str, Any], host: str
91  ) -> ConfigFlowResult:
92  """Create entry from device data."""
93  return self.async_create_entryasync_create_entryasync_create_entry(
94  title=device_info[DEV_NAME],
95  data={
96  CONF_HOST: host,
97  CONF_ID: device_info[DEV_ID],
98  CONF_NAME: device_info[DEV_NAME],
99  CONF_MODEL: device_info[DEV_MODEL],
100  },
101  )
ConfigFlowResult async_step_dhcp(self, dhcp.DhcpServiceInfo discovery_info)
Definition: config_flow.py:59
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:33
ConfigFlowResult _create_entry_from_device(self, dict[str, Any] device_info, str host)
Definition: config_flow.py:91
ConfigFlowResult async_step_discovery_confirm(self, user_input=None)
Definition: config_flow.py:71
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)
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)
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)