Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Elgato lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from elgato import ElgatoError
8 
10  ATTR_BRIGHTNESS,
11  ATTR_COLOR_TEMP,
12  ATTR_HS_COLOR,
13  ColorMode,
14  LightEntity,
15 )
16 from homeassistant.core import HomeAssistant
17 from homeassistant.exceptions import HomeAssistantError
19  AddEntitiesCallback,
20  async_get_current_platform,
21 )
22 
23 from . import ElgatorConfigEntry
24 from .const import SERVICE_IDENTIFY
25 from .coordinator import ElgatoDataUpdateCoordinator
26 from .entity import ElgatoEntity
27 
28 PARALLEL_UPDATES = 1
29 
30 
32  hass: HomeAssistant,
33  entry: ElgatorConfigEntry,
34  async_add_entities: AddEntitiesCallback,
35 ) -> None:
36  """Set up Elgato Light based on a config entry."""
37  coordinator = entry.runtime_data
38  async_add_entities([ElgatoLight(coordinator)])
39 
40  platform = async_get_current_platform()
41  platform.async_register_entity_service(
42  SERVICE_IDENTIFY,
43  None,
44  ElgatoLight.async_identify.__name__,
45  )
46 
47 
49  """Defines an Elgato Light."""
50 
51  _attr_name = None
52  _attr_min_mireds = 143
53  _attr_max_mireds = 344
54 
55  def __init__(self, coordinator: ElgatoDataUpdateCoordinator) -> None:
56  """Initialize Elgato Light."""
57  super().__init__(coordinator)
58  self._attr_supported_color_modes_attr_supported_color_modes = {ColorMode.COLOR_TEMP}
59  self._attr_unique_id_attr_unique_id = coordinator.data.info.serial_number
60 
61  # Elgato Light supporting color, have a different temperature range
62  if (
63  self.coordinator.data.info.product_name
64  in (
65  "Elgato Light Strip",
66  "Elgato Light Strip Pro",
67  )
68  or self.coordinator.data.settings.power_on_hue
69  or self.coordinator.data.state.hue is not None
70  ):
71  self._attr_supported_color_modes_attr_supported_color_modes = {ColorMode.COLOR_TEMP, ColorMode.HS}
72  self._attr_min_mireds_attr_min_mireds_attr_min_mireds = 153
73  self._attr_max_mireds_attr_max_mireds_attr_max_mireds = 285
74 
75  @property
76  def brightness(self) -> int | None:
77  """Return the brightness of this light between 1..255."""
78  return round((self.coordinator.data.state.brightness * 255) / 100)
79 
80  @property
81  def color_temp(self) -> int | None:
82  """Return the CT color value in mireds."""
83  return self.coordinator.data.state.temperature
84 
85  @property
86  def color_mode(self) -> str | None:
87  """Return the color mode of the light."""
88  if self.coordinator.data.state.hue is not None:
89  return ColorMode.HS
90 
91  return ColorMode.COLOR_TEMP
92 
93  @property
94  def hs_color(self) -> tuple[float, float] | None:
95  """Return the hue and saturation color value [float, float]."""
96  return (
97  self.coordinator.data.state.hue or 0,
98  self.coordinator.data.state.saturation or 0,
99  )
100 
101  @property
102  def is_on(self) -> bool:
103  """Return the state of the light."""
104  return self.coordinator.data.state.on
105 
106  async def async_turn_off(self, **kwargs: Any) -> None:
107  """Turn off the light."""
108  try:
109  await self.coordinator.client.light(on=False)
110  except ElgatoError as error:
111  raise HomeAssistantError(
112  "An error occurred while updating the Elgato Light"
113  ) from error
114  finally:
115  await self.coordinator.async_refresh()
116 
117  async def async_turn_on(self, **kwargs: Any) -> None:
118  """Turn on the light."""
119  temperature = kwargs.get(ATTR_COLOR_TEMP)
120 
121  hue = None
122  saturation = None
123  if ATTR_HS_COLOR in kwargs:
124  hue, saturation = kwargs[ATTR_HS_COLOR]
125 
126  brightness = None
127  if ATTR_BRIGHTNESS in kwargs:
128  brightness = round((kwargs[ATTR_BRIGHTNESS] / 255) * 100)
129 
130  # For Elgato lights supporting color mode, but in temperature mode;
131  # adjusting only brightness make them jump back to color mode.
132  # Resending temperature prevents that.
133  if (
134  brightness
135  and ATTR_HS_COLOR not in kwargs
136  and ATTR_COLOR_TEMP not in kwargs
137  and self.supported_color_modessupported_color_modes
138  and ColorMode.HS in self.supported_color_modessupported_color_modes
139  and self.color_modecolor_modecolor_modecolor_mode == ColorMode.COLOR_TEMP
140  ):
141  temperature = self.color_tempcolor_tempcolor_temp
142 
143  try:
144  await self.coordinator.client.light(
145  on=True,
146  brightness=brightness,
147  hue=hue,
148  saturation=saturation,
149  temperature=temperature,
150  )
151  except ElgatoError as error:
152  raise HomeAssistantError(
153  "An error occurred while updating the Elgato Light"
154  ) from error
155  finally:
156  await self.coordinator.async_refresh()
157 
158  async def async_identify(self) -> None:
159  """Identify the light, will make it blink."""
160  try:
161  await self.coordinator.client.identify()
162  except ElgatoError as error:
163  raise HomeAssistantError(
164  "An error occurred while identifying the Elgato Light"
165  ) from error
None __init__(self, ElgatoDataUpdateCoordinator coordinator)
Definition: light.py:55
tuple[float, float]|None hs_color(self)
Definition: light.py:94
None async_turn_off(self, **Any kwargs)
Definition: light.py:106
None async_turn_on(self, **Any kwargs)
Definition: light.py:117
set[ColorMode]|set[str]|None supported_color_modes(self)
Definition: __init__.py:1302
ColorMode|str|None color_mode(self)
Definition: __init__.py:909
None async_setup_entry(HomeAssistant hass, ElgatorConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:35