1 """WiZ Platform integration."""
3 from __future__
import annotations
5 from datetime
import timedelta
9 from pywizlight
import PilotParser, wizlight
10 from pywizlight.bulb
import PIR_SOURCE
24 DISCOVER_SCAN_TIMEOUT,
28 WIZ_CONNECT_EXCEPTIONS,
31 from .discovery
import async_discover_devices, async_trigger_discovery
32 from .models
import WizData
34 type WizConfigEntry = ConfigEntry[WizData]
36 _LOGGER = logging.getLogger(__name__)
39 Platform.BINARY_SENSOR,
46 REQUEST_REFRESH_DELAY = 0.35
48 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
51 async
def async_setup(hass: HomeAssistant, hass_config: ConfigType) -> bool:
52 """Set up the wiz integration."""
54 async
def _async_discovery(*_: Any) ->
None:
59 hass.async_create_background_task(_async_discovery(),
"wiz-discovery")
61 hass, _async_discovery, DISCOVERY_INTERVAL, cancel_on_shutdown=
True
67 """Handle options update."""
68 await hass.config_entries.async_reload(entry.entry_id)
72 """Set up the wiz integration from a config entry."""
73 ip_address = entry.data[CONF_HOST]
74 _LOGGER.debug(
"Get bulb with IP: %s", ip_address)
75 bulb = wizlight(ip_address)
77 scenes = await bulb.getSupportedScenes()
79 except WIZ_CONNECT_EXCEPTIONS
as err:
80 await bulb.async_close()
83 if bulb.mac != entry.unique_id:
89 "Found bulb {bulb.mac} at {ip_address}, expected {entry.unique_id}"
92 async
def _async_update() -> float | None:
93 """Update the WiZ device."""
95 await bulb.updateState()
96 if bulb.power_monitoring
is not False:
97 power: float |
None = await bulb.get_power()
99 except WIZ_EXCEPTIONS
as ex:
100 raise UpdateFailed(f
"Failed to update device at {ip_address}: {ex}")
from ex
109 update_method=_async_update,
113 hass, _LOGGER, cooldown=REQUEST_REFRESH_DELAY, immediate=
False
118 await coordinator.async_config_entry_first_refresh()
119 except ConfigEntryNotReady:
120 await bulb.async_close()
123 async
def _async_shutdown_on_stop(event: Event) ->
None:
124 await bulb.async_close()
126 entry.async_on_unload(
127 hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_shutdown_on_stop)
131 def _async_push_update(state: PilotParser) ->
None:
132 """Receive a push update."""
133 _LOGGER.debug(
"%s: Got push update: %s", bulb.mac, state.pilotResult)
134 coordinator.async_set_updated_data(coordinator.data)
135 if state.get_source() == PIR_SOURCE:
138 await bulb.start_push(_async_push_update)
141 entry.runtime_data =
WizData(coordinator=coordinator, bulb=bulb, scenes=scenes)
142 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
144 entry.async_on_unload(entry.add_update_listener(_async_update_listener))
149 """Unload a config entry."""
150 if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
151 await entry.runtime_data.bulb.async_close()
dict[str, Device] async_discover_devices(HomeAssistant hass)
None async_trigger_discovery(HomeAssistant hass, dict[str, Device] discovered_devices)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
None _async_update_listener(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType hass_config)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
CALLBACK_TYPE async_track_time_interval(HomeAssistant hass, Callable[[datetime], Coroutine[Any, Any, None]|None] action, timedelta interval, *str|None name=None, bool|None cancel_on_shutdown=None)