Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the WLED integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 from wled import WLED, Device, WLEDConnectionError
9 
10 from homeassistant.components import onboarding, zeroconf
11 from homeassistant.config_entries import (
12  ConfigEntry,
13  ConfigFlow,
14  ConfigFlowResult,
15  OptionsFlow,
16 )
17 from homeassistant.const import CONF_HOST, CONF_MAC
18 from homeassistant.core import callback
19 from homeassistant.helpers.aiohttp_client import async_get_clientsession
20 
21 from .const import CONF_KEEP_MAIN_LIGHT, DEFAULT_KEEP_MAIN_LIGHT, DOMAIN
22 
23 
24 class WLEDFlowHandler(ConfigFlow, domain=DOMAIN):
25  """Handle a WLED config flow."""
26 
27  VERSION = 1
28  discovered_host: str
29  discovered_device: Device
30 
31  @staticmethod
32  @callback
34  config_entry: ConfigEntry,
35  ) -> WLEDOptionsFlowHandler:
36  """Get the options flow for this handler."""
37  return WLEDOptionsFlowHandler()
38 
39  async def async_step_user(
40  self, user_input: dict[str, Any] | None = None
41  ) -> ConfigFlowResult:
42  """Handle a flow initiated by the user."""
43  errors = {}
44 
45  if user_input is not None:
46  try:
47  device = await self._async_get_device_async_get_device(user_input[CONF_HOST])
48  except WLEDConnectionError:
49  errors["base"] = "cannot_connect"
50  else:
51  await self.async_set_unique_idasync_set_unique_id(
52  device.info.mac_address, raise_on_progress=False
53  )
54  self._abort_if_unique_id_configured_abort_if_unique_id_configured(
55  updates={CONF_HOST: user_input[CONF_HOST]}
56  )
57  return self.async_create_entryasync_create_entryasync_create_entry(
58  title=device.info.name,
59  data={
60  CONF_HOST: user_input[CONF_HOST],
61  },
62  )
63 
64  return self.async_show_formasync_show_formasync_show_form(
65  step_id="user",
66  data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
67  errors=errors or {},
68  )
69 
71  self, discovery_info: zeroconf.ZeroconfServiceInfo
72  ) -> ConfigFlowResult:
73  """Handle zeroconf discovery."""
74  # Abort quick if the mac address is provided by discovery info
75  if mac := discovery_info.properties.get(CONF_MAC):
76  await self.async_set_unique_idasync_set_unique_id(mac)
77  self._abort_if_unique_id_configured_abort_if_unique_id_configured(
78  updates={CONF_HOST: discovery_info.host}
79  )
80 
81  self.discovered_hostdiscovered_host = discovery_info.host
82  try:
83  self.discovered_devicediscovered_device = await self._async_get_device_async_get_device(discovery_info.host)
84  except WLEDConnectionError:
85  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
86 
87  await self.async_set_unique_idasync_set_unique_id(self.discovered_devicediscovered_device.info.mac_address)
88  self._abort_if_unique_id_configured_abort_if_unique_id_configured(updates={CONF_HOST: discovery_info.host})
89 
90  self.context.update(
91  {
92  "title_placeholders": {"name": self.discovered_devicediscovered_device.info.name},
93  "configuration_url": f"http://{discovery_info.host}",
94  }
95  )
96  return await self.async_step_zeroconf_confirmasync_step_zeroconf_confirm()
97 
99  self, user_input: dict[str, Any] | None = None
100  ) -> ConfigFlowResult:
101  """Handle a flow initiated by zeroconf."""
102  if user_input is not None or not onboarding.async_is_onboarded(self.hass):
103  return self.async_create_entryasync_create_entryasync_create_entry(
104  title=self.discovered_devicediscovered_device.info.name,
105  data={
106  CONF_HOST: self.discovered_hostdiscovered_host,
107  },
108  )
109 
110  return self.async_show_formasync_show_formasync_show_form(
111  step_id="zeroconf_confirm",
112  description_placeholders={"name": self.discovered_devicediscovered_device.info.name},
113  )
114 
115  async def _async_get_device(self, host: str) -> Device:
116  """Get device information from WLED device."""
117  session = async_get_clientsession(self.hass)
118  wled = WLED(host, session=session)
119  return await wled.update()
120 
121 
123  """Handle WLED options."""
124 
125  async def async_step_init(
126  self, user_input: dict[str, Any] | None = None
127  ) -> ConfigFlowResult:
128  """Manage WLED options."""
129  if user_input is not None:
130  return self.async_create_entryasync_create_entry(title="", data=user_input)
131 
132  return self.async_show_formasync_show_form(
133  step_id="init",
134  data_schema=vol.Schema(
135  {
136  vol.Optional(
137  CONF_KEEP_MAIN_LIGHT,
138  default=self.config_entryconfig_entryconfig_entry.options.get(
139  CONF_KEEP_MAIN_LIGHT, DEFAULT_KEEP_MAIN_LIGHT
140  ),
141  ): bool,
142  }
143  ),
144  )
ConfigFlowResult async_step_zeroconf(self, zeroconf.ZeroconfServiceInfo discovery_info)
Definition: config_flow.py:72
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:41
WLEDOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:35
ConfigFlowResult async_step_zeroconf_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:100
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:127
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)
None config_entry(self, ConfigEntry value)
_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)