Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """BleBox light entities implementation."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 from typing import Any
8 
10 from blebox_uniapi.light import BleboxColorMode
11 
13  ATTR_BRIGHTNESS,
14  ATTR_COLOR_TEMP,
15  ATTR_EFFECT,
16  ATTR_RGB_COLOR,
17  ATTR_RGBW_COLOR,
18  ATTR_RGBWW_COLOR,
19  ColorMode,
20  LightEntity,
21  LightEntityFeature,
22 )
23 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.entity_platform import AddEntitiesCallback
25 
26 from . import BleBoxConfigEntry
27 from .entity import BleBoxEntity
28 
29 _LOGGER = logging.getLogger(__name__)
30 
31 SCAN_INTERVAL = timedelta(seconds=5)
32 
33 
35  hass: HomeAssistant,
36  config_entry: BleBoxConfigEntry,
37  async_add_entities: AddEntitiesCallback,
38 ) -> None:
39  """Set up a BleBox entry."""
40  entities = [
41  BleBoxLightEntity(feature)
42  for feature in config_entry.runtime_data.features.get("lights", [])
43  ]
44  async_add_entities(entities, True)
45 
46 
47 COLOR_MODE_MAP = {
48  BleboxColorMode.RGBW: ColorMode.RGBW,
49  BleboxColorMode.RGB: ColorMode.RGB,
50  BleboxColorMode.MONO: ColorMode.BRIGHTNESS,
51  BleboxColorMode.RGBorW: ColorMode.RGBW, # white hex is prioritised over RGB channel
52  BleboxColorMode.CT: ColorMode.COLOR_TEMP,
53  BleboxColorMode.CTx2: ColorMode.COLOR_TEMP, # two instances
54  BleboxColorMode.RGBWW: ColorMode.RGBWW,
55 }
56 
57 
58 class BleBoxLightEntity(BleBoxEntity[blebox_uniapi.light.Light], LightEntity):
59  """Representation of BleBox lights."""
60 
61  _attr_max_mireds = 370 # 1,000,000 divided by 2700 Kelvin = 370 Mireds
62  _attr_min_mireds = 154 # 1,000,000 divided by 6500 Kelvin = 154 Mireds
63 
64  def __init__(self, feature: blebox_uniapi.light.Light) -> None:
65  """Initialize a BleBox light."""
66  super().__init__(feature)
67  if feature.effect_list:
68  self._attr_supported_features_attr_supported_features = LightEntityFeature.EFFECT
69 
70  @property
71  def is_on(self) -> bool:
72  """Return if light is on."""
73  return self._feature.is_on
74 
75  @property
76  def brightness(self):
77  """Return the name."""
78  return self._feature.brightness
79 
80  @property
81  def color_temp(self):
82  """Return color temperature."""
83  return self._feature.color_temp
84 
85  @property
86  def color_mode(self):
87  """Return the color mode.
88 
89  Set values to _attr_ibutes if needed.
90  """
91  return COLOR_MODE_MAP.get(self._feature.color_mode, ColorMode.ONOFF)
92 
93  @property
95  """Return supported color modes."""
96  return {self.color_modecolor_modecolor_modecolor_mode}
97 
98  @property
99  def effect_list(self) -> list[str]:
100  """Return the list of supported effects."""
101  return self._feature.effect_list
102 
103  @property
104  def effect(self) -> str | None:
105  """Return the current effect."""
106  return self._feature.effect
107 
108  @property
109  def rgb_color(self):
110  """Return value for rgb."""
111  if (rgb_hex := self._feature.rgb_hex) is None:
112  return None
113  return tuple(
114  blebox_uniapi.light.Light.normalise_elements_of_rgb(
115  blebox_uniapi.light.Light.rgb_hex_to_rgb_list(rgb_hex)[0:3]
116  )
117  )
118 
119  @property
120  def rgbw_color(self):
121  """Return the hue and saturation."""
122  if (rgbw_hex := self._feature.rgbw_hex) is None:
123  return None
124  return tuple(blebox_uniapi.light.Light.rgb_hex_to_rgb_list(rgbw_hex)[0:4])
125 
126  @property
127  def rgbww_color(self):
128  """Return value for rgbww."""
129  if (rgbww_hex := self._feature.rgbww_hex) is None:
130  return None
131  return tuple(blebox_uniapi.light.Light.rgb_hex_to_rgb_list(rgbww_hex))
132 
133  async def async_turn_on(self, **kwargs: Any) -> None:
134  """Turn the light on."""
135 
136  rgbw = kwargs.get(ATTR_RGBW_COLOR)
137  brightness = kwargs.get(ATTR_BRIGHTNESS)
138  effect = kwargs.get(ATTR_EFFECT)
139  color_temp = kwargs.get(ATTR_COLOR_TEMP)
140  rgbww = kwargs.get(ATTR_RGBWW_COLOR)
141  feature = self._feature
142  value = feature.sensible_on_value
143  rgb = kwargs.get(ATTR_RGB_COLOR)
144 
145  if rgbw is not None:
146  value = list(rgbw)
147  if color_temp is not None:
148  value = feature.return_color_temp_with_brightness(
149  int(color_temp), self.brightnessbrightnessbrightness
150  )
151 
152  if rgbww is not None:
153  value = list(rgbww)
154 
155  if rgb is not None:
156  if self.color_modecolor_modecolor_modecolor_mode == ColorMode.RGB and brightness is None:
157  brightness = self.brightnessbrightnessbrightness
158  value = list(rgb)
159 
160  if brightness is not None:
161  if self.color_modecolor_modecolor_modecolor_mode == ATTR_COLOR_TEMP:
162  value = feature.return_color_temp_with_brightness(
163  self.color_tempcolor_tempcolor_temp, brightness
164  )
165  else:
166  value = feature.apply_brightness(value, brightness)
167 
168  try:
169  await self._feature.async_on(value)
170  except ValueError as exc:
171  raise ValueError(
172  f"Turning on '{self.name}' failed: Bad value {value}"
173  ) from exc
174 
175  if effect is not None:
176  try:
177  effect_value = self.effect_listeffect_listeffect_list.index(effect)
178  await self._feature.async_api_command("effect", effect_value)
179  except ValueError as exc:
180  raise ValueError(
181  f"Turning on with effect '{self.name}' failed: {effect} not in"
182  " effect list."
183  ) from exc
184 
185  async def async_turn_off(self, **kwargs: Any) -> None:
186  """Turn the light off."""
187  await self._feature.async_off()
None __init__(self, blebox_uniapi.light.Light feature)
Definition: light.py:64
ColorMode|str|None color_mode(self)
Definition: __init__.py:909
None async_setup_entry(HomeAssistant hass, BleBoxConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:38