Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Aqualink pool lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from iaqualink.device import AqualinkLight
8 
10  ATTR_BRIGHTNESS,
11  ATTR_EFFECT,
12  DOMAIN as LIGHT_DOMAIN,
13  ColorMode,
14  LightEntity,
15  LightEntityFeature,
16 )
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import refresh_system
22 from .const import DOMAIN as AQUALINK_DOMAIN
23 from .entity import AqualinkEntity
24 from .utils import await_or_reraise
25 
26 PARALLEL_UPDATES = 0
27 
28 
30  hass: HomeAssistant,
31  config_entry: ConfigEntry,
32  async_add_entities: AddEntitiesCallback,
33 ) -> None:
34  """Set up discovered lights."""
36  (HassAqualinkLight(dev) for dev in hass.data[AQUALINK_DOMAIN][LIGHT_DOMAIN]),
37  True,
38  )
39 
40 
42  """Representation of a light."""
43 
44  def __init__(self, dev: AqualinkLight) -> None:
45  """Initialize AquaLink light."""
46  super().__init__(dev)
47  self._attr_name_attr_name = dev.label
48  if dev.supports_effect:
49  self._attr_effect_list_attr_effect_list = list(dev.supported_effects)
50  self._attr_supported_features_attr_supported_features = LightEntityFeature.EFFECT
51  color_mode = ColorMode.ONOFF
52  if dev.supports_brightness:
53  color_mode = ColorMode.BRIGHTNESS
54  self._attr_color_mode_attr_color_mode = color_mode
55  self._attr_supported_color_modes_attr_supported_color_modes = {color_mode}
56 
57  @property
58  def is_on(self) -> bool:
59  """Return whether the light is on or off."""
60  return self.devdev.is_on
61 
62  @refresh_system
63  async def async_turn_on(self, **kwargs: Any) -> None:
64  """Turn on the light.
65 
66  This handles brightness and light effects for lights that do support
67  them.
68  """
69  # For now I'm assuming lights support either effects or brightness.
70  if effect_name := kwargs.get(ATTR_EFFECT):
71  await await_or_reraise(self.devdev.set_effect_by_name(effect_name))
72  elif brightness := kwargs.get(ATTR_BRIGHTNESS):
73  # Aqualink supports percentages in 25% increments.
74  pct = int(round(brightness * 4.0 / 255)) * 25
75  await await_or_reraise(self.devdev.set_brightness(pct))
76  else:
77  await await_or_reraise(self.devdev.turn_on())
78 
79  @refresh_system
80  async def async_turn_off(self, **kwargs: Any) -> None:
81  """Turn off the light."""
82  await await_or_reraise(self.devdev.turn_off())
83 
84  @property
85  def brightness(self) -> int:
86  """Return current brightness of the light.
87 
88  The scale needs converting between 0-100 and 0-255.
89  """
90  return self.devdev.brightness * 255 / 100
91 
92  @property
93  def effect(self) -> str:
94  """Return the current light effect if supported."""
95  return self.devdev.effect
None turn_off(self, **Any kwargs)
Definition: entity.py:1705
None turn_on(self, **Any kwargs)
Definition: entity.py:1697