1 """Support for RFXtrx sirens."""
3 from __future__
import annotations
5 from datetime
import datetime
8 import RFXtrx
as rfxtrxmod
17 from .
import DEFAULT_OFF_DELAY, DeviceTuple, async_setup_platform_entry
18 from .const
import CONF_OFF_DELAY
19 from .entity
import RfxtrxCommandEntity
21 SECURITY_PANIC_ON =
"Panic"
22 SECURITY_PANIC_OFF =
"End Panic"
23 SECURITY_PANIC_ALL = {SECURITY_PANIC_ON, SECURITY_PANIC_OFF}
26 def supported(event: rfxtrxmod.RFXtrxEvent) -> bool:
27 """Return whether an event supports sirens."""
30 if isinstance(device, rfxtrxmod.ChimeDevice):
33 if isinstance(device, rfxtrxmod.SecurityDevice)
and isinstance(
34 event, rfxtrxmod.SensorEvent
36 if event.values[
"Sensor Status"]
in SECURITY_PANIC_ALL:
43 """Find a key based on the items value."""
44 return next((key
for key, value
in data.items()
if value == entry))
49 config_entry: ConfigEntry,
50 async_add_entities: AddEntitiesCallback,
52 """Set up config entry."""
55 event: rfxtrxmod.RFXtrxEvent,
56 auto: rfxtrxmod.RFXtrxEvent |
None,
57 device_id: DeviceTuple,
58 entity_info: dict[str, Any],
60 """Construct a entity from an event."""
63 if isinstance(device, rfxtrxmod.ChimeDevice):
68 entity_info.get(CONF_OFF_DELAY, DEFAULT_OFF_DELAY),
73 if isinstance(device, rfxtrxmod.SecurityDevice)
and isinstance(
74 event, rfxtrxmod.SensorEvent
76 if event.values[
"Sensor Status"]
in SECURITY_PANIC_ALL:
81 entity_info.get(CONF_OFF_DELAY, DEFAULT_OFF_DELAY),
88 hass, config_entry, async_add_entities, supported, _constructor
93 """Mixin to support timeouts on data.
95 Many 433 devices only send data when active. They will
96 repeatedly (every x seconds) send a command to indicate
97 being active and stop sending this command when inactive.
98 This mixin allow us to keep track of the timeout once
102 _timeout: CALLBACK_TYPE |
None =
None
103 _off_delay: float |
None =
None
107 def _done(_: datetime) ->
None:
120 """Run when entity will be removed from hass."""
126 """Representation of a RFXtrx chime."""
128 _attr_supported_features = SirenEntityFeature.TURN_ON | SirenEntityFeature.TONES
129 _device: rfxtrxmod.ChimeDevice
133 device: rfxtrxmod.RFXtrxDevice,
134 device_id: DeviceTuple,
135 off_delay: float |
None =
None,
136 event: rfxtrxmod.RFXtrxEvent |
None =
None,
138 """Initialize the entity."""
139 super().
__init__(device, device_id, event)
146 """Return true if device is on."""
147 return self.
_timeout_timeout
is not None
150 """Turn the device on."""
153 if tone := kwargs.get(ATTR_TONE):
158 await self._async_send(self.
_device_device.send_command, command)
165 """Apply a received event."""
168 sound = event.values.get(
"Sound")
169 if sound
is not None:
175 self, event: rfxtrxmod.RFXtrxEvent, device_id: DeviceTuple
177 """Check if event applies to me and update."""
185 """Representation of a security device."""
187 _attr_supported_features = SirenEntityFeature.TURN_ON | SirenEntityFeature.TURN_OFF
188 _device: rfxtrxmod.SecurityDevice
192 device: rfxtrxmod.RFXtrxDevice,
193 device_id: DeviceTuple,
194 off_delay: float |
None =
None,
195 event: rfxtrxmod.RFXtrxEvent |
None =
None,
197 """Initialize the entity."""
198 super().
__init__(device, device_id, event)
205 """Return true if device is on."""
206 return self.
_timeout_timeout
is not None
209 """Turn the device on."""
212 await self._async_send(self.
_device_device.send_status, self.
_on_value_on_value)
219 """Turn the device off."""
222 await self._async_send(self.
_device_device.send_status, self.
_off_value_off_value)
227 """Apply a received event."""
230 status = event.values.get(
"Sensor Status")
232 if status == SECURITY_PANIC_ON:
235 elif status == SECURITY_PANIC_OFF:
240 self, event: rfxtrxmod.RFXtrxEvent, device_id: DeviceTuple
242 """Check if event applies to me and update."""
None _apply_event(self, rfxtrxmod.RFXtrxEvent event)
bool _event_applies(self, rfxtrxmod.RFXtrxEvent event, DeviceTuple device_id)
None _handle_event(self, rfxtrxmod.RFXtrxEvent event, DeviceTuple device_id)
None _apply_event(self, rfxtrxmod.ControlEvent event)
None __init__(self, rfxtrxmod.RFXtrxDevice device, DeviceTuple device_id, float|None off_delay=None, rfxtrxmod.RFXtrxEvent|None event=None)
None async_turn_on(self, **Any kwargs)
None _cancel_timeout(self)
None _setup_timeout(self)
None async_will_remove_from_hass(self)
None async_turn_off(self, **Any kwargs)
None _handle_event(self, rfxtrxmod.RFXtrxEvent event, DeviceTuple device_id)
None _apply_event(self, rfxtrxmod.SensorEvent event)
None async_turn_on(self, **Any kwargs)
None __init__(self, rfxtrxmod.RFXtrxDevice device, DeviceTuple device_id, float|None off_delay=None, rfxtrxmod.RFXtrxEvent|None event=None)
None async_write_ha_state(self)
bool supported(rfxtrxmod.RFXtrxEvent event)
int get_first_key(dict[int, str] data, str entry)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
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)
CALLBACK_TYPE async_call_later(HomeAssistant hass, float|timedelta delay, HassJob[[datetime], Coroutine[Any, Any, None]|None]|Callable[[datetime], Coroutine[Any, Any, None]|None] action)