Home Assistant Unofficial Reference 2024.12.1
event.py
Go to the documentation of this file.
1 """Support for RFXtrx sensors."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from RFXtrx import ControlEvent, RFXtrxDevice, RFXtrxEvent, SensorEvent
9 
10 from homeassistant.components.event import EventEntity
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers.entity import Entity
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 from homeassistant.util import slugify
16 
17 from . import DeviceTuple, async_setup_platform_entry
18 from .const import DEVICE_PACKET_TYPE_LIGHTING4
19 from .entity import RfxtrxEntity
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 
25  hass: HomeAssistant,
26  config_entry: ConfigEntry,
27  async_add_entities: AddEntitiesCallback,
28 ) -> None:
29  """Set up config entry."""
30 
31  def _supported(event: RFXtrxEvent) -> bool:
32  return (
33  isinstance(event, (ControlEvent, SensorEvent))
34  and event.device.packettype != DEVICE_PACKET_TYPE_LIGHTING4
35  )
36 
37  def _constructor(
38  event: RFXtrxEvent,
39  auto: RFXtrxEvent | None,
40  device_id: DeviceTuple,
41  entity_info: dict[str, Any],
42  ) -> list[Entity]:
43  entities: list[Entity] = []
44 
45  if hasattr(event.device, "COMMANDS"):
46  entities.append(
48  event.device, device_id, "COMMANDS", "Command", "command"
49  )
50  )
51 
52  if hasattr(event.device, "STATUS"):
53  entities.append(
55  event.device, device_id, "STATUS", "Sensor Status", "status"
56  )
57  )
58 
59  return entities
60 
62  hass, config_entry, async_add_entities, _supported, _constructor
63  )
64 
65 
67  """Representation of a RFXtrx event."""
68 
69  def __init__(
70  self,
71  device: RFXtrxDevice,
72  device_id: DeviceTuple,
73  device_attribute: str,
74  value_attribute: str,
75  translation_key: str,
76  ) -> None:
77  """Initialize the sensor."""
78  super().__init__(device, device_id)
79  commands: dict[int, str] = getattr(device, device_attribute)
80  self._attr_name_attr_name = None
81  self._attr_unique_id_attr_unique_id_attr_unique_id = "_".join(x for x in device_id)
82  self._attr_event_types_attr_event_types = [slugify(command) for command in commands.values()]
83  self._attr_translation_key_attr_translation_key = translation_key
84  self._value_attribute_value_attribute = value_attribute
85 
86  @callback
87  def _handle_event(self, event: RFXtrxEvent, device_id: DeviceTuple) -> None:
88  """Check if event applies to me and update."""
89  if not self._event_applies_event_applies(event, device_id):
90  return
91 
92  assert isinstance(event, (ControlEvent, SensorEvent))
93 
94  event_type = slugify(event.values[self._value_attribute_value_attribute])
95  if event_type not in self._attr_event_types_attr_event_types:
96  _LOGGER.warning("Event type %s is not known", event_type)
97  return
98 
99  self._trigger_event_trigger_event(event_type, event.values)
100  self.async_write_ha_stateasync_write_ha_state()
None _trigger_event(self, str event_type, dict[str, Any]|None event_attributes=None)
Definition: __init__.py:148
bool _event_applies(self, rfxtrxmod.RFXtrxEvent event, DeviceTuple device_id)
Definition: entity.py:77
None _handle_event(self, RFXtrxEvent event, DeviceTuple device_id)
Definition: event.py:87
None __init__(self, RFXtrxDevice device, DeviceTuple device_id, str device_attribute, str value_attribute, str translation_key)
Definition: event.py:76
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: event.py:28
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