Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Platform for light integration."""
2 
3 from typing import Any
4 
5 from smarttub import SpaLight
6 
8  ATTR_BRIGHTNESS,
9  ATTR_EFFECT,
10  EFFECT_COLORLOOP,
11  ColorMode,
12  LightEntity,
13  LightEntityFeature,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from .const import (
20  ATTR_LIGHTS,
21  DEFAULT_LIGHT_BRIGHTNESS,
22  DEFAULT_LIGHT_EFFECT,
23  DOMAIN,
24  SMARTTUB_CONTROLLER,
25 )
26 from .entity import SmartTubEntity
27 from .helpers import get_spa_name
28 
29 
31  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
32 ) -> None:
33  """Set up entities for any lights in the tub."""
34 
35  controller = hass.data[DOMAIN][entry.entry_id][SMARTTUB_CONTROLLER]
36 
37  entities = [
38  SmartTubLight(controller.coordinator, light)
39  for spa in controller.spas
40  for light in controller.coordinator.data[spa.id][ATTR_LIGHTS].values()
41  ]
42 
43  async_add_entities(entities)
44 
45 
47  """A light on a spa."""
48 
49  _attr_color_mode = ColorMode.BRIGHTNESS
50  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
51  _attr_supported_features = LightEntityFeature.EFFECT
52 
53  def __init__(self, coordinator, light):
54  """Initialize the entity."""
55  super().__init__(coordinator, light.spa, "light")
56  self.light_zonelight_zone = light.zone
57  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{super().unique_id}-{light.zone}"
58  spa_name = get_spa_name(self.spaspa)
59  self._attr_name_attr_name_attr_name = f"{spa_name} Light {light.zone}"
60 
61  @property
62  def light(self) -> SpaLight:
63  """Return the underlying SpaLight object for this entity."""
64  return self.coordinator.data[self.spaspa.id][ATTR_LIGHTS][self.light_zonelight_zone]
65 
66  @property
67  def brightness(self):
68  """Return the brightness of this light between 0..255."""
69 
70  # SmartTub intensity is 0..100
71  return self._smarttub_to_hass_brightness_smarttub_to_hass_brightness(self.lightlight.intensity)
72 
73  @staticmethod
75  if intensity in (0, 1):
76  return 0
77  return round(intensity * 255 / 100)
78 
79  @staticmethod
81  return round(brightness * 100 / 255)
82 
83  @property
84  def is_on(self):
85  """Return true if the light is on."""
86  return self.lightlight.mode != SpaLight.LightMode.OFF
87 
88  @property
89  def effect(self):
90  """Return the current effect."""
91  mode = self.lightlight.mode.name.lower()
92  if mode in self.effect_listeffect_listeffect_list:
93  return mode
94  return None
95 
96  @property
97  def effect_list(self):
98  """Return the list of supported effects."""
99  return [
100  effect
101  for effect in map(self._light_mode_to_effect_light_mode_to_effect, SpaLight.LightMode)
102  if effect is not None
103  ]
104 
105  @staticmethod
106  def _light_mode_to_effect(light_mode: SpaLight.LightMode):
107  if light_mode == SpaLight.LightMode.OFF:
108  return None
109  if light_mode == SpaLight.LightMode.HIGH_SPEED_COLOR_WHEEL:
110  return EFFECT_COLORLOOP
111 
112  return light_mode.name.lower()
113 
114  @staticmethod
116  if effect == EFFECT_COLORLOOP:
117  return SpaLight.LightMode.HIGH_SPEED_COLOR_WHEEL
118 
119  return SpaLight.LightMode[effect.upper()]
120 
121  async def async_turn_on(self, **kwargs: Any) -> None:
122  """Turn the light on."""
123 
124  mode = self._effect_to_light_mode_effect_to_light_mode(kwargs.get(ATTR_EFFECT, DEFAULT_LIGHT_EFFECT))
125  intensity = self._hass_to_smarttub_brightness_hass_to_smarttub_brightness(
126  kwargs.get(ATTR_BRIGHTNESS, DEFAULT_LIGHT_BRIGHTNESS)
127  )
128 
129  await self.lightlight.set_mode(mode, intensity)
130  await self.coordinator.async_request_refresh()
131 
132  async def async_turn_off(self, **kwargs: Any) -> None:
133  """Turn the light off."""
134  await self.lightlight.set_mode(SpaLight.LightMode.OFF, 0)
135  await self.coordinator.async_request_refresh()
def _light_mode_to_effect(SpaLight.LightMode light_mode)
Definition: light.py:106
def __init__(self, coordinator, light)
Definition: light.py:53
str get_spa_name(smarttub.Spa spa)
Definition: helpers.py:6
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:32