Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Platform for UPB light integration."""
2 
3 from typing import Any
4 
6  ATTR_BRIGHTNESS,
7  ATTR_FLASH,
8  ATTR_TRANSITION,
9  ColorMode,
10  LightEntity,
11  LightEntityFeature,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers import entity_platform
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .const import DOMAIN, UPB_BLINK_RATE_SCHEMA, UPB_BRIGHTNESS_RATE_SCHEMA
19 from .entity import UpbAttachedEntity
20 
21 SERVICE_LIGHT_FADE_START = "light_fade_start"
22 SERVICE_LIGHT_FADE_STOP = "light_fade_stop"
23 SERVICE_LIGHT_BLINK = "light_blink"
24 
25 
27  hass: HomeAssistant,
28  config_entry: ConfigEntry,
29  async_add_entities: AddEntitiesCallback,
30 ) -> None:
31  """Set up the UPB light based on a config entry."""
32 
33  upb = hass.data[DOMAIN][config_entry.entry_id]["upb"]
34  unique_id = config_entry.entry_id
36  UpbLight(upb.devices[dev], unique_id, upb) for dev in upb.devices
37  )
38 
39  platform = entity_platform.async_get_current_platform()
40 
41  platform.async_register_entity_service(
42  SERVICE_LIGHT_FADE_START, UPB_BRIGHTNESS_RATE_SCHEMA, "async_light_fade_start"
43  )
44  platform.async_register_entity_service(
45  SERVICE_LIGHT_FADE_STOP, None, "async_light_fade_stop"
46  )
47  platform.async_register_entity_service(
48  SERVICE_LIGHT_BLINK, UPB_BLINK_RATE_SCHEMA, "async_light_blink"
49  )
50 
51 
53  """Representation of a UPB Light."""
54 
55  _attr_has_entity_name = True
56  _attr_name = None
57 
58  def __init__(self, element, unique_id, upb):
59  """Initialize an UpbLight."""
60  super().__init__(element, unique_id, upb)
61  self._attr_brightness_attr_brightness: int = self._element_element.status
62 
63  @property
64  def color_mode(self) -> ColorMode:
65  """Return the color mode of the light."""
66  if self._element_element.dimmable:
67  return ColorMode.BRIGHTNESS
68  return ColorMode.ONOFF
69 
70  @property
71  def supported_color_modes(self) -> set[str]:
72  """Flag supported color modes."""
73  return {self.color_modecolor_modecolor_mode}
74 
75  @property
76  def supported_features(self) -> LightEntityFeature:
77  """Flag supported features."""
78  if self._element_element.dimmable:
79  return LightEntityFeature.TRANSITION | LightEntityFeature.FLASH
80  return LightEntityFeature.FLASH
81 
82  @property
83  def is_on(self) -> bool:
84  """Get the current brightness."""
85  return self._attr_brightness_attr_brightness != 0
86 
87  async def async_turn_on(self, **kwargs: Any) -> None:
88  """Turn on the light."""
89  if flash := kwargs.get(ATTR_FLASH):
90  await self.async_light_blinkasync_light_blink(0.5 if flash == "short" else 1.5)
91  else:
92  rate = kwargs.get(ATTR_TRANSITION, -1)
93  brightness = round(kwargs.get(ATTR_BRIGHTNESS, 255) / 2.55)
94  self._element_element.turn_on(brightness, rate)
95 
96  async def async_turn_off(self, **kwargs: Any) -> None:
97  """Turn off the device."""
98  rate = kwargs.get(ATTR_TRANSITION, -1)
99  self._element_element.turn_off(rate)
100 
101  async def async_light_fade_start(self, rate, brightness=None, brightness_pct=None):
102  """Start dimming of device."""
103  if brightness is not None:
104  brightness_pct = round(brightness / 2.55)
105  self._element_element.fade_start(brightness_pct, rate)
106 
107  async def async_light_fade_stop(self):
108  """Stop dimming of device."""
109  self._element_element.fade_stop()
110 
111  async def async_light_blink(self, blink_rate):
112  """Request device to blink."""
113  blink_rate = int(blink_rate * 60) # Convert seconds to 60 hz pulses
114  self._element_element.blink(blink_rate)
115 
116  async def async_update(self) -> None:
117  """Request the device to update its status."""
118  self._element_element.update_status()
119 
120  def _element_changed(self, element, changeset):
121  status = self._element_element.status
122  self._attr_brightness_attr_brightness = round(status * 2.55) if status else 0
ColorMode|str|None color_mode(self)
Definition: __init__.py:909
None async_turn_off(self, **Any kwargs)
Definition: light.py:96
None async_turn_on(self, **Any kwargs)
Definition: light.py:87
def async_light_fade_start(self, rate, brightness=None, brightness_pct=None)
Definition: light.py:101
LightEntityFeature supported_features(self)
Definition: light.py:76
def __init__(self, element, unique_id, upb)
Definition: light.py:58
def async_light_blink(self, blink_rate)
Definition: light.py:111
def _element_changed(self, element, changeset)
Definition: light.py:120
None turn_off(self, **Any kwargs)
Definition: entity.py:1705
None turn_on(self, **Any kwargs)
Definition: entity.py:1697
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:30