1 """Lights on Zigbee Home Automation networks."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
10 from zha.application.platforms.light.const
import (
11 ColorMode
as ZhaColorMode,
12 LightEntityFeature
as ZhaLightEntityFeature,
33 from .entity
import ZHAEntity
34 from .helpers
import (
37 async_add_entities
as zha_async_add_entities,
38 convert_zha_error_to_ha_error,
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,
50 HA_TO_ZHA_COLOR_MODE = {v: k
for k, v
in ZHA_TO_HA_COLOR_MODE.items()}
52 OFF_BRIGHTNESS =
"off_brightness"
53 OFF_WITH_TRANSITION =
"off_with_transition"
55 _LOGGER = logging.getLogger(__name__)
60 config_entry: ConfigEntry,
61 async_add_entities: AddEntitiesCallback,
63 """Set up the Zigbee Home Automation light from config entry."""
65 entities_to_create = zha_data.platforms[Platform.LIGHT]
71 zha_async_add_entities, async_add_entities, Light, entities_to_create
74 config_entry.async_on_unload(unsub)
78 """Representation of a ZHA or ZLL light."""
80 def __init__(self, entity_data: EntityData) ->
None:
81 """Initialize the ZHA light."""
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:
88 if color_mode
not in (ZhaColorMode.BRIGHTNESS, ZhaColorMode.ONOFF):
89 color_modes.add(ZHA_TO_HA_COLOR_MODE[color_mode])
93 color_modes.add(ColorMode.BRIGHTNESS)
96 color_modes.add(ColorMode.ONOFF)
100 zha_features: ZhaLightEntityFeature = self.entity_data.entity.supported_features
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
113 """Return entity specific state attributes."""
114 state = self.entity_data.entity.state
116 "off_with_transition": state.get(
"off_with_transition"),
117 "off_brightness": state.get(
"off_brightness"),
122 """Return true if entity is on."""
123 return self.entity_data.entity.is_on
127 """Return the brightness of this light."""
128 return self.entity_data.entity.brightness
132 """Return the coldest color_temp that this light supports."""
133 return self.entity_data.entity.min_mireds
137 """Return the warmest color_temp that this light supports."""
138 return self.entity_data.entity.max_mireds
142 """Return the xy color value [float, float]."""
143 return self.entity_data.entity.xy_color
147 """Return the CT color value in mireds."""
148 return self.entity_data.entity.color_temp
152 """Return the color mode."""
153 if self.entity_data.entity.color_mode
is None:
155 return ZHA_TO_HA_COLOR_MODE[self.entity_data.entity.color_mode]
159 """Return the list of supported effects."""
160 return self.entity_data.entity.effect_list
164 """Return the current effect."""
165 return self.entity_data.entity.effect
167 @convert_zha_error_to_ha_error
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),
180 @convert_zha_error_to_ha_error
182 """Turn the entity off."""
183 await self.entity_data.entity.async_turn_off(
184 transition=kwargs.get(ATTR_TRANSITION)
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),
199 HA_TO_ZHA_COLOR_MODE[
ColorMode(state.attributes[ATTR_COLOR_MODE])]
200 if state.attributes.get(ATTR_COLOR_MODE)
is not None
203 effect=state.attributes.get(ATTR_EFFECT),
None async_turn_on(self, **Any kwargs)
ColorMode|None color_mode(self)
None async_turn_off(self, **Any kwargs)
tuple[float, float]|None xy_color(self)
int|None color_temp(self)
Mapping[str, Any]|None extra_state_attributes(self)
None __init__(self, EntityData entity_data)
list[str]|None effect_list(self)
None restore_external_state_attributes(self, State state)
_attr_supported_color_modes
None async_write_ha_state(self)
HAZHAData get_zha_data(HomeAssistant hass)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)