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

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

139 lines
4.1 KiB
Python
Raw Normal View History

2023-03-03 11:26:13 +01:00
"""Component providing HA sensor support for Ring Door Bell/Chimes."""
2021-08-22 22:26:24 +02:00
from __future__ import annotations
from collections.abc import Callable, Mapping
2021-08-22 22:26:24 +02:00
from dataclasses import dataclass
2020-01-16 16:26:10 -08:00
from datetime import datetime
2023-01-24 16:35:11 +01:00
from typing import Any
from ring_doorbell import Ring, RingEvent, RingGeneric
from homeassistant.components.binary_sensor import (
2021-12-16 08:12:57 -05:00
BinarySensorDeviceClass,
BinarySensorEntity,
2021-08-22 22:26:24 +02:00
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import RingData
from .const import DOMAIN
from .coordinator import RingNotificationsCoordinator
from .entity import RingBaseEntity
2021-08-22 22:26:24 +02:00
@dataclass(frozen=True, kw_only=True)
class RingBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes Ring binary sensor entity."""
2021-08-22 22:26:24 +02:00
exists_fn: Callable[[RingGeneric], bool]
2021-08-22 22:26:24 +02:00
BINARY_SENSOR_TYPES: tuple[RingBinarySensorEntityDescription, ...] = (
RingBinarySensorEntityDescription(
key="ding",
2023-07-09 19:55:10 +02:00
translation_key="ding",
2021-12-16 08:12:57 -05:00
device_class=BinarySensorDeviceClass.OCCUPANCY,
exists_fn=lambda device: device.family
in {"doorbots", "authorized_doorbots", "other"},
2021-08-22 22:26:24 +02:00
),
RingBinarySensorEntityDescription(
key="motion",
2021-12-16 08:12:57 -05:00
device_class=BinarySensorDeviceClass.MOTION,
exists_fn=lambda device: device.family
in {"doorbots", "authorized_doorbots", "stickup_cams"},
2021-08-22 22:26:24 +02:00
),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
2020-01-10 21:35:31 +01:00
"""Set up the Ring binary sensors from a config entry."""
ring_data: RingData = hass.data[DOMAIN][config_entry.entry_id]
2021-08-22 22:26:24 +02:00
entities = [
RingBinarySensor(
ring_data.api,
device,
ring_data.notifications_coordinator,
description,
)
2021-08-22 22:26:24 +02:00
for description in BINARY_SENSOR_TYPES
for device in ring_data.devices.all_devices
if description.exists_fn(device)
2021-08-22 22:26:24 +02:00
]
2021-08-22 22:26:24 +02:00
async_add_entities(entities)
class RingBinarySensor(
RingBaseEntity[RingNotificationsCoordinator], BinarySensorEntity
):
"""A binary sensor implementation for Ring device."""
_active_alert: RingEvent | None = None
2021-08-22 22:26:24 +02:00
entity_description: RingBinarySensorEntityDescription
2020-01-15 08:10:42 -08:00
2021-08-22 22:26:24 +02:00
def __init__(
self,
ring: Ring,
device: RingGeneric,
coordinator: RingNotificationsCoordinator,
2021-08-22 22:26:24 +02:00
description: RingBinarySensorEntityDescription,
) -> None:
"""Initialize a sensor for Ring device."""
super().__init__(
device,
coordinator,
)
2021-08-22 22:26:24 +02:00
self.entity_description = description
2020-01-14 12:54:45 -08:00
self._ring = ring
2021-08-22 22:26:24 +02:00
self._attr_unique_id = f"{device.id}-{description.key}"
2020-01-15 08:10:42 -08:00
self._update_alert()
2020-01-14 12:54:45 -08:00
@callback
def _handle_coordinator_update(self, _: Any = None) -> None:
2020-01-14 12:54:45 -08:00
"""Call update method."""
2020-01-15 08:10:42 -08:00
self._update_alert()
super()._handle_coordinator_update()
2020-01-14 12:54:45 -08:00
2020-01-15 08:10:42 -08:00
@callback
def _update_alert(self) -> None:
2020-01-15 08:10:42 -08:00
"""Update active alert."""
self._active_alert = next(
(
alert
for alert in self._ring.active_alerts()
2021-08-22 22:26:24 +02:00
if alert["kind"] == self.entity_description.key
2020-01-15 08:10:42 -08:00
and alert["doorbot_id"] == self._device.id
),
None,
)
@property
def is_on(self) -> bool:
"""Return True if the binary sensor is on."""
2020-01-15 08:10:42 -08:00
return self._active_alert is not None
@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
"""Return the state attributes."""
attrs = super().extra_state_attributes
2020-01-15 08:10:42 -08:00
if self._active_alert is None:
return attrs
assert isinstance(attrs, dict)
2020-01-15 08:10:42 -08:00
attrs["state"] = self._active_alert["state"]
now = self._active_alert.get("now")
expires_in = self._active_alert.get("expires_in")
assert now and expires_in
attrs["expires_at"] = datetime.fromtimestamp(now + expires_in).isoformat()
return attrs