1 """Config flow flow LIFX."""
3 from __future__
import annotations
6 from typing
import Any, Self
8 from aiolifx.aiolifx
import Light
9 from aiolifx.connection
import LIFXConnection
10 import voluptuous
as vol
28 from .discovery
import async_discover_devices
30 async_entry_is_legacy,
31 async_get_legacy_entry,
32 async_multi_execute_lifx_with_retries,
35 mac_matches_serial_number,
40 """Handle a config flow for LIFX."""
44 host: str |
None =
None
47 """Initialize the config flow."""
52 self, discovery_info: DhcpServiceInfo
53 ) -> ConfigFlowResult:
54 """Handle discovery via DHCP."""
55 mac = discovery_info.macaddress
56 host = discovery_info.ip
64 if entry.data[CONF_HOST] != host:
65 hass.config_entries.async_update_entry(
66 entry, data={**entry.data, CONF_HOST: host}
68 hass.async_create_task(
69 hass.config_entries.async_reload(entry.entry_id)
75 self, discovery_info: zeroconf.ZeroconfServiceInfo
76 ) -> ConfigFlowResult:
77 """Handle HomeKit discovery."""
81 self, discovery_info: DiscoveryInfoType
82 ) -> ConfigFlowResult:
83 """Handle LIFX UDP broadcast discovery."""
84 serial = discovery_info[CONF_SERIAL]
85 host = discovery_info[CONF_HOST]
91 self, host: str, serial: str |
None =
None
92 ) -> ConfigFlowResult:
93 """Handle any discovery."""
96 if self.hass.config_entries.flow.async_has_matching_flow(self):
100 host, serial=serial, raise_on_progress=
True
108 """Return True if other_flow is matching this flow."""
109 return other_flow.host == self.
hosthost
113 """Check if a discovered device is pending migration."""
114 assert self.
unique_idunique_id
is not None
117 device_registry = dr.async_get(self.hass)
118 existing_device = device_registry.async_get_device(
119 identifiers={(DOMAIN, self.
unique_idunique_id)}
122 existing_device
is not None
123 and legacy_entry.entry_id
in existing_device.config_entries
127 self, user_input: dict[str, Any] |
None =
None
128 ) -> ConfigFlowResult:
129 """Confirm discovery."""
133 "Confirming discovery of %s (%s) [%s]",
144 "label": discovered.label,
145 "group": discovered.group,
147 self.context[
"title_placeholders"] = placeholders
149 step_id=
"discovery_confirm", description_placeholders=placeholders
153 self, user_input: dict[str, Any] |
None =
None
154 ) -> ConfigFlowResult:
155 """Handle the initial step."""
157 if user_input
is not None:
158 host = user_input[CONF_HOST]
162 device := await self.
_async_try_connect_async_try_connect(host, raise_on_progress=
False)
164 errors[
"base"] =
"cannot_connect"
170 data_schema=vol.Schema({vol.Optional(CONF_HOST, default=
""): str}),
175 self, user_input: dict[str, Any] |
None =
None
176 ) -> ConfigFlowResult:
177 """Handle the step to pick discovered device."""
178 if user_input
is not None:
179 serial = user_input[CONF_DEVICE]
183 device_without_label.ip_addr, raise_on_progress=
False
189 configured_serials: set[str] = set()
190 configured_hosts: set[str] = set()
193 configured_serials.add(entry.unique_id)
194 configured_hosts.add(entry.data[CONF_HOST])
197 device.mac_addr: device
201 serial: f
"{serial} ({device.ip_addr})"
203 if serial
not in configured_serials
204 and device.ip_addr
not in configured_hosts
210 step_id=
"pick_device",
211 data_schema=vol.Schema({vol.Required(CONF_DEVICE): vol.In(devices_name)}),
216 """Create a config entry from a smart device."""
220 data={CONF_HOST: device.ip_addr},
224 self, host: str, serial: str |
None =
None, raise_on_progress: bool =
True
226 """Try to connect."""
228 connection = LIFXConnection(host, TARGET_ANY)
230 await connection.async_setup()
231 except socket.gaierror:
233 device: Light = connection.device
241 device.get_hostfirmware,
252 connection.async_stop()
255 or len(messages) != 4
257 or device.host_firmware_version
is None
261 device.mac_addr = serial
or messages[0].target_addr
ConfigFlowResult _async_create_entry_from_device(self, Light device)
ConfigFlowResult async_step_dhcp(self, DhcpServiceInfo discovery_info)
ConfigFlowResult async_step_homekit(self, zeroconf.ZeroconfServiceInfo discovery_info)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult _async_handle_discovery(self, str host, str|None serial=None)
Light|None _async_try_connect(self, str host, str|None serial=None, bool raise_on_progress=True)
ConfigFlowResult async_step_pick_device(self, dict[str, Any]|None user_input=None)
bool _async_discovered_pending_migration(self)
ConfigFlowResult async_step_integration_discovery(self, DiscoveryInfoType discovery_info)
ConfigFlowResult async_step_discovery_confirm(self, dict[str, Any]|None user_input=None)
bool is_matching(self, Self other_flow)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
None _set_confirm_only(self)
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)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=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)
list[ElkSystem] async_discover_devices(HomeAssistant hass, int timeout, str|None address=None)
ConfigEntry|None async_get_legacy_entry(HomeAssistant hass)
list[Message] async_multi_execute_lifx_with_retries(list[Callable] methods, int attempts, int overall_timeout)
bool async_entry_is_legacy(ConfigEntry entry)
bool mac_matches_serial_number(str mac_addr, str serial_number)
str formatted_serial(str serial_number)
dict[str, Any] lifx_features(Light bulb)