Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """WiZ integration light platform."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from pywizlight import PilotBuilder
8 from pywizlight.bulblibrary import BulbClass, BulbType, Features
9 from pywizlight.scenes import get_id_from_scene_name
10 
12  ATTR_BRIGHTNESS,
13  ATTR_COLOR_TEMP,
14  ATTR_EFFECT,
15  ATTR_RGBW_COLOR,
16  ATTR_RGBWW_COLOR,
17  ColorMode,
18  LightEntity,
19  LightEntityFeature,
20  filter_supported_color_modes,
21 )
22 from homeassistant.core import HomeAssistant, callback
23 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24 from homeassistant.util.color import (
25  color_temperature_kelvin_to_mired,
26  color_temperature_mired_to_kelvin,
27 )
28 
29 from . import WizConfigEntry
30 from .entity import WizToggleEntity
31 from .models import WizData
32 
33 RGB_WHITE_CHANNELS_COLOR_MODE = {1: ColorMode.RGBW, 2: ColorMode.RGBWW}
34 
35 
36 def _async_pilot_builder(**kwargs: Any) -> PilotBuilder:
37  """Create the PilotBuilder for turn on."""
38  brightness = kwargs.get(ATTR_BRIGHTNESS)
39 
40  if ATTR_RGBWW_COLOR in kwargs:
41  return PilotBuilder(brightness=brightness, rgbww=kwargs[ATTR_RGBWW_COLOR])
42 
43  if ATTR_RGBW_COLOR in kwargs:
44  return PilotBuilder(brightness=brightness, rgbw=kwargs[ATTR_RGBW_COLOR])
45 
46  if ATTR_COLOR_TEMP in kwargs:
47  return PilotBuilder(
48  brightness=brightness,
49  colortemp=color_temperature_mired_to_kelvin(kwargs[ATTR_COLOR_TEMP]),
50  )
51 
52  if ATTR_EFFECT in kwargs:
53  scene_id = get_id_from_scene_name(kwargs[ATTR_EFFECT])
54  if scene_id == 1000: # rhythm
55  return PilotBuilder()
56  return PilotBuilder(brightness=brightness, scene=scene_id)
57 
58  return PilotBuilder(brightness=brightness)
59 
60 
62  hass: HomeAssistant,
63  entry: WizConfigEntry,
64  async_add_entities: AddEntitiesCallback,
65 ) -> None:
66  """Set up the WiZ Platform from config_flow."""
67  if entry.runtime_data.bulb.bulbtype.bulb_type != BulbClass.SOCKET:
68  async_add_entities([WizBulbEntity(entry.runtime_data, entry.title)])
69 
70 
72  """Representation of WiZ Light bulb."""
73 
74  _attr_name = None
75  _fixed_color_mode: ColorMode | None = None
76 
77  def __init__(self, wiz_data: WizData, name: str) -> None:
78  """Initialize an WiZLight."""
79  super().__init__(wiz_data, name)
80  bulb_type: BulbType = self._device_device.bulbtype
81  features: Features = bulb_type.features
82  color_modes = {ColorMode.ONOFF}
83  if features.color:
84  color_modes.add(RGB_WHITE_CHANNELS_COLOR_MODE[bulb_type.white_channels])
85  if features.color_tmp:
86  color_modes.add(ColorMode.COLOR_TEMP)
87  if features.brightness:
88  color_modes.add(ColorMode.BRIGHTNESS)
89  self._attr_supported_color_modes_attr_supported_color_modes = filter_supported_color_modes(color_modes)
90  if len(self._attr_supported_color_modes_attr_supported_color_modes) == 1:
91  # If the light supports only a single color mode, set it now
92  self._attr_color_mode_attr_color_mode = next(iter(self._attr_supported_color_modes_attr_supported_color_modes))
93  self._attr_effect_list_attr_effect_list = wiz_data.scenes
94  if bulb_type.bulb_type != BulbClass.DW:
95  kelvin = bulb_type.kelvin_range
96  self._attr_min_mireds_attr_min_mireds = color_temperature_kelvin_to_mired(kelvin.max)
97  self._attr_max_mireds_attr_max_mireds = color_temperature_kelvin_to_mired(kelvin.min)
98  if bulb_type.features.effect:
99  self._attr_supported_features_attr_supported_features = LightEntityFeature.EFFECT
101 
102  @callback
103  def _async_update_attrs(self) -> None:
104  """Handle updating _attr values."""
105  state = self._device_device.state
106  color_modes = self.supported_color_modessupported_color_modes
107  assert color_modes is not None
108  if (brightness := state.get_brightness()) is not None:
109  self._attr_brightness_attr_brightness = max(0, min(255, brightness))
110  if ColorMode.COLOR_TEMP in color_modes and (
111  color_temp := state.get_colortemp()
112  ):
113  self._attr_color_mode_attr_color_mode = ColorMode.COLOR_TEMP
114  self._attr_color_temp_attr_color_temp = color_temperature_kelvin_to_mired(color_temp)
115  elif (
116  ColorMode.RGBWW in color_modes and (rgbww := state.get_rgbww()) is not None
117  ):
118  self._attr_rgbww_color_attr_rgbww_color = rgbww
119  self._attr_color_mode_attr_color_mode = ColorMode.RGBWW
120  elif ColorMode.RGBW in color_modes and (rgbw := state.get_rgbw()) is not None:
121  self._attr_rgbw_color_attr_rgbw_color = rgbw
122  self._attr_color_mode_attr_color_mode = ColorMode.RGBW
123  self._attr_effect_attr_effect = state.get_scene()
124  super()._async_update_attrs()
125 
126  async def async_turn_on(self, **kwargs: Any) -> None:
127  """Instruct the light to turn on."""
128  await self._device_device.turn_on(_async_pilot_builder(**kwargs))
129  await self.coordinator.async_request_refresh()
set[ColorMode]|set[str]|None supported_color_modes(self)
Definition: __init__.py:1302
None __init__(self, WizData wiz_data, str name)
Definition: light.py:77
None async_turn_on(self, **Any kwargs)
Definition: light.py:126
None turn_on(self, **Any kwargs)
Definition: entity.py:1697
set[ColorMode] filter_supported_color_modes(Iterable[ColorMode] color_modes)
Definition: __init__.py:122
None async_setup_entry(HomeAssistant hass, WizConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:65
PilotBuilder _async_pilot_builder(**Any kwargs)
Definition: light.py:36
int color_temperature_mired_to_kelvin(float mired_temperature)
Definition: color.py:631
int color_temperature_kelvin_to_mired(float kelvin_temperature)
Definition: color.py:636