Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Lights on Zigbee Home Automation networks."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import functools
7 import logging
8 from typing import Any
9 
10 from zha.application.platforms.light.const import (
11  ColorMode as ZhaColorMode,
12  LightEntityFeature as ZhaLightEntityFeature,
13 )
14 
16  ATTR_BRIGHTNESS,
17  ATTR_COLOR_MODE,
18  ATTR_COLOR_TEMP,
19  ATTR_EFFECT,
20  ATTR_FLASH,
21  ATTR_TRANSITION,
22  ATTR_XY_COLOR,
23  ColorMode,
24  LightEntity,
25  LightEntityFeature,
26 )
27 from homeassistant.config_entries import ConfigEntry
28 from homeassistant.const import STATE_ON, Platform
29 from homeassistant.core import HomeAssistant, State, callback
30 from homeassistant.helpers.dispatcher import async_dispatcher_connect
31 from homeassistant.helpers.entity_platform import AddEntitiesCallback
32 
33 from .entity import ZHAEntity
34 from .helpers import (
35  SIGNAL_ADD_ENTITIES,
36  EntityData,
37  async_add_entities as zha_async_add_entities,
38  convert_zha_error_to_ha_error,
39  get_zha_data,
40 )
41 
42 ZHA_TO_HA_COLOR_MODE = {
43  ZhaColorMode.UNKNOWN: ColorMode.UNKNOWN,
44  ZhaColorMode.ONOFF: ColorMode.ONOFF,
45  ZhaColorMode.BRIGHTNESS: ColorMode.BRIGHTNESS,
46  ZhaColorMode.COLOR_TEMP: ColorMode.COLOR_TEMP,
47  ZhaColorMode.XY: ColorMode.XY,
48 }
49 
50 HA_TO_ZHA_COLOR_MODE = {v: k for k, v in ZHA_TO_HA_COLOR_MODE.items()}
51 
52 OFF_BRIGHTNESS = "off_brightness"
53 OFF_WITH_TRANSITION = "off_with_transition"
54 
55 _LOGGER = logging.getLogger(__name__)
56 
57 
59  hass: HomeAssistant,
60  config_entry: ConfigEntry,
61  async_add_entities: AddEntitiesCallback,
62 ) -> None:
63  """Set up the Zigbee Home Automation light from config entry."""
64  zha_data = get_zha_data(hass)
65  entities_to_create = zha_data.platforms[Platform.LIGHT]
66 
68  hass,
69  SIGNAL_ADD_ENTITIES,
70  functools.partial(
71  zha_async_add_entities, async_add_entities, Light, entities_to_create
72  ),
73  )
74  config_entry.async_on_unload(unsub)
75 
76 
78  """Representation of a ZHA or ZLL light."""
79 
80  def __init__(self, entity_data: EntityData) -> None:
81  """Initialize the ZHA light."""
82  super().__init__(entity_data)
83  color_modes: set[ColorMode] = set()
84  has_brightness = False
85  for color_mode in self.entity_data.entity.supported_color_modes:
86  if color_mode == ZhaColorMode.BRIGHTNESS:
87  has_brightness = True
88  if color_mode not in (ZhaColorMode.BRIGHTNESS, ZhaColorMode.ONOFF):
89  color_modes.add(ZHA_TO_HA_COLOR_MODE[color_mode])
90  if color_modes:
91  self._attr_supported_color_modes_attr_supported_color_modes = color_modes
92  elif has_brightness:
93  color_modes.add(ColorMode.BRIGHTNESS)
94  self._attr_supported_color_modes_attr_supported_color_modes = color_modes
95  else:
96  color_modes.add(ColorMode.ONOFF)
97  self._attr_supported_color_modes_attr_supported_color_modes = color_modes
98 
99  features = LightEntityFeature(0)
100  zha_features: ZhaLightEntityFeature = self.entity_data.entity.supported_features
101 
102  if ZhaLightEntityFeature.EFFECT in zha_features:
103  features |= LightEntityFeature.EFFECT
104  if ZhaLightEntityFeature.FLASH in zha_features:
105  features |= LightEntityFeature.FLASH
106  if ZhaLightEntityFeature.TRANSITION in zha_features:
107  features |= LightEntityFeature.TRANSITION
108 
109  self._attr_supported_features_attr_supported_features = features
110 
111  @property
112  def extra_state_attributes(self) -> Mapping[str, Any] | None:
113  """Return entity specific state attributes."""
114  state = self.entity_data.entity.state
115  return {
116  "off_with_transition": state.get("off_with_transition"),
117  "off_brightness": state.get("off_brightness"),
118  }
119 
120  @property
121  def is_on(self) -> bool:
122  """Return true if entity is on."""
123  return self.entity_data.entity.is_on
124 
125  @property
126  def brightness(self) -> int:
127  """Return the brightness of this light."""
128  return self.entity_data.entity.brightness
129 
130  @property
131  def min_mireds(self) -> int:
132  """Return the coldest color_temp that this light supports."""
133  return self.entity_data.entity.min_mireds
134 
135  @property
136  def max_mireds(self) -> int:
137  """Return the warmest color_temp that this light supports."""
138  return self.entity_data.entity.max_mireds
139 
140  @property
141  def xy_color(self) -> tuple[float, float] | None:
142  """Return the xy color value [float, float]."""
143  return self.entity_data.entity.xy_color
144 
145  @property
146  def color_temp(self) -> int | None:
147  """Return the CT color value in mireds."""
148  return self.entity_data.entity.color_temp
149 
150  @property
151  def color_mode(self) -> ColorMode | None:
152  """Return the color mode."""
153  if self.entity_data.entity.color_mode is None:
154  return None
155  return ZHA_TO_HA_COLOR_MODE[self.entity_data.entity.color_mode]
156 
157  @property
158  def effect_list(self) -> list[str] | None:
159  """Return the list of supported effects."""
160  return self.entity_data.entity.effect_list
161 
162  @property
163  def effect(self) -> str | None:
164  """Return the current effect."""
165  return self.entity_data.entity.effect
166 
167  @convert_zha_error_to_ha_error
168  async def async_turn_on(self, **kwargs: Any) -> None:
169  """Turn the entity on."""
170  await self.entity_data.entity.async_turn_on(
171  transition=kwargs.get(ATTR_TRANSITION),
172  brightness=kwargs.get(ATTR_BRIGHTNESS),
173  effect=kwargs.get(ATTR_EFFECT),
174  flash=kwargs.get(ATTR_FLASH),
175  color_temp=kwargs.get(ATTR_COLOR_TEMP),
176  xy_color=kwargs.get(ATTR_XY_COLOR),
177  )
178  self.async_write_ha_stateasync_write_ha_state()
179 
180  @convert_zha_error_to_ha_error
181  async def async_turn_off(self, **kwargs: Any) -> None:
182  """Turn the entity off."""
183  await self.entity_data.entity.async_turn_off(
184  transition=kwargs.get(ATTR_TRANSITION)
185  )
186  self.async_write_ha_stateasync_write_ha_state()
187 
188  @callback
189  def restore_external_state_attributes(self, state: State) -> None:
190  """Restore entity state."""
191  self.entity_data.entity.restore_external_state_attributes(
192  state=(state.state == STATE_ON),
193  off_with_transition=state.attributes.get(OFF_WITH_TRANSITION),
194  off_brightness=state.attributes.get(OFF_BRIGHTNESS),
195  brightness=state.attributes.get(ATTR_BRIGHTNESS),
196  color_temp=state.attributes.get(ATTR_COLOR_TEMP),
197  xy_color=state.attributes.get(ATTR_XY_COLOR),
198  color_mode=(
199  HA_TO_ZHA_COLOR_MODE[ColorMode(state.attributes[ATTR_COLOR_MODE])]
200  if state.attributes.get(ATTR_COLOR_MODE) is not None
201  else None
202  ),
203  effect=state.attributes.get(ATTR_EFFECT),
204  )
None async_turn_on(self, **Any kwargs)
Definition: light.py:168
ColorMode|None color_mode(self)
Definition: light.py:151
None async_turn_off(self, **Any kwargs)
Definition: light.py:181
tuple[float, float]|None xy_color(self)
Definition: light.py:141
Mapping[str, Any]|None extra_state_attributes(self)
Definition: light.py:112
None __init__(self, EntityData entity_data)
Definition: light.py:80
list[str]|None effect_list(self)
Definition: light.py:158
None restore_external_state_attributes(self, State state)
Definition: light.py:189
HAZHAData get_zha_data(HomeAssistant hass)
Definition: helpers.py:1020
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:62
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103