Home Assistant Unofficial Reference 2024.12.1
scene.py
Go to the documentation of this file.
1 """Platform for UPB link integration."""
2 
3 from typing import Any
4 
5 from homeassistant.components.scene import Scene
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers import entity_platform
9 from homeassistant.helpers.entity_platform import AddEntitiesCallback
10 
11 from .const import DOMAIN, UPB_BLINK_RATE_SCHEMA, UPB_BRIGHTNESS_RATE_SCHEMA
12 from .entity import UpbEntity
13 
14 SERVICE_LINK_DEACTIVATE = "link_deactivate"
15 SERVICE_LINK_FADE_STOP = "link_fade_stop"
16 SERVICE_LINK_GOTO = "link_goto"
17 SERVICE_LINK_FADE_START = "link_fade_start"
18 SERVICE_LINK_BLINK = "link_blink"
19 
20 
22  hass: HomeAssistant,
23  config_entry: ConfigEntry,
24  async_add_entities: AddEntitiesCallback,
25 ) -> None:
26  """Set up the UPB link based on a config entry."""
27  upb = hass.data[DOMAIN][config_entry.entry_id]["upb"]
28  unique_id = config_entry.entry_id
29  async_add_entities(UpbLink(upb.links[link], unique_id, upb) for link in upb.links)
30 
31  platform = entity_platform.async_get_current_platform()
32 
33  platform.async_register_entity_service(
34  SERVICE_LINK_DEACTIVATE, None, "async_link_deactivate"
35  )
36  platform.async_register_entity_service(
37  SERVICE_LINK_FADE_STOP, None, "async_link_fade_stop"
38  )
39  platform.async_register_entity_service(
40  SERVICE_LINK_GOTO, UPB_BRIGHTNESS_RATE_SCHEMA, "async_link_goto"
41  )
42  platform.async_register_entity_service(
43  SERVICE_LINK_FADE_START, UPB_BRIGHTNESS_RATE_SCHEMA, "async_link_fade_start"
44  )
45  platform.async_register_entity_service(
46  SERVICE_LINK_BLINK, UPB_BLINK_RATE_SCHEMA, "async_link_blink"
47  )
48 
49 
51  """Representation of a UPB Link."""
52 
53  def __init__(self, element, unique_id, upb):
54  """Initialize the base of all UPB devices."""
55  super().__init__(element, unique_id, upb)
56  self._attr_name_attr_name = element.name
57 
58  async def async_activate(self, **kwargs: Any) -> None:
59  """Activate the task."""
60  self._element_element.activate()
61 
62  async def async_link_deactivate(self):
63  """Activate the task."""
64  self._element_element.deactivate()
65 
66  async def async_link_goto(self, rate, brightness=None, brightness_pct=None):
67  """Activate the task."""
68  if brightness is not None:
69  brightness_pct = round(brightness / 2.55)
70  self._element_element.goto(brightness_pct, rate)
71 
72  async def async_link_fade_start(self, rate, brightness=None, brightness_pct=None):
73  """Start dimming a link."""
74  if brightness is not None:
75  brightness_pct = round(brightness / 2.55)
76  self._element_element.fade_start(brightness_pct, rate)
77 
78  async def async_link_fade_stop(self):
79  """Stop dimming a link."""
80  self._element_element.fade_stop()
81 
82  async def async_link_blink(self, blink_rate):
83  """Blink a link."""
84  blink_rate = int(blink_rate * 60)
85  self._element_element.blink(blink_rate)
None activate(self, **Any kwargs)
Definition: __init__.py:131
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: scene.py:25