Files
core/homeassistant/components/skybell/binary_sensor.py
Robert Hillis 58d4ea0db9 Bump aioskybell to 22.6.0 (#73073)
* Bump aioskybell to 22.6.0

* uno mas
2022-06-05 07:09:44 -10:00

89 lines
2.8 KiB
Python

"""Binary sensor support for the Skybell HD Doorbell."""
from __future__ import annotations
from datetime import datetime
from aioskybell.helpers import const as CONST
import voluptuous as vol
from homeassistant.components.binary_sensor import (
PLATFORM_SCHEMA,
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ENTITY_NAMESPACE, CONF_MONITORED_CONDITIONS
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import DOMAIN
from .coordinator import SkybellDataUpdateCoordinator
from .entity import SkybellEntity
BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
BinarySensorEntityDescription(
key="button",
name="Button",
device_class=BinarySensorDeviceClass.OCCUPANCY,
),
BinarySensorEntityDescription(
key="motion",
name="Motion",
device_class=BinarySensorDeviceClass.MOTION,
),
)
# Deprecated in Home Assistant 2022.6
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_ENTITY_NAMESPACE, default=DOMAIN): cv.string,
vol.Required(CONF_MONITORED_CONDITIONS, default=[]): vol.All(
cv.ensure_list, [vol.In(BINARY_SENSOR_TYPES)]
),
}
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""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]
)
class SkybellBinarySensor(SkybellEntity, BinarySensorEntity):
"""A binary sensor implementation for Skybell devices."""
def __init__(
self,
coordinator: SkybellDataUpdateCoordinator,
description: BinarySensorEntityDescription,
) -> None:
"""Initialize a binary sensor for a Skybell device."""
super().__init__(coordinator, description)
self._event: dict[str, str] = {}
@property
def extra_state_attributes(
self,
) -> dict[str, str | int | datetime | tuple[str, str]]:
"""Return the state attributes."""
attrs = super().extra_state_attributes
if event := self._event.get(CONST.CREATED_AT):
attrs["event_date"] = event
return attrs
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
event = self._device.latest(self.entity_description.key)
self._attr_is_on = bool(event.get(CONST.ID) != self._event.get(CONST.ID))
self._event = event
super()._handle_coordinator_update()