2019-02-13 20:21:14 +00:00
|
|
|
"""Binary sensor support for the Skybell HD Doorbell."""
|
2021-07-21 17:42:30 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-06-05 02:37:08 +00:00
|
|
|
from aioskybell.helpers import const as CONST
|
2017-10-08 18:14:39 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-09-12 16:07:13 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
2021-12-16 13:19:23 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-09-12 16:07:13 +00:00
|
|
|
BinarySensorEntity,
|
2021-07-27 17:51:07 +00:00
|
|
|
BinarySensorEntityDescription,
|
2020-09-12 16:07:13 +00:00
|
|
|
)
|
2022-06-05 02:37:08 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import CONF_ENTITY_NAMESPACE, CONF_MONITORED_CONDITIONS
|
2022-06-05 02:37:08 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2017-10-08 18:14:39 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-05 16:34:47 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2017-10-08 18:14:39 +00:00
|
|
|
|
2022-06-05 02:37:08 +00:00
|
|
|
from . import DOMAIN
|
|
|
|
from .coordinator import SkybellDataUpdateCoordinator
|
|
|
|
from .entity import SkybellEntity
|
2021-07-21 17:42:30 +00:00
|
|
|
|
2022-06-05 02:37:08 +00:00
|
|
|
BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key="button",
|
2021-07-27 17:51:07 +00:00
|
|
|
name="Button",
|
2021-12-16 13:19:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.OCCUPANCY,
|
2021-07-21 17:42:30 +00:00
|
|
|
),
|
2022-06-05 02:37:08 +00:00
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key="motion",
|
2021-07-27 17:51:07 +00:00
|
|
|
name="Motion",
|
2021-12-16 13:19:23 +00:00
|
|
|
device_class=BinarySensorDeviceClass.MOTION,
|
2021-07-21 17:42:30 +00:00
|
|
|
),
|
2022-06-05 02:37:08 +00:00
|
|
|
)
|
2021-07-27 17:51:07 +00:00
|
|
|
|
2022-06-05 02:37:08 +00:00
|
|
|
# Deprecated in Home Assistant 2022.6
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
2022-06-05 02:37:08 +00:00
|
|
|
vol.Optional(CONF_ENTITY_NAMESPACE, default=DOMAIN): cv.string,
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Required(CONF_MONITORED_CONDITIONS, default=[]): vol.All(
|
2021-07-27 17:51:07 +00:00
|
|
|
cv.ensure_list, [vol.In(BINARY_SENSOR_TYPES)]
|
2019-07-31 19:25:30 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2017-10-08 18:14:39 +00:00
|
|
|
|
|
|
|
|
2022-06-05 02:37:08 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2022-01-05 16:34:47 +00:00
|
|
|
) -> None:
|
2022-06-05 02:37:08 +00:00
|
|
|
"""Set up Skybell switch."""
|
|
|
|
async_add_entities(
|
|
|
|
SkybellBinarySensor(coordinator, sensor)
|
|
|
|
for sensor in BINARY_SENSOR_TYPES
|
|
|
|
for coordinator in hass.data[DOMAIN][entry.entry_id]
|
|
|
|
)
|
2017-10-08 18:14:39 +00:00
|
|
|
|
|
|
|
|
2022-06-05 02:37:08 +00:00
|
|
|
class SkybellBinarySensor(SkybellEntity, BinarySensorEntity):
|
2017-10-08 18:14:39 +00:00
|
|
|
"""A binary sensor implementation for Skybell devices."""
|
|
|
|
|
2021-07-27 17:51:07 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-06-05 02:37:08 +00:00
|
|
|
coordinator: SkybellDataUpdateCoordinator,
|
2021-07-27 17:51:07 +00:00
|
|
|
description: BinarySensorEntityDescription,
|
2022-06-05 02:37:08 +00:00
|
|
|
) -> None:
|
2017-10-08 18:14:39 +00:00
|
|
|
"""Initialize a binary sensor for a Skybell device."""
|
2022-06-05 02:37:08 +00:00
|
|
|
super().__init__(coordinator, description)
|
|
|
|
self._event: dict[str, str] = {}
|
2017-10-08 18:14:39 +00:00
|
|
|
|
2022-06-05 02:37:08 +00:00
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle updated data from the coordinator."""
|
2021-07-27 17:51:07 +00:00
|
|
|
event = self._device.latest(self.entity_description.key)
|
2022-06-05 02:37:08 +00:00
|
|
|
self._attr_is_on = bool(event.get(CONST.ID) != self._event.get(CONST.ID))
|
|
|
|
self._event = event
|
|
|
|
super()._handle_coordinator_update()
|