2022-02-05 00:20:21 +00:00
|
|
|
"""WiZ Platform integration."""
|
2022-02-05 16:36:44 +00:00
|
|
|
import asyncio
|
2022-02-05 15:23:19 +00:00
|
|
|
from datetime import timedelta
|
2022-02-05 00:20:21 +00:00
|
|
|
import logging
|
2022-02-05 16:36:44 +00:00
|
|
|
from typing import Any
|
2022-02-05 00:20:21 +00:00
|
|
|
|
|
|
|
from pywizlight import wizlight
|
2022-02-05 16:36:44 +00:00
|
|
|
from pywizlight.exceptions import WizLightNotKnownBulb
|
2022-02-05 00:20:21 +00:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-02-05 15:23:19 +00:00
|
|
|
from homeassistant.const import CONF_HOST, Platform
|
2022-02-05 00:20:21 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2022-02-05 15:23:19 +00:00
|
|
|
from homeassistant.helpers.debounce import Debouncer
|
2022-02-05 16:36:44 +00:00
|
|
|
from homeassistant.helpers.event import async_track_time_interval
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2022-02-05 15:23:19 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
2022-02-05 00:20:21 +00:00
|
|
|
|
2022-02-05 16:36:44 +00:00
|
|
|
from .const import DISCOVER_SCAN_TIMEOUT, DISCOVERY_INTERVAL, DOMAIN, WIZ_EXCEPTIONS
|
|
|
|
from .discovery import async_discover_devices, async_trigger_discovery
|
2022-02-05 15:23:19 +00:00
|
|
|
from .models import WizData
|
2022-02-05 00:20:21 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2022-02-05 17:56:17 +00:00
|
|
|
PLATFORMS = [Platform.LIGHT, Platform.SWITCH]
|
2022-02-05 15:23:19 +00:00
|
|
|
|
|
|
|
REQUEST_REFRESH_DELAY = 0.35
|
2022-02-05 00:20:21 +00:00
|
|
|
|
|
|
|
|
2022-02-05 16:36:44 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, hass_config: ConfigType) -> bool:
|
|
|
|
"""Set up the wiz integration."""
|
|
|
|
|
|
|
|
async def _async_discovery(*_: Any) -> None:
|
|
|
|
async_trigger_discovery(
|
|
|
|
hass, await async_discover_devices(hass, DISCOVER_SCAN_TIMEOUT)
|
|
|
|
)
|
|
|
|
|
|
|
|
asyncio.create_task(_async_discovery())
|
|
|
|
async_track_time_interval(hass, _async_discovery, DISCOVERY_INTERVAL)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-02-05 00:20:21 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up the wiz integration from a config entry."""
|
2022-02-05 15:23:19 +00:00
|
|
|
ip_address = entry.data[CONF_HOST]
|
2022-02-05 00:20:21 +00:00
|
|
|
_LOGGER.debug("Get bulb with IP: %s", ip_address)
|
2022-02-05 15:23:19 +00:00
|
|
|
bulb = wizlight(ip_address)
|
2022-02-05 00:20:21 +00:00
|
|
|
try:
|
|
|
|
await bulb.getMac()
|
2022-02-05 15:23:19 +00:00
|
|
|
scenes = await bulb.getSupportedScenes()
|
|
|
|
# ValueError gets thrown if the bulb type
|
|
|
|
# cannot be determined on the first try.
|
|
|
|
# This is likely because way the library
|
|
|
|
# processes responses and can be cleaned up
|
|
|
|
# in the future.
|
2022-02-05 16:36:44 +00:00
|
|
|
except WizLightNotKnownBulb:
|
|
|
|
# This is only thrown on IndexError when the
|
|
|
|
# bulb responds with invalid data? It may
|
|
|
|
# not actually be possible anymore
|
|
|
|
_LOGGER.warning("The WiZ bulb type could not be determined for %s", ip_address)
|
|
|
|
return False
|
2022-02-05 15:23:19 +00:00
|
|
|
except (ValueError, *WIZ_EXCEPTIONS) as err:
|
2022-02-05 00:20:21 +00:00
|
|
|
raise ConfigEntryNotReady from err
|
|
|
|
|
2022-02-05 15:23:19 +00:00
|
|
|
async def _async_update() -> None:
|
|
|
|
"""Update the WiZ device."""
|
|
|
|
try:
|
|
|
|
await bulb.updateState()
|
|
|
|
except WIZ_EXCEPTIONS as ex:
|
|
|
|
raise UpdateFailed(f"Failed to update device at {ip_address}: {ex}") from ex
|
|
|
|
|
|
|
|
coordinator = DataUpdateCoordinator(
|
|
|
|
hass=hass,
|
|
|
|
logger=_LOGGER,
|
|
|
|
name=entry.title,
|
|
|
|
update_interval=timedelta(seconds=15),
|
|
|
|
update_method=_async_update,
|
|
|
|
# We don't want an immediate refresh since the device
|
|
|
|
# takes a moment to reflect the state change
|
|
|
|
request_refresh_debouncer=Debouncer(
|
|
|
|
hass, _LOGGER, cooldown=REQUEST_REFRESH_DELAY, immediate=False
|
|
|
|
),
|
|
|
|
)
|
2022-02-07 15:46:00 +00:00
|
|
|
|
|
|
|
await bulb.start_push(lambda _: coordinator.async_set_updated_data(None))
|
2022-02-05 15:23:19 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = WizData(
|
|
|
|
coordinator=coordinator, bulb=bulb, scenes=scenes
|
|
|
|
)
|
2022-02-05 00:20:21 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload a config entry."""
|
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
2022-02-07 15:46:00 +00:00
|
|
|
data: WizData = hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
await data.bulb.async_close()
|
2022-02-05 00:20:21 +00:00
|
|
|
return unload_ok
|