Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for RFXtrx lights."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import RFXtrx as rfxtrxmod
9 
10 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import STATE_ON
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers.entity import Entity
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import DeviceTuple, async_setup_platform_entry
18 from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST
19 from .entity import RfxtrxCommandEntity
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 
24 def supported(event: rfxtrxmod.RFXtrxEvent) -> bool:
25  """Return whether an event supports light."""
26  return (
27  isinstance(event.device, rfxtrxmod.LightingDevice)
28  and event.device.known_to_be_dimmable
29  )
30 
31 
33  hass: HomeAssistant,
34  config_entry: ConfigEntry,
35  async_add_entities: AddEntitiesCallback,
36 ) -> None:
37  """Set up config entry."""
38 
39  def _constructor(
40  event: rfxtrxmod.RFXtrxEvent,
41  auto: rfxtrxmod.RFXtrxEvent | None,
42  device_id: DeviceTuple,
43  entity_info: dict[str, Any],
44  ) -> list[Entity]:
45  return [
47  event.device,
48  device_id,
49  event=event if auto else None,
50  )
51  ]
52 
54  hass, config_entry, async_add_entities, supported, _constructor
55  )
56 
57 
59  """Representation of a RFXtrx light."""
60 
61  _attr_color_mode = ColorMode.BRIGHTNESS
62  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
63  _attr_brightness: int = 0
64  _device: rfxtrxmod.LightingDevice
65 
66  async def async_added_to_hass(self) -> None:
67  """Restore RFXtrx device state (ON/OFF)."""
68  await super().async_added_to_hass()
69 
70  if self._event_event is None:
71  old_state = await self.async_get_last_stateasync_get_last_state()
72  if old_state is not None:
73  self._attr_is_on_attr_is_on = old_state.state == STATE_ON
74  if brightness := old_state.attributes.get(ATTR_BRIGHTNESS):
75  self._attr_brightness_attr_brightness = int(brightness)
76 
77  async def async_turn_on(self, **kwargs: Any) -> None:
78  """Turn the device on."""
79  brightness = kwargs.get(ATTR_BRIGHTNESS)
80  self._attr_is_on_attr_is_on = True
81  if brightness is None:
82  await self._async_send(self._device_device.send_on)
83  self._attr_brightness_attr_brightness = 255
84  else:
85  await self._async_send(self._device_device.send_dim, brightness * 100 // 255)
86  self._attr_brightness_attr_brightness = brightness
87 
88  self.async_write_ha_stateasync_write_ha_state()
89 
90  async def async_turn_off(self, **kwargs: Any) -> None:
91  """Turn the device off."""
92  await self._async_send(self._device_device.send_off)
93  self._attr_is_on_attr_is_on = False
94  self._attr_brightness_attr_brightness = 0
95  self.async_write_ha_stateasync_write_ha_state()
96 
97  def _apply_event(self, event: rfxtrxmod.RFXtrxEvent) -> None:
98  """Apply command from rfxtrx."""
99  assert isinstance(event, rfxtrxmod.ControlEvent)
100  super()._apply_event(event)
101  if event.values["Command"] in COMMAND_ON_LIST:
102  self._attr_is_on_attr_is_on = True
103  elif event.values["Command"] in COMMAND_OFF_LIST:
104  self._attr_is_on_attr_is_on = False
105  elif event.values["Command"] == "Set level":
106  brightness = event.values["Dim level"] * 255 // 100
107  self._attr_brightness_attr_brightness = brightness
108  self._attr_is_on_attr_is_on = brightness > 0
109 
110  @callback
111  def _handle_event(
112  self, event: rfxtrxmod.RFXtrxEvent, device_id: DeviceTuple
113  ) -> None:
114  """Check if event applies to me and update."""
115  if device_id != self._device_id_device_id:
116  return
117 
118  self._apply_event_apply_event(event)
119 
120  self.async_write_ha_stateasync_write_ha_state()
None _apply_event(self, rfxtrxmod.RFXtrxEvent event)
Definition: entity.py:91
None _handle_event(self, rfxtrxmod.RFXtrxEvent event, DeviceTuple device_id)
Definition: entity.py:98
None async_turn_off(self, **Any kwargs)
Definition: entity.py:1709
None async_turn_on(self, **Any kwargs)
Definition: entity.py:1701
bool supported(rfxtrxmod.RFXtrxEvent event)
Definition: light.py:24
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:36
None async_setup_platform_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities, Callable[[rfxtrxmod.RFXtrxEvent], bool] supported, Callable[[rfxtrxmod.RFXtrxEvent, rfxtrxmod.RFXtrxEvent|None, DeviceTuple, dict[str, Any],], list[Entity],] constructor)
Definition: __init__.py:308