Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Platform for light integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from devolo_home_control_api.devices.zwave import Zwave
8 from devolo_home_control_api.homecontrol import HomeControl
9 
10 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import DevoloHomeControlConfigEntry
15 from .devolo_multi_level_switch import DevoloMultiLevelSwitchDeviceEntity
16 
17 
19  hass: HomeAssistant,
20  entry: DevoloHomeControlConfigEntry,
21  async_add_entities: AddEntitiesCallback,
22 ) -> None:
23  """Get all light devices and setup them via config entry."""
24 
27  homecontrol=gateway,
28  device_instance=device,
29  element_uid=multi_level_switch.element_uid,
30  )
31  for gateway in entry.runtime_data
32  for device in gateway.multi_level_switch_devices
33  for multi_level_switch in device.multi_level_switch_property.values()
34  if multi_level_switch.switch_type == "dimmer"
35  )
36 
37 
39  """Representation of a light within devolo Home Control."""
40 
41  _attr_color_mode = ColorMode.BRIGHTNESS
42 
43  def __init__(
44  self, homecontrol: HomeControl, device_instance: Zwave, element_uid: str
45  ) -> None:
46  """Initialize a devolo multi level switch."""
47  super().__init__(
48  homecontrol=homecontrol,
49  device_instance=device_instance,
50  element_uid=element_uid,
51  )
52 
53  self._attr_supported_color_modes_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
54  self._binary_switch_property_binary_switch_property = device_instance.binary_switch_property.get(
55  element_uid.replace("Dimmer", "BinarySwitch")
56  )
57 
58  @property
59  def brightness(self) -> int:
60  """Return the brightness value of the light."""
61  return round(self._value_value_value / 100 * 255)
62 
63  @property
64  def is_on(self) -> bool:
65  """Return the state of the light."""
66  return bool(self._value_value_value)
67 
68  def turn_on(self, **kwargs: Any) -> None:
69  """Turn device on."""
70  if kwargs.get(ATTR_BRIGHTNESS) is not None:
71  self._multi_level_switch_property_multi_level_switch_property.set(
72  round(kwargs[ATTR_BRIGHTNESS] / 255 * 100)
73  )
74  elif self._binary_switch_property_binary_switch_property is not None:
75  # Turn on the light device to the latest known value. The value is known by the device itself.
76  self._binary_switch_property_binary_switch_property.set(True)
77  else:
78  # If there is no binary switch attached to the device, turn it on to 100 %.
79  self._multi_level_switch_property_multi_level_switch_property.set(100)
80 
81  def turn_off(self, **kwargs: Any) -> None:
82  """Turn device off."""
83  if self._binary_switch_property_binary_switch_property is not None:
84  self._binary_switch_property_binary_switch_property.set(False)
85  else:
86  self._multi_level_switch_property_multi_level_switch_property.set(0)
None __init__(self, HomeControl homecontrol, Zwave device_instance, str element_uid)
Definition: light.py:45
None async_setup_entry(HomeAssistant hass, DevoloHomeControlConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:22