Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """WiZ Platform integration."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 from typing import Any
8 
9 from pywizlight import PilotParser, wizlight
10 from pywizlight.bulb import PIR_SOURCE
11 
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import CONF_HOST, EVENT_HOMEASSISTANT_STOP, Platform
14 from homeassistant.core import Event, HomeAssistant, callback
15 from homeassistant.exceptions import ConfigEntryNotReady
16 from homeassistant.helpers import config_validation as cv
17 from homeassistant.helpers.debounce import Debouncer
18 from homeassistant.helpers.dispatcher import async_dispatcher_send
19 from homeassistant.helpers.event import async_track_time_interval
20 from homeassistant.helpers.typing import ConfigType
21 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
22 
23 from .const import (
24  DISCOVER_SCAN_TIMEOUT,
25  DISCOVERY_INTERVAL,
26  DOMAIN,
27  SIGNAL_WIZ_PIR,
28  WIZ_CONNECT_EXCEPTIONS,
29  WIZ_EXCEPTIONS,
30 )
31 from .discovery import async_discover_devices, async_trigger_discovery
32 from .models import WizData
33 
34 type WizConfigEntry = ConfigEntry[WizData]
35 
36 _LOGGER = logging.getLogger(__name__)
37 
38 PLATFORMS = [
39  Platform.BINARY_SENSOR,
40  Platform.LIGHT,
41  Platform.NUMBER,
42  Platform.SENSOR,
43  Platform.SWITCH,
44 ]
45 
46 REQUEST_REFRESH_DELAY = 0.35
47 
48 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
49 
50 
51 async def async_setup(hass: HomeAssistant, hass_config: ConfigType) -> bool:
52  """Set up the wiz integration."""
53 
54  async def _async_discovery(*_: Any) -> None:
56  hass, await async_discover_devices(hass, DISCOVER_SCAN_TIMEOUT)
57  )
58 
59  hass.async_create_background_task(_async_discovery(), "wiz-discovery")
61  hass, _async_discovery, DISCOVERY_INTERVAL, cancel_on_shutdown=True
62  )
63  return True
64 
65 
66 async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
67  """Handle options update."""
68  await hass.config_entries.async_reload(entry.entry_id)
69 
70 
71 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
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)
76  try:
77  scenes = await bulb.getSupportedScenes()
78  await bulb.getMac()
79  except WIZ_CONNECT_EXCEPTIONS as err:
80  await bulb.async_close()
81  raise ConfigEntryNotReady(f"{ip_address}: {err}") from err
82 
83  if bulb.mac != entry.unique_id:
84  # The ip address of the bulb has changed and its likely offline
85  # and another WiZ device has taken the IP. Avoid setting up
86  # since its the wrong device. As soon as the device comes back
87  # online the ip will get updated and setup will proceed.
88  raise ConfigEntryNotReady(
89  "Found bulb {bulb.mac} at {ip_address}, expected {entry.unique_id}"
90  )
91 
92  async def _async_update() -> float | None:
93  """Update the WiZ device."""
94  try:
95  await bulb.updateState()
96  if bulb.power_monitoring is not False:
97  power: float | None = await bulb.get_power()
98  return power
99  except WIZ_EXCEPTIONS as ex:
100  raise UpdateFailed(f"Failed to update device at {ip_address}: {ex}") from ex
101  return None
102 
103  coordinator = DataUpdateCoordinator(
104  hass=hass,
105  logger=_LOGGER,
106  config_entry=entry,
107  name=entry.title,
108  update_interval=timedelta(seconds=15),
109  update_method=_async_update,
110  # We don't want an immediate refresh since the device
111  # takes a moment to reflect the state change
112  request_refresh_debouncer=Debouncer(
113  hass, _LOGGER, cooldown=REQUEST_REFRESH_DELAY, immediate=False
114  ),
115  )
116 
117  try:
118  await coordinator.async_config_entry_first_refresh()
119  except ConfigEntryNotReady:
120  await bulb.async_close()
121  raise
122 
123  async def _async_shutdown_on_stop(event: Event) -> None:
124  await bulb.async_close()
125 
126  entry.async_on_unload(
127  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_shutdown_on_stop)
128  )
129 
130  @callback
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:
136  async_dispatcher_send(hass, SIGNAL_WIZ_PIR.format(bulb.mac))
137 
138  await bulb.start_push(_async_push_update)
139  bulb.set_discovery_callback(lambda bulb: async_trigger_discovery(hass, [bulb]))
140 
141  entry.runtime_data = WizData(coordinator=coordinator, bulb=bulb, scenes=scenes)
142  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
143 
144  entry.async_on_unload(entry.add_update_listener(_async_update_listener))
145  return True
146 
147 
148 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
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()
152  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:71
None _async_update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:66
bool async_setup(HomeAssistant hass, ConfigType hass_config)
Definition: __init__.py:51
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:148
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193
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)
Definition: event.py:1679