From bd3efe57f7135858e0ce731ba4cd5e3b5bcbd1eb Mon Sep 17 00:00:00 2001 From: starkillerOG Date: Sun, 22 Sep 2024 14:44:26 +0200 Subject: [PATCH] Add Reolink hub status light (#126388) * Add Home Hub status led * fix styling * Add tests --- homeassistant/components/reolink/light.py | 77 ++++++++++++++- .../reolink/snapshots/test_diagnostics.ambr | 3 + tests/components/reolink/test_light.py | 97 +++++++++++++++++++ 3 files changed, 175 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/reolink/light.py b/homeassistant/components/reolink/light.py index e7f3d3e5d1a..d545a878068 100644 --- a/homeassistant/components/reolink/light.py +++ b/homeassistant/components/reolink/light.py @@ -20,7 +20,12 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .entity import ReolinkChannelCoordinatorEntity, ReolinkChannelEntityDescription +from .entity import ( + ReolinkChannelCoordinatorEntity, + ReolinkChannelEntityDescription, + ReolinkHostCoordinatorEntity, + ReolinkHostEntityDescription, +) from .util import ReolinkConfigEntry, ReolinkData @@ -37,6 +42,17 @@ class ReolinkLightEntityDescription( turn_on_off_fn: Callable[[Host, int, bool], Any] +@dataclass(frozen=True, kw_only=True) +class ReolinkHostLightEntityDescription( + LightEntityDescription, + ReolinkHostEntityDescription, +): + """A class that describes host light entities.""" + + is_on_fn: Callable[[Host], bool] + turn_on_off_fn: Callable[[Host, bool], Any] + + LIGHT_ENTITIES = ( ReolinkLightEntityDescription( key="floodlight", @@ -59,6 +75,18 @@ LIGHT_ENTITIES = ( ), ) +HOST_LIGHT_ENTITIES = ( + ReolinkHostLightEntityDescription( + key="hub_status_led", + cmd_key="GetStateLight", + translation_key="status_led", + entity_category=EntityCategory.CONFIG, + supported=lambda api: api.supported(None, "state_light"), + is_on_fn=lambda api: api.state_light, + turn_on_off_fn=lambda api, value: api.set_state_light(value), + ), +) + async def async_setup_entry( hass: HomeAssistant, @@ -68,13 +96,20 @@ async def async_setup_entry( """Set up a Reolink light entities.""" reolink_data: ReolinkData = config_entry.runtime_data - async_add_entities( + entities: list[ReolinkLightEntity | ReolinkHostLightEntity] = [ ReolinkLightEntity(reolink_data, channel, entity_description) for entity_description in LIGHT_ENTITIES for channel in reolink_data.host.api.channels if entity_description.supported(reolink_data.host.api, channel) + ] + entities.extend( + ReolinkHostLightEntity(reolink_data, entity_description) + for entity_description in HOST_LIGHT_ENTITIES + if entity_description.supported(reolink_data.host.api) ) + async_add_entities(entities) + class ReolinkLightEntity(ReolinkChannelCoordinatorEntity, LightEntity): """Base light entity class for Reolink IP cameras.""" @@ -148,3 +183,41 @@ class ReolinkLightEntity(ReolinkChannelCoordinatorEntity, LightEntity): except ReolinkError as err: raise HomeAssistantError(err) from err self.async_write_ha_state() + + +class ReolinkHostLightEntity(ReolinkHostCoordinatorEntity, LightEntity): + """Base host light entity class for Reolink IP cameras.""" + + entity_description: ReolinkHostLightEntityDescription + _attr_supported_color_modes = {ColorMode.ONOFF} + _attr_color_mode = ColorMode.ONOFF + + def __init__( + self, + reolink_data: ReolinkData, + entity_description: ReolinkHostLightEntityDescription, + ) -> None: + """Initialize Reolink host light entity.""" + self.entity_description = entity_description + super().__init__(reolink_data) + + @property + def is_on(self) -> bool: + """Return true if light is on.""" + return self.entity_description.is_on_fn(self._host.api) + + async def async_turn_off(self, **kwargs: Any) -> None: + """Turn light off.""" + try: + await self.entity_description.turn_on_off_fn(self._host.api, False) + except ReolinkError as err: + raise HomeAssistantError(err) from err + self.async_write_ha_state() + + async def async_turn_on(self, **kwargs: Any) -> None: + """Turn light on.""" + try: + await self.entity_description.turn_on_off_fn(self._host.api, True) + except ReolinkError as err: + raise HomeAssistantError(err) from err + self.async_write_ha_state() diff --git a/tests/components/reolink/snapshots/test_diagnostics.ambr b/tests/components/reolink/snapshots/test_diagnostics.ambr index b8646eb0bee..542df064f5d 100644 --- a/tests/components/reolink/snapshots/test_diagnostics.ambr +++ b/tests/components/reolink/snapshots/test_diagnostics.ambr @@ -137,6 +137,9 @@ '0': 1, 'null': 2, }), + 'GetStateLight': dict({ + 'null': 1, + }), 'GetWhiteLed': dict({ '0': 3, 'null': 3, diff --git a/tests/components/reolink/test_light.py b/tests/components/reolink/test_light.py index c495a0ff25e..7c0c11c3f63 100644 --- a/tests/components/reolink/test_light.py +++ b/tests/components/reolink/test_light.py @@ -144,3 +144,100 @@ async def test_light_turn_on( {ATTR_ENTITY_ID: entity_id, ATTR_BRIGHTNESS: 51}, blocking=True, ) + + +async def test_host_light_state( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test host light entity state with status led.""" + reolink_connect.state_light = True + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.LIGHT]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.LIGHT}.{TEST_NVR_NAME}_status_led" + + state = hass.states.get(entity_id) + assert state.state == STATE_ON + + +async def test_host_light_turn_off( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test host light turn off service.""" + + def mock_supported(ch, capability): + if capability == "power_led": + return False + return True + + reolink_connect.supported = mock_supported + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.LIGHT]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.LIGHT}.{TEST_NVR_NAME}_status_led" + + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + reolink_connect.set_state_light.assert_called_with(False) + + reolink_connect.set_state_light.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + + +async def test_host_light_turn_on( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test host light turn on service.""" + + def mock_supported(ch, capability): + if capability == "power_led": + return False + return True + + reolink_connect.supported = mock_supported + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.LIGHT]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.LIGHT}.{TEST_NVR_NAME}_status_led" + + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + reolink_connect.set_state_light.assert_called_with(True) + + reolink_connect.set_state_light.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + )