Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Tellstick lights using Tellstick Net."""
2 
3 import logging
4 from typing import Any
5 
6 from homeassistant.components import light
7 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.dispatcher import async_dispatcher_connect
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .const import DOMAIN, TELLDUS_DISCOVERY_NEW
14 from .entity import TelldusLiveEntity
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 
20  hass: HomeAssistant,
21  config_entry: ConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Set up tellduslive sensors dynamically."""
25 
26  async def async_discover_light(device_id):
27  """Discover and add a discovered sensor."""
28  client = hass.data[DOMAIN]
29  async_add_entities([TelldusLiveLight(client, device_id)])
30 
32  hass,
33  TELLDUS_DISCOVERY_NEW.format(light.DOMAIN, DOMAIN),
34  async_discover_light,
35  )
36 
37 
39  """Representation of a Tellstick Net light."""
40 
41  _attr_name = None
42  _attr_color_mode = ColorMode.BRIGHTNESS
43  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
44 
45  def __init__(self, client, device_id):
46  """Initialize the Tellstick Net light."""
47  super().__init__(client, device_id)
48  self._last_brightness_last_brightness = self.brightnessbrightnessbrightness
49 
50  def changed(self):
51  """Define a property of the device that might have changed."""
52  self._last_brightness_last_brightness = self.brightnessbrightnessbrightness
53  self.schedule_update_ha_stateschedule_update_ha_state()
54 
55  @property
56  def brightness(self):
57  """Return the brightness of this light between 0..255."""
58  return self.devicedevice.dim_level
59 
60  @property
61  def is_on(self):
62  """Return true if light is on."""
63  return self.devicedevice.is_on
64 
65  def turn_on(self, **kwargs: Any) -> None:
66  """Turn the light on."""
67  brightness = kwargs.get(ATTR_BRIGHTNESS, self._last_brightness_last_brightness)
68  if brightness == 0:
69  fallback_brightness = 100
70  _LOGGER.debug(
71  "Setting brightness to %d%%, because it was 0", fallback_brightness
72  )
73  brightness = int(fallback_brightness * 255 / 100)
74  self.devicedevice.dim(level=brightness)
75  self.changedchanged()
76 
77  def turn_off(self, **kwargs: Any) -> None:
78  """Turn the light off."""
79  self.devicedevice.turn_off()
80  self.changedchanged()
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:23
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103