Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for EnOcean light sources."""
2 
3 from __future__ import annotations
4 
5 import math
6 from typing import Any
7 
8 from enocean.utils import combine_hex
9 import voluptuous as vol
10 
12  ATTR_BRIGHTNESS,
13  PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA,
14  ColorMode,
15  LightEntity,
16 )
17 from homeassistant.const import CONF_ID, CONF_NAME
18 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
22 
23 from .entity import EnOceanEntity
24 
25 CONF_SENDER_ID = "sender_id"
26 
27 DEFAULT_NAME = "EnOcean Light"
28 
29 PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend(
30  {
31  vol.Optional(CONF_ID, default=[]): vol.All(cv.ensure_list, [vol.Coerce(int)]),
32  vol.Required(CONF_SENDER_ID): vol.All(cv.ensure_list, [vol.Coerce(int)]),
33  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
34  }
35 )
36 
37 
39  hass: HomeAssistant,
40  config: ConfigType,
41  add_entities: AddEntitiesCallback,
42  discovery_info: DiscoveryInfoType | None = None,
43 ) -> None:
44  """Set up the EnOcean light platform."""
45  sender_id: list[int] = config[CONF_SENDER_ID]
46  dev_name: str = config[CONF_NAME]
47  dev_id: list[int] = config[CONF_ID]
48 
49  add_entities([EnOceanLight(sender_id, dev_id, dev_name)])
50 
51 
53  """Representation of an EnOcean light source."""
54 
55  _attr_color_mode = ColorMode.BRIGHTNESS
56  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
57  _attr_brightness = 50
58  _attr_is_on = False
59 
60  def __init__(self, sender_id: list[int], dev_id: list[int], dev_name: str) -> None:
61  """Initialize the EnOcean light source."""
62  super().__init__(dev_id)
63  self._sender_id_sender_id = sender_id
64  self._attr_unique_id_attr_unique_id = str(combine_hex(dev_id))
65  self._attr_name_attr_name = dev_name
66 
67  def turn_on(self, **kwargs: Any) -> None:
68  """Turn the light source on or sets a specific dimmer value."""
69  if (brightness := kwargs.get(ATTR_BRIGHTNESS)) is not None:
70  self._attr_brightness_attr_brightness_attr_brightness = brightness
71 
72  bval = math.floor(self._attr_brightness_attr_brightness_attr_brightness / 256.0 * 100.0)
73  if bval == 0:
74  bval = 1
75  command = [0xA5, 0x02, bval, 0x01, 0x09]
76  command.extend(self._sender_id_sender_id)
77  command.extend([0x00])
78  self.send_commandsend_command(command, [], 0x01)
79  self._attr_is_on_attr_is_on_attr_is_on = True
80 
81  def turn_off(self, **kwargs: Any) -> None:
82  """Turn the light source off."""
83  command = [0xA5, 0x02, 0x00, 0x01, 0x09]
84  command.extend(self._sender_id_sender_id)
85  command.extend([0x00])
86  self.send_commandsend_command(command, [], 0x01)
87  self._attr_is_on_attr_is_on_attr_is_on = False
88 
89  def value_changed(self, packet):
90  """Update the internal state of this device.
91 
92  Dimmer devices like Eltako FUD61 send telegram in different RORGs.
93  We only care about the 4BS (0xA5).
94  """
95  if packet.data[0] == 0xA5 and packet.data[1] == 0x02:
96  val = packet.data[2]
97  self._attr_brightness_attr_brightness_attr_brightness = math.floor(val / 100.0 * 256.0)
98  self._attr_is_on_attr_is_on_attr_is_on = bool(val != 0)
99  self.schedule_update_ha_stateschedule_update_ha_state()
def send_command(self, data, optional, packet_type)
Definition: entity.py:36
None __init__(self, list[int] sender_id, list[int] dev_id, str dev_name)
Definition: light.py:60
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: light.py:43