Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Homematic lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
8  ATTR_BRIGHTNESS,
9  ATTR_COLOR_TEMP,
10  ATTR_EFFECT,
11  ATTR_HS_COLOR,
12  ATTR_TRANSITION,
13  ColorMode,
14  LightEntity,
15  LightEntityFeature,
16 )
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
20 
21 from .const import ATTR_DISCOVER_DEVICES
22 from .entity import HMDevice
23 
24 
26  hass: HomeAssistant,
27  config: ConfigType,
28  add_entities: AddEntitiesCallback,
29  discovery_info: DiscoveryInfoType | None = None,
30 ) -> None:
31  """Set up the Homematic light platform."""
32  if discovery_info is None:
33  return
34 
35  devices = []
36  for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
37  new_device = HMLight(conf)
38  devices.append(new_device)
39 
40  add_entities(devices, True)
41 
42 
44  """Representation of a Homematic light."""
45 
46  @property
47  def brightness(self):
48  """Return the brightness of this light between 0..255."""
49  # Is dimmer?
50  if self._state_state_state == "LEVEL":
51  return int(self._hm_get_state_hm_get_state() * 255)
52  return None
53 
54  @property
55  def is_on(self):
56  """Return true if light is on."""
57  try:
58  return self._hm_get_state_hm_get_state() > 0
59  except TypeError:
60  return False
61 
62  @property
63  def color_mode(self) -> ColorMode:
64  """Return the color mode of the light."""
65  if "COLOR" in self._hmdevice_hmdevice.WRITENODE:
66  return ColorMode.HS
67  if hasattr(self._hmdevice_hmdevice, "get_color_temp"):
68  return ColorMode.COLOR_TEMP
69  return ColorMode.BRIGHTNESS
70 
71  @property
72  def supported_color_modes(self) -> set[ColorMode]:
73  """Flag supported color modes."""
74  color_modes: set[ColorMode] = set()
75 
76  if "COLOR" in self._hmdevice_hmdevice.WRITENODE:
77  color_modes.add(ColorMode.HS)
78  if hasattr(self._hmdevice_hmdevice, "get_color_temp"):
79  color_modes.add(ColorMode.COLOR_TEMP)
80  if not color_modes:
81  color_modes.add(ColorMode.BRIGHTNESS)
82 
83  return color_modes
84 
85  @property
86  def supported_features(self) -> LightEntityFeature:
87  """Flag supported features."""
88  features = LightEntityFeature.TRANSITION
89  if "PROGRAM" in self._hmdevice_hmdevice.WRITENODE:
90  features |= LightEntityFeature.EFFECT
91  return features
92 
93  @property
94  def hs_color(self):
95  """Return the hue and saturation color value [float, float]."""
96  if ColorMode.HS not in self.supported_color_modessupported_color_modessupported_color_modes:
97  return None
98  hue, sat = self._hmdevice_hmdevice.get_hs_color(self._channel_channel)
99  return hue * 360.0, sat * 100.0
100 
101  @property
102  def color_temp(self):
103  """Return the color temp in mireds [int]."""
104  if ColorMode.COLOR_TEMP not in self.supported_color_modessupported_color_modessupported_color_modes:
105  return None
106  hm_color_temp = self._hmdevice_hmdevice.get_color_temp(self._channel_channel)
107  return self.max_miredsmax_mireds - (self.max_miredsmax_mireds - self.min_miredsmin_mireds) * hm_color_temp
108 
109  @property
110  def effect_list(self):
111  """Return the list of supported effects."""
112  if not self.supported_featuressupported_featuressupported_featuressupported_features & LightEntityFeature.EFFECT:
113  return None
114  return self._hmdevice_hmdevice.get_effect_list()
115 
116  @property
117  def effect(self):
118  """Return the current color change program of the light."""
119  if not self.supported_featuressupported_featuressupported_featuressupported_features & LightEntityFeature.EFFECT:
120  return None
121  return self._hmdevice_hmdevice.get_effect()
122 
123  def turn_on(self, **kwargs: Any) -> None:
124  """Turn the light on and/or change color or color effect settings."""
125  if ATTR_TRANSITION in kwargs:
126  self._hmdevice_hmdevice.setValue("RAMP_TIME", kwargs[ATTR_TRANSITION], self._channel_channel)
127 
128  if ATTR_BRIGHTNESS in kwargs and self._state_state_state == "LEVEL":
129  percent_bright = float(kwargs[ATTR_BRIGHTNESS]) / 255
130  self._hmdevice_hmdevice.set_level(percent_bright, self._channel_channel)
131  elif (
132  ATTR_HS_COLOR not in kwargs
133  and ATTR_COLOR_TEMP not in kwargs
134  and ATTR_EFFECT not in kwargs
135  ):
136  self._hmdevice_hmdevice.on(self._channel_channel)
137 
138  if ATTR_HS_COLOR in kwargs:
139  self._hmdevice_hmdevice.set_hs_color(
140  hue=kwargs[ATTR_HS_COLOR][0] / 360.0,
141  saturation=kwargs[ATTR_HS_COLOR][1] / 100.0,
142  channel=self._channel_channel,
143  )
144  if ATTR_COLOR_TEMP in kwargs:
145  hm_temp = (self.max_miredsmax_mireds - kwargs[ATTR_COLOR_TEMP]) / (
146  self.max_miredsmax_mireds - self.min_miredsmin_mireds
147  )
148  self._hmdevice_hmdevice.set_color_temp(hm_temp)
149  if ATTR_EFFECT in kwargs:
150  self._hmdevice_hmdevice.set_effect(kwargs[ATTR_EFFECT])
151 
152  def turn_off(self, **kwargs: Any) -> None:
153  """Turn the light off."""
154  if ATTR_TRANSITION in kwargs:
155  self._hmdevice_hmdevice.setValue("RAMP_TIME", kwargs[ATTR_TRANSITION], self._channel_channel)
156 
157  self._hmdevice_hmdevice.off(self._channel_channel)
158 
159  def _init_data_struct(self):
160  """Generate a data dict (self._data) from the Homematic metadata."""
161  # Use LEVEL
162  self._state_state_state = "LEVEL"
163  self._data[self._state_state_state] = None
164 
165  if ColorMode.HS in self.supported_color_modessupported_color_modessupported_color_modes:
166  self._data.update({"COLOR": None})
167  if self.supported_featuressupported_featuressupported_featuressupported_features & LightEntityFeature.EFFECT:
168  self._data.update({"PROGRAM": None})
set[ColorMode] supported_color_modes(self)
Definition: light.py:72
LightEntityFeature supported_features(self)
Definition: light.py:86
set[ColorMode]|set[str]|None supported_color_modes(self)
Definition: __init__.py:1302
LightEntityFeature supported_features(self)
Definition: __init__.py:1307
int|None supported_features(self)
Definition: entity.py:861
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: light.py:30