Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Velbus light."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from velbusaio.channels import (
8  Button as VelbusButton,
9  Channel as VelbusChannel,
10  Dimmer as VelbusDimmer,
11 )
12 
14  ATTR_BRIGHTNESS,
15  ATTR_FLASH,
16  ATTR_TRANSITION,
17  FLASH_LONG,
18  FLASH_SHORT,
19  ColorMode,
20  LightEntity,
21  LightEntityFeature,
22 )
23 from homeassistant.config_entries import ConfigEntry
24 from homeassistant.const import EntityCategory
25 from homeassistant.core import HomeAssistant
26 from homeassistant.helpers.entity import Entity
27 from homeassistant.helpers.entity_platform import AddEntitiesCallback
28 
29 from .const import DOMAIN
30 from .entity import VelbusEntity, api_call
31 
32 
34  hass: HomeAssistant,
35  entry: ConfigEntry,
36  async_add_entities: AddEntitiesCallback,
37 ) -> None:
38  """Set up Velbus switch based on config_entry."""
39  await hass.data[DOMAIN][entry.entry_id]["tsk"]
40  cntrl = hass.data[DOMAIN][entry.entry_id]["cntrl"]
41  entities: list[Entity] = [
42  VelbusLight(channel) for channel in cntrl.get_all("light")
43  ]
44  entities.extend(VelbusButtonLight(channel) for channel in cntrl.get_all("led"))
45  async_add_entities(entities)
46 
47 
49  """Representation of a Velbus light."""
50 
51  _channel: VelbusDimmer
52  _attr_color_mode = ColorMode.BRIGHTNESS
53  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
54  _attr_supported_features = LightEntityFeature.TRANSITION
55 
56  @property
57  def is_on(self) -> bool:
58  """Return true if the light is on."""
59  return self._channel_channel.is_on()
60 
61  @property
62  def brightness(self) -> int:
63  """Return the brightness of the light."""
64  return int((self._channel_channel.get_dimmer_state() * 255) / 100)
65 
66  @api_call
67  async def async_turn_on(self, **kwargs: Any) -> None:
68  """Instruct the Velbus light to turn on."""
69  if ATTR_BRIGHTNESS in kwargs:
70  # Make sure a low but non-zero value is not rounded down to zero
71  if kwargs[ATTR_BRIGHTNESS] == 0:
72  brightness = 0
73  else:
74  brightness = max(int((kwargs[ATTR_BRIGHTNESS] * 100) / 255), 1)
75  attr, *args = (
76  "set_dimmer_state",
77  brightness,
78  kwargs.get(ATTR_TRANSITION, 0),
79  )
80  else:
81  attr, *args = (
82  "restore_dimmer_state",
83  kwargs.get(ATTR_TRANSITION, 0),
84  )
85  await getattr(self._channel_channel, attr)(*args)
86 
87  @api_call
88  async def async_turn_off(self, **kwargs: Any) -> None:
89  """Instruct the velbus light to turn off."""
90  attr, *args = (
91  "set_dimmer_state",
92  0,
93  kwargs.get(ATTR_TRANSITION, 0),
94  )
95  await getattr(self._channel_channel, attr)(*args)
96 
97 
99  """Representation of a Velbus light."""
100 
101  _channel: VelbusButton
102  _attr_entity_registry_enabled_default = False
103  _attr_entity_category = EntityCategory.CONFIG
104  _attr_color_mode = ColorMode.ONOFF
105  _attr_supported_color_modes = {ColorMode.ONOFF}
106  _attr_supported_features = LightEntityFeature.FLASH
107 
108  def __init__(self, channel: VelbusChannel) -> None:
109  """Initialize the button light (led)."""
110  super().__init__(channel)
111  self._attr_name_attr_name_attr_name = f"LED {self._channel.get_name()}"
112 
113  @property
114  def is_on(self) -> bool:
115  """Return true if the light is on."""
116  return self._channel_channel.is_on()
117 
118  @api_call
119  async def async_turn_on(self, **kwargs: Any) -> None:
120  """Instruct the Velbus light to turn on."""
121  if ATTR_FLASH in kwargs:
122  if kwargs[ATTR_FLASH] == FLASH_LONG:
123  attr, *args = "set_led_state", "slow"
124  elif kwargs[ATTR_FLASH] == FLASH_SHORT:
125  attr, *args = "set_led_state", "fast"
126  else:
127  attr, *args = "set_led_state", "on"
128  else:
129  attr, *args = "set_led_state", "on"
130  await getattr(self._channel_channel, attr)(*args)
131 
132  @api_call
133  async def async_turn_off(self, **kwargs: Any) -> None:
134  """Instruct the velbus light to turn off."""
135  attr, *args = "set_led_state", "off"
136  await getattr(self._channel_channel, attr)(*args)
None __init__(self, VelbusChannel channel)
Definition: light.py:108
None async_turn_off(self, **Any kwargs)
Definition: light.py:88
None async_turn_on(self, **Any kwargs)
Definition: light.py:67
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:37