From edd3c45c09b34715e180070435db75463d31228a Mon Sep 17 00:00:00 2001 From: starkillerOG Date: Wed, 5 Jun 2024 14:30:15 +0200 Subject: [PATCH] Add binary "sleeping" sensor to Reolink (#118774) --- .../components/reolink/binary_sensor.py | 27 +++++++++++++++++-- homeassistant/components/reolink/icons.json | 6 +++++ homeassistant/components/reolink/strings.json | 7 +++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/reolink/binary_sensor.py b/homeassistant/components/reolink/binary_sensor.py index fe80177da12..d19987c3bc6 100644 --- a/homeassistant/components/reolink/binary_sensor.py +++ b/homeassistant/components/reolink/binary_sensor.py @@ -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() diff --git a/homeassistant/components/reolink/icons.json b/homeassistant/components/reolink/icons.json index 6346881e8f7..a06293abf9a 100644 --- a/homeassistant/components/reolink/icons.json +++ b/homeassistant/components/reolink/icons.json @@ -42,6 +42,12 @@ "state": { "on": "mdi:motion-sensor" } + }, + "sleep": { + "default": "mdi:sleep-off", + "state": { + "on": "mdi:sleep" + } } }, "button": { diff --git a/homeassistant/components/reolink/strings.json b/homeassistant/components/reolink/strings.json index d1fa0f4426b..799e7f2cac5 100644 --- a/homeassistant/components/reolink/strings.json +++ b/homeassistant/components/reolink/strings.json @@ -233,6 +233,13 @@ "off": "[%key:component::binary_sensor::entity_component::gas::state::off%]", "on": "[%key:component::binary_sensor::entity_component::gas::state::on%]" } + }, + "sleep": { + "name": "Sleep status", + "state": { + "off": "Awake", + "on": "Sleeping" + } } }, "button": {