From d3e60690956c1cf003da84a481ebc39408239b87 Mon Sep 17 00:00:00 2001 From: starkillerOG Date: Mon, 30 Sep 2024 19:00:37 +0200 Subject: [PATCH] Mark Reolink camera entities as unavailable when camera is offline (#127127) Co-authored-by: Franck Nijhof --- homeassistant/components/reolink/entity.py | 5 +++++ tests/components/reolink/conftest.py | 1 + tests/components/reolink/test_switch.py | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/homeassistant/components/reolink/entity.py b/homeassistant/components/reolink/entity.py index d73c3a9b6e6a71..d0a8f6dfc8d1b3 100644 --- a/homeassistant/components/reolink/entity.py +++ b/homeassistant/components/reolink/entity.py @@ -155,6 +155,11 @@ def __init__( configuration_url=self._conf_url, ) + @property + def available(self) -> bool: + """Return True if entity is available.""" + return super().available and self._host.api.camera_online(self._channel) + async def async_added_to_hass(self) -> None: """Entity created.""" await super().async_added_to_hass() diff --git a/tests/components/reolink/conftest.py b/tests/components/reolink/conftest.py index 458bac5022b6ee..79a63963bca361 100644 --- a/tests/components/reolink/conftest.py +++ b/tests/components/reolink/conftest.py @@ -92,6 +92,7 @@ def reolink_connect_class() -> Generator[MagicMock]: host_mock.camera_sw_version.return_value = "v1.1.0.0.0.0000" host_mock.camera_sw_version_update_required.return_value = False host_mock.camera_uid.return_value = TEST_UID_CAM + host_mock.camera_online.return_value = True host_mock.channel_for_uid.return_value = 0 host_mock.get_encoding.return_value = "h264" host_mock.firmware_update_available.return_value = False diff --git a/tests/components/reolink/test_switch.py b/tests/components/reolink/test_switch.py index 142075ca0b0826..b2e82040ad4951 100644 --- a/tests/components/reolink/test_switch.py +++ b/tests/components/reolink/test_switch.py @@ -17,6 +17,7 @@ SERVICE_TURN_ON, STATE_OFF, STATE_ON, + STATE_UNAVAILABLE, Platform, ) from homeassistant.core import HomeAssistant @@ -302,6 +303,15 @@ async def test_switch( reolink_connect.set_recording.reset_mock(side_effect=True) + reolink_connect.camera_online.return_value = False + freezer.tick(DEVICE_UPDATE_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert hass.states.get(entity_id).state == STATE_UNAVAILABLE + + reolink_connect.camera_online.return_value = True + async def test_host_switch( hass: HomeAssistant,