Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for LightwaveRF lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
8 from homeassistant.const import CONF_NAME
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
12 
13 from . import LIGHTWAVE_LINK
14 
15 MAX_BRIGHTNESS = 255
16 
17 
19  hass: HomeAssistant,
20  config: ConfigType,
21  async_add_entities: AddEntitiesCallback,
22  discovery_info: DiscoveryInfoType | None = None,
23 ) -> None:
24  """Find and return LightWave lights."""
25  if not discovery_info:
26  return
27 
28  lights = []
29  lwlink = hass.data[LIGHTWAVE_LINK]
30 
31  for device_id, device_config in discovery_info.items():
32  name = device_config[CONF_NAME]
33  lights.append(LWRFLight(name, device_id, lwlink))
34 
35  async_add_entities(lights)
36 
37 
39  """Representation of a LightWaveRF light."""
40 
41  _attr_color_mode = ColorMode.BRIGHTNESS
42  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
43  _attr_should_poll = False
44 
45  def __init__(self, name, device_id, lwlink):
46  """Initialize LWRFLight entity."""
47  self._attr_name_attr_name = name
48  self._device_id_device_id = device_id
49  self._attr_brightness_attr_brightness = MAX_BRIGHTNESS
50  self._lwlink_lwlink = lwlink
51 
52  async def async_turn_on(self, **kwargs: Any) -> None:
53  """Turn the LightWave light on."""
54  self._attr_is_on_attr_is_on = True
55 
56  if ATTR_BRIGHTNESS in kwargs:
57  self._attr_brightness_attr_brightness = kwargs[ATTR_BRIGHTNESS]
58 
59  if self._attr_brightness_attr_brightness != MAX_BRIGHTNESS:
60  self._lwlink_lwlink.turn_on_with_brightness(
61  self._device_id_device_id, self._attr_name_attr_name, self._attr_brightness_attr_brightness
62  )
63  else:
64  self._lwlink_lwlink.turn_on_light(self._device_id_device_id, self._attr_name_attr_name)
65 
66  self.async_write_ha_stateasync_write_ha_state()
67 
68  async def async_turn_off(self, **kwargs: Any) -> None:
69  """Turn the LightWave light off."""
70  self._attr_is_on_attr_is_on = False
71  self._lwlink_lwlink.turn_off(self._device_id_device_id, self._attr_name_attr_name)
72  self.async_write_ha_stateasync_write_ha_state()
None async_turn_off(self, **Any kwargs)
Definition: light.py:68
def __init__(self, name, device_id, lwlink)
Definition: light.py:45
None async_turn_on(self, **Any kwargs)
Definition: light.py:52
None turn_off(self, **Any kwargs)
Definition: entity.py:1705
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: light.py:23