Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Demo light platform that implements lights."""
2 
3 from __future__ import annotations
4 
5 import random
6 from typing import Any
7 
9  ATTR_BRIGHTNESS,
10  ATTR_COLOR_TEMP,
11  ATTR_EFFECT,
12  ATTR_HS_COLOR,
13  ATTR_RGBW_COLOR,
14  ATTR_RGBWW_COLOR,
15  ATTR_WHITE,
16  ColorMode,
17  LightEntity,
18  LightEntityFeature,
19 )
20 from homeassistant.config_entries import ConfigEntry
21 from homeassistant.core import HomeAssistant
22 from homeassistant.helpers.device_registry import DeviceInfo
23 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24 
25 from . import DOMAIN
26 
27 LIGHT_COLORS = [(56, 86), (345, 75)]
28 
29 LIGHT_EFFECT_LIST = ["rainbow", "none"]
30 
31 LIGHT_TEMPS = [240, 380]
32 
33 SUPPORT_DEMO = {ColorMode.HS, ColorMode.COLOR_TEMP}
34 SUPPORT_DEMO_HS_WHITE = {ColorMode.HS, ColorMode.WHITE}
35 
36 
38  hass: HomeAssistant,
39  config_entry: ConfigEntry,
40  async_add_entities: AddEntitiesCallback,
41 ) -> None:
42  """Set up the demo light platform."""
44  [
45  DemoLight(
46  available=True,
47  effect_list=LIGHT_EFFECT_LIST,
48  effect=LIGHT_EFFECT_LIST[0],
49  device_name="Bed Light",
50  state=False,
51  unique_id="light_1",
52  ),
53  DemoLight(
54  available=True,
55  ct=LIGHT_TEMPS[1],
56  device_name="Ceiling Lights",
57  state=True,
58  unique_id="light_2",
59  ),
60  DemoLight(
61  available=True,
62  hs_color=LIGHT_COLORS[1],
63  device_name="Kitchen Lights",
64  state=True,
65  unique_id="light_3",
66  ),
67  DemoLight(
68  available=True,
69  ct=LIGHT_TEMPS[1],
70  device_name="Office RGBW Lights",
71  rgbw_color=(255, 0, 0, 255),
72  state=True,
73  supported_color_modes={ColorMode.RGBW},
74  unique_id="light_4",
75  ),
76  DemoLight(
77  available=True,
78  device_name="Living Room RGBWW Lights",
79  rgbww_color=(255, 0, 0, 255, 0),
80  state=True,
81  supported_color_modes={ColorMode.RGBWW},
82  unique_id="light_5",
83  ),
84  DemoLight(
85  available=True,
86  device_name="Entrance Color + White Lights",
87  hs_color=LIGHT_COLORS[1],
88  state=True,
89  supported_color_modes=SUPPORT_DEMO_HS_WHITE,
90  unique_id="light_6",
91  ),
92  ]
93  )
94 
95 
97  """Representation of a demo light."""
98 
99  _attr_has_entity_name = True
100  _attr_name = None
101  _attr_should_poll = False
102 
103  def __init__(
104  self,
105  unique_id: str,
106  device_name: str,
107  state: bool,
108  available: bool = False,
109  brightness: int = 180,
110  ct: int | None = None,
111  effect_list: list[str] | None = None,
112  effect: str | None = None,
113  hs_color: tuple[int, int] | None = None,
114  rgbw_color: tuple[int, int, int, int] | None = None,
115  rgbww_color: tuple[int, int, int, int, int] | None = None,
116  supported_color_modes: set[ColorMode] | None = None,
117  ) -> None:
118  """Initialize the light."""
119  self._available_available = True
120  self._brightness_brightness = brightness
121  self._ct_ct = ct or random.choice(LIGHT_TEMPS)
122  self._effect_effect = effect
123  self._effect_list_effect_list = effect_list
124  self._hs_color_hs_color = hs_color
125  self._rgbw_color_rgbw_color = rgbw_color
126  self._rgbww_color_rgbww_color = rgbww_color
127  self._state_state = state
128  self._unique_id_unique_id = unique_id
129  if hs_color:
130  self._color_mode_color_mode = ColorMode.HS
131  elif rgbw_color:
132  self._color_mode_color_mode = ColorMode.RGBW
133  elif rgbww_color:
134  self._color_mode_color_mode = ColorMode.RGBWW
135  else:
136  self._color_mode_color_mode = ColorMode.COLOR_TEMP
137  if not supported_color_modes:
138  supported_color_modes = SUPPORT_DEMO
139  self._color_modes_color_modes = supported_color_modes
140  if self._effect_list_effect_list is not None:
141  self._attr_supported_features |= LightEntityFeature.EFFECT
142  self._attr_device_info_attr_device_info = DeviceInfo(
143  identifiers={
144  # Serial numbers are unique identifiers within a specific domain
145  (DOMAIN, self.unique_idunique_idunique_id)
146  },
147  name=device_name,
148  )
149 
150  @property
151  def unique_id(self) -> str:
152  """Return unique ID for light."""
153  return self._unique_id_unique_id
154 
155  @property
156  def available(self) -> bool:
157  """Return availability."""
158  # This demo light is always available, but well-behaving components
159  # should implement this to inform Home Assistant accordingly.
160  return self._available_available
161 
162  @property
163  def brightness(self) -> int:
164  """Return the brightness of this light between 0..255."""
165  return self._brightness_brightness
166 
167  @property
168  def color_mode(self) -> str | None:
169  """Return the color mode of the light."""
170  return self._color_mode_color_mode
171 
172  @property
173  def hs_color(self) -> tuple[int, int] | None:
174  """Return the hs color value."""
175  return self._hs_color_hs_color
176 
177  @property
178  def rgbw_color(self) -> tuple[int, int, int, int] | None:
179  """Return the rgbw color value."""
180  return self._rgbw_color_rgbw_color
181 
182  @property
183  def rgbww_color(self) -> tuple[int, int, int, int, int] | None:
184  """Return the rgbww color value."""
185  return self._rgbww_color_rgbww_color
186 
187  @property
188  def color_temp(self) -> int:
189  """Return the CT color temperature."""
190  return self._ct_ct
191 
192  @property
193  def effect_list(self) -> list[str] | None:
194  """Return the list of supported effects."""
195  return self._effect_list_effect_list
196 
197  @property
198  def effect(self) -> str | None:
199  """Return the current effect."""
200  return self._effect_effect
201 
202  @property
203  def is_on(self) -> bool:
204  """Return true if light is on."""
205  return self._state_state
206 
207  @property
208  def supported_color_modes(self) -> set[ColorMode]:
209  """Flag supported color modes."""
210  return self._color_modes_color_modes
211 
212  async def async_turn_on(self, **kwargs: Any) -> None:
213  """Turn the light on."""
214  self._state_state = True
215 
216  if ATTR_BRIGHTNESS in kwargs:
217  self._brightness_brightness = kwargs[ATTR_BRIGHTNESS]
218 
219  if ATTR_COLOR_TEMP in kwargs:
220  self._color_mode_color_mode = ColorMode.COLOR_TEMP
221  self._ct_ct = kwargs[ATTR_COLOR_TEMP]
222 
223  if ATTR_EFFECT in kwargs:
224  self._effect_effect = kwargs[ATTR_EFFECT]
225 
226  if ATTR_HS_COLOR in kwargs:
227  self._color_mode_color_mode = ColorMode.HS
228  self._hs_color_hs_color = kwargs[ATTR_HS_COLOR]
229 
230  if ATTR_RGBW_COLOR in kwargs:
231  self._color_mode_color_mode = ColorMode.RGBW
232  self._rgbw_color_rgbw_color = kwargs[ATTR_RGBW_COLOR]
233 
234  if ATTR_RGBWW_COLOR in kwargs:
235  self._color_mode_color_mode = ColorMode.RGBWW
236  self._rgbww_color_rgbww_color = kwargs[ATTR_RGBWW_COLOR]
237 
238  if ATTR_WHITE in kwargs:
239  self._color_mode_color_mode = ColorMode.WHITE
240  self._brightness_brightness = kwargs[ATTR_WHITE]
241 
242  # As we have disabled polling, we need to inform
243  # Home Assistant about updates in our state ourselves.
244  self.async_write_ha_stateasync_write_ha_state()
245 
246  async def async_turn_off(self, **kwargs: Any) -> None:
247  """Turn the light off."""
248  self._state_state = False
249 
250  # As we have disabled polling, we need to inform
251  # Home Assistant about updates in our state ourselves.
252  self.async_write_ha_stateasync_write_ha_state()
tuple[int, int, int, int, int]|None rgbww_color(self)
Definition: light.py:183
tuple[int, int, int, int]|None rgbw_color(self)
Definition: light.py:178
None async_turn_off(self, **Any kwargs)
Definition: light.py:246
None async_turn_on(self, **Any kwargs)
Definition: light.py:212
tuple[int, int]|None hs_color(self)
Definition: light.py:173
set[ColorMode] supported_color_modes(self)
Definition: light.py:208
None __init__(self, str unique_id, str device_name, bool state, bool available=False, int brightness=180, int|None ct=None, list[str]|None effect_list=None, str|None effect=None, tuple[int, int]|None hs_color=None, tuple[int, int, int, int]|None rgbw_color=None, tuple[int, int, int, int, int]|None rgbww_color=None, set[ColorMode]|None supported_color_modes=None)
Definition: light.py:117
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:41