1 """Config flow for WiZ Platform."""
3 from __future__
import annotations
8 from pywizlight
import wizlight
9 from pywizlight.discovery
import DiscoveredBulb
10 from pywizlight.exceptions
import WizLightConnectionError, WizLightTimeOutError
11 import voluptuous
as vol
19 from .const
import DEFAULT_NAME, DISCOVER_SCAN_TIMEOUT, DOMAIN, WIZ_CONNECT_EXCEPTIONS
20 from .discovery
import async_discover_devices
21 from .utils
import _short_mac, name_from_bulb_type_and_mac
23 _LOGGER = logging.getLogger(__name__)
25 CONF_DEVICE =
"device"
29 """Handle a config flow for WiZ."""
33 _discovered_device: DiscoveredBulb
37 """Initialize the config flow."""
41 self, discovery_info: dhcp.DhcpServiceInfo
42 ) -> ConfigFlowResult:
43 """Handle discovery via dhcp."""
45 discovery_info.ip, discovery_info.macaddress
50 self, discovery_info: dict[str, str]
51 ) -> ConfigFlowResult:
52 """Handle integration discovery."""
54 discovery_info[
"ip_address"], discovery_info[
"mac_address"]
59 """Handle any discovery."""
61 _LOGGER.debug(
"Discovered device: %s", device)
62 ip_address = device.ip_address
63 mac = device.mac_address
70 """Connect to the device and verify its responding."""
72 bulb = wizlight(device.ip_address)
74 bulbtype = await bulb.get_bulbtype()
75 except WIZ_CONNECT_EXCEPTIONS
as ex:
77 "Failed to connect to %s during discovery: %s",
86 self, user_input: dict[str, Any] |
None =
None
87 ) -> ConfigFlowResult:
88 """Confirm discovery."""
90 if user_input
is not None or not onboarding.async_is_onboarded(self.hass):
96 title=self.
_name_name,
97 data={CONF_HOST: ip_address},
101 placeholders = {
"name": self.
_name_name,
"host": ip_address}
102 self.context[
"title_placeholders"] = placeholders
104 step_id=
"discovery_confirm",
105 description_placeholders=placeholders,
109 self, user_input: dict[str, Any] |
None =
None
110 ) -> ConfigFlowResult:
111 """Handle the step to pick discovered device."""
112 if user_input
is not None:
114 await self.
async_set_unique_idasync_set_unique_id(device.mac_address, raise_on_progress=
False)
115 bulb = wizlight(device.ip_address)
117 bulbtype = await bulb.get_bulbtype()
118 except WIZ_CONNECT_EXCEPTIONS:
123 data={CONF_HOST: device.ip_address},
128 entry.data[CONF_HOST]
132 self.hass, DISCOVER_SCAN_TIMEOUT
135 device.mac_address: device
for device
in discovered_devices
138 mac: f
"{DEFAULT_NAME} {_short_mac(mac)} ({device.ip_address})"
140 if mac
not in current_unique_ids
and device.ip_address
not in current_hosts
146 step_id=
"pick_device",
147 data_schema=vol.Schema({vol.Required(CONF_DEVICE): vol.In(devices_name)}),
151 self, user_input: dict[str, Any] |
None =
None
152 ) -> ConfigFlowResult:
153 """Handle a flow initialized by the user."""
155 if user_input
is not None:
156 if not (host := user_input[CONF_HOST]):
159 errors[
"base"] =
"no_ip"
161 bulb = wizlight(host)
163 bulbtype = await bulb.get_bulbtype()
164 mac = await bulb.getMac()
165 except WizLightTimeOutError:
166 errors[
"base"] =
"bulb_time_out"
167 except ConnectionRefusedError:
168 errors[
"base"] =
"cannot_connect"
169 except WizLightConnectionError:
170 errors[
"base"] =
"no_wiz_light"
172 _LOGGER.exception(
"Unexpected exception")
173 errors[
"base"] =
"unknown"
177 updates={CONF_HOST: user_input[CONF_HOST]}
187 data_schema=vol.Schema({vol.Optional(CONF_HOST, default=
""): str}),
None _async_connect_discovered_or_abort(self)
ConfigFlowResult _async_handle_discovery(self)
ConfigFlowResult async_step_discovery_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_dhcp(self, dhcp.DhcpServiceInfo discovery_info)
ConfigFlowResult async_step_integration_discovery(self, dict[str, str] discovery_info)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_pick_device(self, dict[str, Any]|None user_input=None)
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)
set[str|None] _async_current_ids(self, bool include_ignore=True)
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)
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)
str name_from_bulb_type_and_mac(BulbType bulb_type, str mac)
bool is_ip_address(str address)