Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Duotecno lights."""
2 
3 from typing import Any
4 
5 from duotecno.controller import PyDuotecno
6 from duotecno.unit import DimUnit
7 
8 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .const import DOMAIN
14 from .entity import DuotecnoEntity, api_call
15 
16 
18  hass: HomeAssistant,
19  entry: ConfigEntry,
20  async_add_entities: AddEntitiesCallback,
21 ) -> None:
22  """Set up Duotecno light based on config_entry."""
23  cntrl: PyDuotecno = hass.data[DOMAIN][entry.entry_id]
24  async_add_entities(DuotecnoLight(channel) for channel in cntrl.get_units("DimUnit"))
25 
26 
28  """Representation of a light."""
29 
30  _unit: DimUnit
31  _attr_color_mode = ColorMode.BRIGHTNESS
32  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
33 
34  @property
35  def is_on(self) -> bool:
36  """Return true if the light is on."""
37  return self._unit_unit.is_on()
38 
39  @property
40  def brightness(self) -> int:
41  """Return the brightness of the light."""
42  return int((self._unit_unit.get_dimmer_state() * 255) / 100)
43 
44  @api_call
45  async def async_turn_on(self, **kwargs: Any) -> None:
46  """Instruct the light to turn on."""
47  if (val := kwargs.get(ATTR_BRIGHTNESS)) is not None:
48  # set to a value
49  val = max(int((val * 100) / 255), 1)
50  else:
51  # restore state
52  val = None
53  await self._unit_unit.set_dimmer_state(val)
54 
55  @api_call
56  async def async_turn_off(self, **kwargs: Any) -> None:
57  """Instruct the light to turn off."""
58  await self._unit_unit.set_dimmer_state(0)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:21