Add binary "sleeping" sensor to Reolink (#118774)

This commit is contained in:
starkillerOG 2024-06-05 14:30:15 +02:00 committed by GitHub
parent 0d1fb1fc9f
commit edd3c45c09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 38 additions and 2 deletions

View file

@ -21,6 +21,7 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -40,7 +41,7 @@ class ReolinkBinarySensorEntityDescription(
value: Callable[[Host, int], bool]
BINARY_SENSORS = (
BINARY_PUSH_SENSORS = (
ReolinkBinarySensorEntityDescription(
key="motion",
device_class=BinarySensorDeviceClass.MOTION,
@ -93,6 +94,17 @@ BINARY_SENSORS = (
),
)
BINARY_SENSORS = (
ReolinkBinarySensorEntityDescription(
key="sleep",
cmd_key="GetChannelstatus",
translation_key="sleep",
entity_category=EntityCategory.DIAGNOSTIC,
value=lambda api, ch: api.sleeping(ch),
supported=lambda api, ch: api.supported(ch, "sleep"),
),
)
async def async_setup_entry(
hass: HomeAssistant,
@ -104,6 +116,13 @@ async def async_setup_entry(
entities: list[ReolinkBinarySensorEntity] = []
for channel in reolink_data.host.api.channels:
entities.extend(
[
ReolinkPushBinarySensorEntity(reolink_data, channel, entity_description)
for entity_description in BINARY_PUSH_SENSORS
if entity_description.supported(reolink_data.host.api, channel)
]
)
entities.extend(
[
ReolinkBinarySensorEntity(reolink_data, channel, entity_description)
@ -116,7 +135,7 @@ async def async_setup_entry(
class ReolinkBinarySensorEntity(ReolinkChannelCoordinatorEntity, BinarySensorEntity):
"""Base binary-sensor class for Reolink IP camera motion sensors."""
"""Base binary-sensor class for Reolink IP camera."""
entity_description: ReolinkBinarySensorEntityDescription
@ -142,6 +161,10 @@ class ReolinkBinarySensorEntity(ReolinkChannelCoordinatorEntity, BinarySensorEnt
"""State of the sensor."""
return self.entity_description.value(self._host.api, self._channel)
class ReolinkPushBinarySensorEntity(ReolinkBinarySensorEntity):
"""Binary-sensor class for Reolink IP camera motion sensors."""
async def async_added_to_hass(self) -> None:
"""Entity created."""
await super().async_added_to_hass()