Files
homeassistant-core/homeassistant/components/rfxtrx/binary_sensor.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

235 lines
7.2 KiB
Python
Raw Normal View History

2019-02-13 21:21:14 +01:00
"""Support for RFXtrx binary sensors."""
from __future__ import annotations
2017-06-22 07:48:45 +02:00
import logging
2022-09-23 16:47:58 +02:00
from typing import Any
2019-10-14 17:38:34 +02:00
2019-10-12 21:37:59 +02:00
import RFXtrx as rfxtrxmod
2018-01-12 20:52:53 +01:00
from homeassistant.components.binary_sensor import (
2021-12-16 08:34:10 -05:00
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
2022-01-13 07:00:14 +01:00
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_COMMAND_OFF, CONF_COMMAND_ON, STATE_ON
2022-01-13 07:00:14 +01:00
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.helpers import event as evt
2022-09-23 16:47:58 +02:00
from homeassistant.helpers.entity import Entity
2022-01-13 07:00:14 +01:00
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import DeviceTuple, RfxtrxEntity, async_setup_platform_entry, get_pt2262_cmd
2021-09-09 07:17:02 +02:00
from .const import (
COMMAND_OFF_LIST,
COMMAND_ON_LIST,
CONF_DATA_BITS,
CONF_OFF_DELAY,
DEVICE_PACKET_TYPE_LIGHTING4,
)
2017-06-22 07:48:45 +02:00
_LOGGER = logging.getLogger(__name__)
SENSOR_STATUS_ON = [
"Panic",
"Motion",
"Motion Tamper",
"Light Detected",
"Alarm",
"Alarm Tamper",
]
SENSOR_STATUS_OFF = [
"End Panic",
"No Motion",
"No Motion Tamper",
"Dark Detected",
"Normal",
"Normal Tamper",
]
SENSOR_TYPES = (
BinarySensorEntityDescription(
key="X10 Security Motion Detector",
2021-12-16 08:34:10 -05:00
device_class=BinarySensorDeviceClass.MOTION,
),
BinarySensorEntityDescription(
key="KD101 Smoke Detector",
2021-12-16 08:34:10 -05:00
device_class=BinarySensorDeviceClass.SMOKE,
),
BinarySensorEntityDescription(
key="Visonic Powercode Motion Detector",
2021-12-16 08:34:10 -05:00
device_class=BinarySensorDeviceClass.MOTION,
),
BinarySensorEntityDescription(
key="Alecto SA30 Smoke Detector",
2021-12-16 08:34:10 -05:00
device_class=BinarySensorDeviceClass.SMOKE,
),
BinarySensorEntityDescription(
key="RM174RF Smoke Detector",
2021-12-16 08:34:10 -05:00
device_class=BinarySensorDeviceClass.SMOKE,
),
)
SENSOR_TYPES_DICT = {desc.key: desc for desc in SENSOR_TYPES}
2022-09-23 16:47:58 +02:00
def supported(event: rfxtrxmod.RFXtrxEvent) -> bool:
"""Return whether an event supports binary_sensor."""
if isinstance(event, rfxtrxmod.ControlEvent):
return True
if isinstance(event, rfxtrxmod.SensorEvent):
return event.values.get("Sensor Status") in [
*SENSOR_STATUS_ON,
*SENSOR_STATUS_OFF,
]
return False
2020-07-13 23:24:28 +02:00
async def async_setup_entry(
2022-01-13 07:00:14 +01:00
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up config entry."""
2020-07-13 23:24:28 +02:00
2022-09-23 16:47:58 +02:00
def get_sensor_description(type_string: str) -> BinarySensorEntityDescription:
2021-10-20 17:42:26 +02:00
if (description := SENSOR_TYPES_DICT.get(type_string)) is None:
return BinarySensorEntityDescription(key=type_string)
return description
def _constructor(
event: rfxtrxmod.RFXtrxEvent,
2022-01-13 07:00:14 +01:00
auto: rfxtrxmod.RFXtrxEvent | None,
device_id: DeviceTuple,
2022-09-23 16:47:58 +02:00
entity_info: dict[str, Any],
) -> list[Entity]:
return [
RfxtrxBinarySensor(
event.device,
device_id,
get_sensor_description(event.device.type_string),
entity_info.get(CONF_OFF_DELAY),
entity_info.get(CONF_DATA_BITS),
entity_info.get(CONF_COMMAND_ON),
entity_info.get(CONF_COMMAND_OFF),
event=event if auto else None,
)
]
2017-06-22 07:48:45 +02:00
await async_setup_platform_entry(
hass, config_entry, async_add_entities, supported, _constructor
)
2017-06-22 07:48:45 +02:00
class RfxtrxBinarySensor(RfxtrxEntity, BinarySensorEntity):
2022-09-23 16:47:58 +02:00
"""A representation of a RFXtrx binary sensor.
Since all repeated events have meaning, these types of sensors
need to have force update enabled.
"""
2017-06-22 07:48:45 +02:00
_attr_force_update = True
_attr_name = None
def __init__(
self,
2021-12-09 22:35:53 +01:00
device: rfxtrxmod.RFXtrxDevice,
device_id: DeviceTuple,
entity_description: BinarySensorEntityDescription,
off_delay: float | None = None,
data_bits: int | None = None,
cmd_on: int | None = None,
cmd_off: int | None = None,
event: rfxtrxmod.RFXtrxEvent | None = None,
) -> None:
2018-01-21 07:35:38 +01:00
"""Initialize the RFXtrx sensor."""
super().__init__(device, device_id, event=event)
self.entity_description = entity_description
self._data_bits = data_bits
2017-06-22 07:48:45 +02:00
self._off_delay = off_delay
2021-12-09 22:35:53 +01:00
self._delay_listener: CALLBACK_TYPE | None = None
2017-06-22 07:48:45 +02:00
self._cmd_on = cmd_on
self._cmd_off = cmd_off
2022-09-06 09:47:35 +02:00
async def async_added_to_hass(self) -> None:
"""Restore device state."""
await super().async_added_to_hass()
if self._event is None:
old_state = await self.async_get_last_state()
if old_state is not None:
self._attr_is_on = old_state.state == STATE_ON
if self.is_on and self._off_delay is not None:
self._attr_is_on = False
2017-06-22 07:48:45 +02:00
2022-09-23 16:47:58 +02:00
def _apply_event_lighting4(self, event: rfxtrxmod.RFXtrxEvent) -> None:
2020-07-06 00:10:26 +02:00
"""Apply event for a lighting 4 device."""
if self._data_bits is not None:
2021-12-09 22:35:53 +01:00
cmdstr = get_pt2262_cmd(event.device.id_string, self._data_bits)
assert cmdstr
cmd = int(cmdstr, 16)
if cmd == self._cmd_on:
self._attr_is_on = True
elif cmd == self._cmd_off:
self._attr_is_on = False
2020-07-06 00:10:26 +02:00
else:
self._attr_is_on = True
2020-07-06 00:10:26 +02:00
2022-09-23 16:47:58 +02:00
def _apply_event_standard(self, event: rfxtrxmod.RFXtrxEvent) -> None:
2021-12-09 22:35:53 +01:00
assert isinstance(event, (rfxtrxmod.SensorEvent, rfxtrxmod.ControlEvent))
if event.values.get("Command") in COMMAND_ON_LIST:
self._attr_is_on = True
elif event.values.get("Command") in COMMAND_OFF_LIST:
self._attr_is_on = False
elif event.values.get("Sensor Status") in SENSOR_STATUS_ON:
self._attr_is_on = True
elif event.values.get("Sensor Status") in SENSOR_STATUS_OFF:
self._attr_is_on = False
2020-07-06 00:10:26 +02:00
2022-09-23 16:47:58 +02:00
def _apply_event(self, event: rfxtrxmod.RFXtrxEvent) -> None:
2020-07-06 00:10:26 +02:00
"""Apply command from rfxtrx."""
super()._apply_event(event)
if event.device.packettype == DEVICE_PACKET_TYPE_LIGHTING4:
2020-07-06 00:10:26 +02:00
self._apply_event_lighting4(event)
else:
self._apply_event_standard(event)
2017-06-22 07:48:45 +02:00
2020-07-13 23:24:28 +02:00
@callback
2022-09-23 16:47:58 +02:00
def _handle_event(
self, event: rfxtrxmod.RFXtrxEvent, device_id: DeviceTuple
) -> None:
"""Check if event applies to me and update."""
if not self._event_applies(event, device_id):
return
_LOGGER.debug(
"Binary sensor update (Device ID: %s Class: %s Sub: %s)",
event.device.id_string,
event.device.__class__.__name__,
event.device.subtype,
)
self._apply_event(event)
2020-07-13 23:24:28 +02:00
self.async_write_ha_state()
2020-07-06 00:10:26 +02:00
if self._delay_listener:
self._delay_listener()
self._delay_listener = None
if self.is_on and self._off_delay is not None:
2020-07-06 00:10:26 +02:00
2020-07-13 23:24:28 +02:00
@callback
2022-09-23 16:47:58 +02:00
def off_delay_listener(now: Any) -> None:
2020-07-06 00:10:26 +02:00
"""Switch device off after a delay."""
self._delay_listener = None
self._attr_is_on = False
2020-07-13 23:24:28 +02:00
self.async_write_ha_state()
2020-07-06 00:10:26 +02:00
self._delay_listener = evt.async_call_later(
self.hass, self._off_delay, off_delay_listener
2020-07-06 00:10:26 +02:00
)