2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Rflink binary sensors."""
|
2022-01-03 10:35:02 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-10-18 20:28:40 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_CLASSES_SCHEMA,
|
|
|
|
PLATFORM_SCHEMA,
|
2020-04-23 19:57:07 +00:00
|
|
|
BinarySensorEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-02-12 22:32:56 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_DEVICE_CLASS,
|
|
|
|
CONF_DEVICES,
|
|
|
|
CONF_FORCE_UPDATE,
|
|
|
|
CONF_NAME,
|
2022-02-21 18:11:05 +00:00
|
|
|
STATE_ON,
|
2021-02-12 22:32:56 +00:00
|
|
|
)
|
2022-01-03 10:35:02 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2018-10-18 20:28:40 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 10:35:02 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-10-18 20:28:40 +00:00
|
|
|
import homeassistant.helpers.event as evt
|
2022-02-21 18:11:05 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2022-01-03 10:35:02 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2018-10-18 20:28:40 +00:00
|
|
|
|
2021-02-12 22:32:56 +00:00
|
|
|
from . import CONF_ALIASES, RflinkDevice
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_OFF_DELAY = "off_delay"
|
2018-10-18 20:28:40 +00:00
|
|
|
DEFAULT_FORCE_UPDATE = False
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_DEVICES, default={}): {
|
|
|
|
cv.string: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_FORCE_UPDATE, default=DEFAULT_FORCE_UPDATE
|
|
|
|
): cv.boolean,
|
|
|
|
vol.Optional(CONF_OFF_DELAY): cv.positive_int,
|
|
|
|
vol.Optional(CONF_ALIASES, default=[]): vol.All(
|
|
|
|
cv.ensure_list, [cv.string]
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2018-10-18 20:28:40 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2018-10-18 20:28:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def devices_from_config(domain_config):
|
|
|
|
"""Parse configuration and add Rflink sensor devices."""
|
|
|
|
devices = []
|
|
|
|
for device_id, config in domain_config[CONF_DEVICES].items():
|
|
|
|
device = RflinkBinarySensor(device_id, **config)
|
|
|
|
devices.append(device)
|
|
|
|
|
|
|
|
return devices
|
|
|
|
|
|
|
|
|
2022-01-03 10:35:02 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2018-10-18 20:28:40 +00:00
|
|
|
"""Set up the Rflink platform."""
|
|
|
|
async_add_entities(devices_from_config(config))
|
|
|
|
|
|
|
|
|
2022-02-21 18:11:05 +00:00
|
|
|
class RflinkBinarySensor(RflinkDevice, BinarySensorEntity, RestoreEntity):
|
2018-10-18 20:28:40 +00:00
|
|
|
"""Representation of an Rflink binary sensor."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(
|
|
|
|
self, device_id, device_class=None, force_update=False, off_delay=None, **kwargs
|
|
|
|
):
|
2018-10-18 20:28:40 +00:00
|
|
|
"""Handle sensor specific args and super init."""
|
|
|
|
self._state = None
|
|
|
|
self._device_class = device_class
|
|
|
|
self._force_update = force_update
|
|
|
|
self._off_delay = off_delay
|
|
|
|
self._delay_listener = None
|
|
|
|
super().__init__(device_id, **kwargs)
|
|
|
|
|
2022-02-21 18:11:05 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Restore RFLink BinarySensor state."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
if (old_state := await self.async_get_last_state()) is not None:
|
|
|
|
if self._off_delay is None:
|
|
|
|
self._state = old_state.state == STATE_ON
|
|
|
|
else:
|
|
|
|
self._state = False
|
|
|
|
|
2018-10-18 20:28:40 +00:00
|
|
|
def _handle_event(self, event):
|
|
|
|
"""Domain specific event handler."""
|
2019-07-31 19:25:30 +00:00
|
|
|
command = event["command"]
|
2020-04-17 15:15:04 +00:00
|
|
|
if command in ["on", "allon"]:
|
2018-10-18 20:28:40 +00:00
|
|
|
self._state = True
|
2020-04-17 15:15:04 +00:00
|
|
|
elif command in ["off", "alloff"]:
|
2018-10-18 20:28:40 +00:00
|
|
|
self._state = False
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._state and self._off_delay is not None:
|
|
|
|
|
2021-12-18 23:14:41 +00:00
|
|
|
@callback
|
2018-10-18 20:28:40 +00:00
|
|
|
def off_delay_listener(now):
|
|
|
|
"""Switch device off after a delay."""
|
|
|
|
self._delay_listener = None
|
|
|
|
self._state = False
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2018-10-18 20:28:40 +00:00
|
|
|
|
|
|
|
if self._delay_listener is not None:
|
|
|
|
self._delay_listener()
|
|
|
|
self._delay_listener = evt.async_call_later(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, self._off_delay, off_delay_listener
|
|
|
|
)
|
2018-10-18 20:28:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if the binary sensor is on."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this sensor."""
|
|
|
|
return self._device_class
|
|
|
|
|
|
|
|
@property
|
|
|
|
def force_update(self):
|
|
|
|
"""Force update."""
|
|
|
|
return self._force_update
|