1 """Support for RFXtrx binary sensors."""
3 from __future__
import annotations
8 import RFXtrx
as rfxtrxmod
11 BinarySensorDeviceClass,
13 BinarySensorEntityDescription,
22 from .
import DeviceTuple, async_setup_platform_entry, get_pt2262_cmd
28 DEVICE_PACKET_TYPE_LIGHTING4,
30 from .entity
import RfxtrxEntity
32 _LOGGER = logging.getLogger(__name__)
55 key=
"X10 Security Motion Detector",
56 device_class=BinarySensorDeviceClass.MOTION,
59 key=
"KD101 Smoke Detector",
60 device_class=BinarySensorDeviceClass.SMOKE,
63 key=
"Visonic Powercode Motion Detector",
64 device_class=BinarySensorDeviceClass.MOTION,
67 key=
"Alecto SA30 Smoke Detector",
68 device_class=BinarySensorDeviceClass.SMOKE,
71 key=
"RM174RF Smoke Detector",
72 device_class=BinarySensorDeviceClass.SMOKE,
76 SENSOR_TYPES_DICT = {desc.key: desc
for desc
in SENSOR_TYPES}
79 def supported(event: rfxtrxmod.RFXtrxEvent) -> bool:
80 """Return whether an event supports binary_sensor."""
81 if isinstance(event, rfxtrxmod.ControlEvent):
83 if isinstance(event, rfxtrxmod.SensorEvent):
84 return event.values.get(
"Sensor Status")
in [
93 config_entry: ConfigEntry,
94 async_add_entities: AddEntitiesCallback,
96 """Set up config entry."""
98 def get_sensor_description(type_string: str) -> BinarySensorEntityDescription:
99 if (description := SENSOR_TYPES_DICT.get(type_string))
is None:
104 event: rfxtrxmod.RFXtrxEvent,
105 auto: rfxtrxmod.RFXtrxEvent |
None,
106 device_id: DeviceTuple,
107 entity_info: dict[str, Any],
113 get_sensor_description(event.device.type_string),
114 entity_info.get(CONF_OFF_DELAY),
115 entity_info.get(CONF_DATA_BITS),
116 entity_info.get(CONF_COMMAND_ON),
117 entity_info.get(CONF_COMMAND_OFF),
118 event=event
if auto
else None,
123 hass, config_entry, async_add_entities, supported, _constructor
128 """A representation of a RFXtrx binary sensor.
130 Since all repeated events have meaning, these types of sensors
131 need to have force update enabled.
134 _attr_force_update =
True
139 device: rfxtrxmod.RFXtrxDevice,
140 device_id: DeviceTuple,
141 entity_description: BinarySensorEntityDescription,
142 off_delay: float |
None =
None,
143 data_bits: int |
None =
None,
144 cmd_on: int |
None =
None,
145 cmd_off: int |
None =
None,
146 event: rfxtrxmod.RFXtrxEvent |
None =
None,
148 """Initialize the RFXtrx sensor."""
149 super().
__init__(device, device_id, event=event)
158 """Restore device state."""
161 if self.
_event_event
is None:
163 if old_state
is not None:
166 if self.is_on
and self.
_off_delay_off_delay
is not None:
170 """Apply event for a lighting 4 device."""
174 cmd =
int(cmdstr, 16)
183 assert isinstance(event, (rfxtrxmod.SensorEvent, rfxtrxmod.ControlEvent))
184 if event.values.get(
"Command")
in COMMAND_ON_LIST:
186 elif event.values.get(
"Command")
in COMMAND_OFF_LIST:
188 elif event.values.get(
"Sensor Status")
in SENSOR_STATUS_ON:
190 elif event.values.get(
"Sensor Status")
in SENSOR_STATUS_OFF:
194 """Apply command from rfxtrx."""
196 if event.device.packettype == DEVICE_PACKET_TYPE_LIGHTING4:
203 self, event: rfxtrxmod.RFXtrxEvent, device_id: DeviceTuple
205 """Check if event applies to me and update."""
210 "Binary sensor update (Device ID: %s Class: %s Sub: %s)",
211 event.device.id_string,
212 event.device.__class__.__name__,
213 event.device.subtype,
224 if self.is_on
and self.
_off_delay_off_delay
is not None:
227 def off_delay_listener(now: Any) ->
None:
228 """Switch device off after a delay."""
None async_added_to_hass(self)
None _apply_event(self, rfxtrxmod.RFXtrxEvent event)
None _apply_event_standard(self, rfxtrxmod.RFXtrxEvent event)
None _apply_event_lighting4(self, rfxtrxmod.RFXtrxEvent event)
None _handle_event(self, rfxtrxmod.RFXtrxEvent event, DeviceTuple device_id)
None __init__(self, rfxtrxmod.RFXtrxDevice device, DeviceTuple device_id, BinarySensorEntityDescription entity_description, float|None off_delay=None, int|None data_bits=None, int|None cmd_on=None, int|None cmd_off=None, rfxtrxmod.RFXtrxEvent|None event=None)
None _apply_event(self, rfxtrxmod.RFXtrxEvent event)
bool _event_applies(self, rfxtrxmod.RFXtrxEvent event, DeviceTuple device_id)
None async_write_ha_state(self)
State|None async_get_last_state(self)
bool supported(rfxtrxmod.RFXtrxEvent event)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
str|None get_pt2262_cmd(str device_id, int data_bits)
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)