core/homeassistant/components/skybell/binary_sensor.py

63 lines
2.0 KiB
Python
Raw Normal View History

"""Binary sensor support for the Skybell HD Doorbell."""
from __future__ import annotations
2022-06-05 02:37:08 +00:00
from aioskybell.helpers import const as CONST
from homeassistant.components.binary_sensor import (
2021-12-16 13:19:23 +00:00
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
2022-06-05 02:37:08 +00:00
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2022-06-05 02:37:08 +00:00
from . import DOMAIN
from .coordinator import SkybellDataUpdateCoordinator
from .entity import SkybellEntity
2022-06-05 02:37:08 +00:00
BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
BinarySensorEntityDescription(
key="button",
name="Button",
2021-12-16 13:19:23 +00:00
device_class=BinarySensorDeviceClass.OCCUPANCY,
),
2022-06-05 02:37:08 +00:00
BinarySensorEntityDescription(
key="motion",
name="Motion",
2021-12-16 13:19:23 +00:00
device_class=BinarySensorDeviceClass.MOTION,
),
2022-06-05 02:37:08 +00:00
)
2022-06-05 02:37:08 +00:00
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Skybell binary sensor."""
2022-06-05 02:37:08 +00:00
async_add_entities(
SkybellBinarySensor(coordinator, sensor)
for sensor in BINARY_SENSOR_TYPES
for coordinator in hass.data[DOMAIN][entry.entry_id]
)
2022-06-05 02:37:08 +00:00
class SkybellBinarySensor(SkybellEntity, BinarySensorEntity):
"""A binary sensor implementation for Skybell devices."""
def __init__(
self,
2022-06-05 02:37:08 +00:00
coordinator: SkybellDataUpdateCoordinator,
description: BinarySensorEntityDescription,
2022-06-05 02:37:08 +00:00
) -> None:
"""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] = {}
2022-06-05 02:37:08 +00:00
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
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()