Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for the Elgato Avea lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import avea
8 
10  ATTR_BRIGHTNESS,
11  ATTR_HS_COLOR,
12  ColorMode,
13  LightEntity,
14 )
15 from homeassistant.core import HomeAssistant
16 from homeassistant.exceptions import PlatformNotReady
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
19 import homeassistant.util.color as color_util
20 
21 
23  hass: HomeAssistant,
24  config: ConfigType,
25  add_entities: AddEntitiesCallback,
26  discovery_info: DiscoveryInfoType | None = None,
27 ) -> None:
28  """Set up the Avea platform."""
29  try:
30  nearby_bulbs = avea.discover_avea_bulbs()
31  for bulb in nearby_bulbs:
32  bulb.get_name()
33  bulb.get_brightness()
34  except OSError as err:
35  raise PlatformNotReady from err
36 
37  add_entities(AveaLight(bulb) for bulb in nearby_bulbs)
38 
39 
41  """Representation of an Avea."""
42 
43  _attr_color_mode = ColorMode.HS
44  _attr_supported_color_modes = {ColorMode.HS}
45 
46  def __init__(self, light):
47  """Initialize an AveaLight."""
48  self._light_light = light
49  self._attr_name_attr_name = light.name
50  self._attr_brightness_attr_brightness = light.brightness
51 
52  def turn_on(self, **kwargs: Any) -> None:
53  """Instruct the light to turn on."""
54  if not kwargs:
55  self._light_light.set_brightness(4095)
56  else:
57  if ATTR_BRIGHTNESS in kwargs:
58  bright = round((kwargs[ATTR_BRIGHTNESS] / 255) * 4095)
59  self._light_light.set_brightness(bright)
60  if ATTR_HS_COLOR in kwargs:
61  rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
62  self._light_light.set_rgb(rgb[0], rgb[1], rgb[2])
63 
64  def turn_off(self, **kwargs: Any) -> None:
65  """Instruct the light to turn off."""
66  self._light_light.set_brightness(0)
67 
68  def update(self) -> None:
69  """Fetch new state data for this light.
70 
71  This is the only method that should fetch new data for Home Assistant.
72  """
73  if (brightness := self._light_light.get_brightness()) is not None:
74  self._attr_is_on_attr_is_on = brightness != 0
75  self._attr_brightness_attr_brightness = round(255 * (brightness / 4095))
None turn_on(self, **Any kwargs)
Definition: light.py:52
None turn_off(self, **Any kwargs)
Definition: light.py:64
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:27