Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Tellstick lights."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
6 from homeassistant.core import HomeAssistant
7 from homeassistant.helpers.entity_platform import AddEntitiesCallback
8 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
9 
10 from .const import (
11  ATTR_DISCOVER_CONFIG,
12  ATTR_DISCOVER_DEVICES,
13  DATA_TELLSTICK,
14  DEFAULT_SIGNAL_REPETITIONS,
15 )
16 from .entity import TellstickDevice
17 
18 
20  hass: HomeAssistant,
21  config: ConfigType,
22  add_entities: AddEntitiesCallback,
23  discovery_info: DiscoveryInfoType | None = None,
24 ) -> None:
25  """Set up the Tellstick lights."""
26  if discovery_info is None or discovery_info[ATTR_DISCOVER_DEVICES] is None:
27  return
28 
29  signal_repetitions = discovery_info.get(
30  ATTR_DISCOVER_CONFIG, DEFAULT_SIGNAL_REPETITIONS
31  )
32 
34  [
35  TellstickLight(hass.data[DATA_TELLSTICK][tellcore_id], signal_repetitions)
36  for tellcore_id in discovery_info[ATTR_DISCOVER_DEVICES]
37  ],
38  True,
39  )
40 
41 
43  """Representation of a Tellstick light."""
44 
45  _attr_color_mode = ColorMode.BRIGHTNESS
46  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
47 
48  def __init__(self, tellcore_device, signal_repetitions):
49  """Initialize the Tellstick light."""
50  super().__init__(tellcore_device, signal_repetitions)
51 
52  self._brightness_brightness = 255
53 
54  @property
55  def brightness(self):
56  """Return the brightness of this light between 0..255."""
57  return self._brightness_brightness
58 
59  def _parse_ha_data(self, kwargs):
60  """Turn the value from HA into something useful."""
61  return kwargs.get(ATTR_BRIGHTNESS)
62 
63  def _parse_tellcore_data(self, tellcore_data):
64  """Turn the value received from tellcore into something useful."""
65  if tellcore_data:
66  return int(tellcore_data) # brightness
67  return None
68 
69  def _update_model(self, new_state, data):
70  """Update the device entity state to match the arguments."""
71  if new_state:
72  if (brightness := data) is not None:
73  self._brightness_brightness = brightness
74 
75  # _brightness is not defined when called from super
76  try:
77  self._state_state_state = self._brightness_brightness > 0
78  except AttributeError:
79  self._state_state_state = True
80  else:
81  self._state_state_state = False
82 
83  def _send_device_command(self, requested_state, requested_data):
84  """Let tellcore update the actual device to the requested state."""
85  if requested_state:
86  if requested_data is not None:
87  self._brightness_brightness = int(requested_data)
88 
89  self._tellcore_device_tellcore_device.dim(self._brightness_brightness)
90  else:
91  self._tellcore_device_tellcore_device.turn_off()
def __init__(self, tellcore_device, signal_repetitions)
Definition: light.py:48
def _send_device_command(self, requested_state, requested_data)
Definition: light.py:83
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:24