2022-02-05 17:56:17 +00:00
|
|
|
"""WiZ integration light platform."""
|
2022-02-05 00:20:21 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-02-05 15:23:19 +00:00
|
|
|
from typing import Any
|
2022-02-05 00:20:21 +00:00
|
|
|
|
2022-02-05 15:23:19 +00:00
|
|
|
from pywizlight import PilotBuilder
|
2022-02-05 21:23:31 +00:00
|
|
|
from pywizlight.bulblibrary import BulbClass, BulbType, Features
|
2022-02-05 00:20:21 +00:00
|
|
|
from pywizlight.scenes import get_id_from_scene_name
|
|
|
|
|
|
|
|
from homeassistant.components.light import (
|
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
ATTR_EFFECT,
|
2022-02-10 15:08:33 +00:00
|
|
|
ATTR_RGBW_COLOR,
|
|
|
|
ATTR_RGBWW_COLOR,
|
2022-04-23 20:59:21 +00:00
|
|
|
ColorMode,
|
2022-02-05 00:20:21 +00:00
|
|
|
LightEntity,
|
2022-04-26 12:33:02 +00:00
|
|
|
LightEntityFeature,
|
2022-02-05 00:20:21 +00:00
|
|
|
)
|
2022-02-05 15:23:19 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-02-05 17:56:17 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-02-05 15:23:19 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-02-05 17:56:17 +00:00
|
|
|
from homeassistant.util.color import (
|
|
|
|
color_temperature_kelvin_to_mired,
|
|
|
|
color_temperature_mired_to_kelvin,
|
|
|
|
)
|
2022-02-05 00:20:21 +00:00
|
|
|
|
2022-02-07 16:44:52 +00:00
|
|
|
from .const import DOMAIN
|
2022-02-05 17:56:17 +00:00
|
|
|
from .entity import WizToggleEntity
|
2022-02-05 15:23:19 +00:00
|
|
|
from .models import WizData
|
2022-02-05 00:20:21 +00:00
|
|
|
|
2022-04-23 20:59:21 +00:00
|
|
|
RGB_WHITE_CHANNELS_COLOR_MODE = {1: ColorMode.RGBW, 2: ColorMode.RGBWW}
|
2022-02-14 15:31:26 +00:00
|
|
|
|
2022-02-10 15:08:33 +00:00
|
|
|
|
|
|
|
def _async_pilot_builder(**kwargs: Any) -> PilotBuilder:
|
|
|
|
"""Create the PilotBuilder for turn on."""
|
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
|
|
|
|
|
|
|
if ATTR_RGBWW_COLOR in kwargs:
|
|
|
|
return PilotBuilder(brightness=brightness, rgbww=kwargs[ATTR_RGBWW_COLOR])
|
|
|
|
|
|
|
|
if ATTR_RGBW_COLOR in kwargs:
|
|
|
|
return PilotBuilder(brightness=brightness, rgbw=kwargs[ATTR_RGBW_COLOR])
|
|
|
|
|
|
|
|
if ATTR_COLOR_TEMP in kwargs:
|
|
|
|
return PilotBuilder(
|
|
|
|
brightness=brightness,
|
|
|
|
colortemp=color_temperature_mired_to_kelvin(kwargs[ATTR_COLOR_TEMP]),
|
|
|
|
)
|
|
|
|
|
|
|
|
if ATTR_EFFECT in kwargs:
|
|
|
|
scene_id = get_id_from_scene_name(kwargs[ATTR_EFFECT])
|
|
|
|
if scene_id == 1000: # rhythm
|
|
|
|
return PilotBuilder()
|
|
|
|
return PilotBuilder(brightness=brightness, scene=scene_id)
|
|
|
|
|
|
|
|
return PilotBuilder(brightness=brightness)
|
2022-02-05 00:20:21 +00:00
|
|
|
|
2022-02-05 15:23:19 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2022-02-05 00:20:21 +00:00
|
|
|
"""Set up the WiZ Platform from config_flow."""
|
2022-02-05 15:23:19 +00:00
|
|
|
wiz_data: WizData = hass.data[DOMAIN][entry.entry_id]
|
2022-02-07 16:44:52 +00:00
|
|
|
if wiz_data.bulb.bulbtype.bulb_type != BulbClass.SOCKET:
|
2022-02-05 17:56:17 +00:00
|
|
|
async_add_entities([WizBulbEntity(wiz_data, entry.title)])
|
2022-02-05 00:20:21 +00:00
|
|
|
|
|
|
|
|
2022-02-05 17:56:17 +00:00
|
|
|
class WizBulbEntity(WizToggleEntity, LightEntity):
|
2022-02-05 00:20:21 +00:00
|
|
|
"""Representation of WiZ Light bulb."""
|
|
|
|
|
2022-02-05 15:23:19 +00:00
|
|
|
def __init__(self, wiz_data: WizData, name: str) -> None:
|
2022-02-05 00:20:21 +00:00
|
|
|
"""Initialize an WiZLight."""
|
2022-02-05 17:56:17 +00:00
|
|
|
super().__init__(wiz_data, name)
|
|
|
|
bulb_type: BulbType = self._device.bulbtype
|
2022-02-05 21:23:31 +00:00
|
|
|
features: Features = bulb_type.features
|
2022-04-23 20:59:21 +00:00
|
|
|
self._attr_supported_color_modes: set[ColorMode | str] = set()
|
2022-02-05 21:23:31 +00:00
|
|
|
if features.color:
|
2022-04-23 20:59:21 +00:00
|
|
|
self._attr_supported_color_modes.add(
|
|
|
|
RGB_WHITE_CHANNELS_COLOR_MODE[bulb_type.white_channels]
|
|
|
|
)
|
2022-02-05 21:23:31 +00:00
|
|
|
if features.color_tmp:
|
2022-04-23 20:59:21 +00:00
|
|
|
self._attr_supported_color_modes.add(ColorMode.COLOR_TEMP)
|
|
|
|
if not self._attr_supported_color_modes and features.brightness:
|
|
|
|
self._attr_supported_color_modes.add(ColorMode.BRIGHTNESS)
|
2022-02-05 15:23:19 +00:00
|
|
|
self._attr_effect_list = wiz_data.scenes
|
2022-02-05 21:23:31 +00:00
|
|
|
if bulb_type.bulb_type != BulbClass.DW:
|
2022-02-14 15:31:26 +00:00
|
|
|
kelvin = bulb_type.kelvin_range
|
|
|
|
self._attr_min_mireds = color_temperature_kelvin_to_mired(kelvin.max)
|
|
|
|
self._attr_max_mireds = color_temperature_kelvin_to_mired(kelvin.min)
|
2022-02-05 17:56:17 +00:00
|
|
|
if bulb_type.features.effect:
|
2022-04-26 12:33:02 +00:00
|
|
|
self._attr_supported_features = LightEntityFeature.EFFECT
|
2022-02-05 21:23:31 +00:00
|
|
|
self._async_update_attrs()
|
2022-02-05 15:23:19 +00:00
|
|
|
|
2022-02-05 17:56:17 +00:00
|
|
|
@callback
|
2022-02-05 21:23:31 +00:00
|
|
|
def _async_update_attrs(self) -> None:
|
|
|
|
"""Handle updating _attr values."""
|
2022-02-05 17:56:17 +00:00
|
|
|
state = self._device.state
|
2022-02-05 15:23:19 +00:00
|
|
|
color_modes = self.supported_color_modes
|
|
|
|
assert color_modes is not None
|
2022-02-05 21:23:31 +00:00
|
|
|
if (brightness := state.get_brightness()) is not None:
|
|
|
|
self._attr_brightness = max(0, min(255, brightness))
|
2022-04-23 20:59:21 +00:00
|
|
|
if ColorMode.COLOR_TEMP in color_modes and (
|
2022-02-11 00:12:36 +00:00
|
|
|
color_temp := state.get_colortemp()
|
2022-02-10 15:08:33 +00:00
|
|
|
):
|
2022-04-23 20:59:21 +00:00
|
|
|
self._attr_color_mode = ColorMode.COLOR_TEMP
|
2022-02-10 15:08:33 +00:00
|
|
|
self._attr_color_temp = color_temperature_kelvin_to_mired(color_temp)
|
2022-02-05 17:56:17 +00:00
|
|
|
elif (
|
2022-04-23 20:59:21 +00:00
|
|
|
ColorMode.RGBWW in color_modes and (rgbww := state.get_rgbww()) is not None
|
2022-02-05 15:23:19 +00:00
|
|
|
):
|
2022-02-10 15:08:33 +00:00
|
|
|
self._attr_rgbww_color = rgbww
|
2022-04-23 20:59:21 +00:00
|
|
|
self._attr_color_mode = ColorMode.RGBWW
|
|
|
|
elif ColorMode.RGBW in color_modes and (rgbw := state.get_rgbw()) is not None:
|
2022-02-10 15:08:33 +00:00
|
|
|
self._attr_rgbw_color = rgbw
|
2022-04-23 20:59:21 +00:00
|
|
|
self._attr_color_mode = ColorMode.RGBW
|
2022-02-05 17:56:17 +00:00
|
|
|
else:
|
2022-04-23 20:59:21 +00:00
|
|
|
self._attr_color_mode = ColorMode.BRIGHTNESS
|
2022-02-05 17:56:17 +00:00
|
|
|
self._attr_effect = state.get_scene()
|
2022-02-05 21:23:31 +00:00
|
|
|
super()._async_update_attrs()
|
2022-02-05 00:20:21 +00:00
|
|
|
|
2022-02-05 17:56:17 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
|
|
"""Instruct the light to turn on."""
|
2022-02-10 15:08:33 +00:00
|
|
|
await self._device.turn_on(_async_pilot_builder(**kwargs))
|
2022-02-05 15:23:19 +00:00
|
|
|
await self.coordinator.async_request_refresh()
|