From 16e52f04271703a23024501d809dfd573fea8015 Mon Sep 17 00:00:00 2001 From: cnico Date: Mon, 19 Aug 2024 14:05:12 +0200 Subject: [PATCH] Allow manually updating entity state in chacon dio (#124187) * Addition of a reload service to manually retrieve the status of the devices. * Removal of reload_state service replaced by the homeassistant.update_entity supported service * remove api update to v1.2.1 for another PR * Review corrections * Review corrections --- homeassistant/components/chacon_dio/entity.py | 8 ++++ tests/components/chacon_dio/test_cover.py | 38 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/chacon_dio/entity.py b/homeassistant/components/chacon_dio/entity.py index 38f3d7f5831..7cec6810897 100644 --- a/homeassistant/components/chacon_dio/entity.py +++ b/homeassistant/components/chacon_dio/entity.py @@ -51,3 +51,11 @@ class ChaconDioEntity(Entity): _LOGGER.debug("Data received from server %s", data) self._update_attr(data) self.async_write_ha_state() + + async def async_update(self) -> None: + """Update the state when the entity is requested to.""" + + _LOGGER.debug("Update called for %s, %s", self, self.target_id) + data = await self.client.get_status_details([self.target_id]) + _LOGGER.debug("Received data from server %s", data) + self._update_attr(data[self.target_id]) diff --git a/tests/components/chacon_dio/test_cover.py b/tests/components/chacon_dio/test_cover.py index be606e67e1e..24e6e8581d8 100644 --- a/tests/components/chacon_dio/test_cover.py +++ b/tests/components/chacon_dio/test_cover.py @@ -17,9 +17,11 @@ from homeassistant.components.cover import ( STATE_OPEN, STATE_OPENING, ) +from homeassistant.components.homeassistant import SERVICE_UPDATE_ENTITY from homeassistant.const import ATTR_ENTITY_ID -from homeassistant.core import HomeAssistant +from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant from homeassistant.helpers import entity_registry as er +from homeassistant.setup import async_setup_component from . import setup_integration @@ -42,6 +44,38 @@ async def test_entities( await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) +async def test_update( + hass: HomeAssistant, + mock_dio_chacon_client: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test the creation and values of the Chacon Dio covers.""" + + await setup_integration(hass, mock_config_entry) + + mock_dio_chacon_client.get_status_details.return_value = { + "L4HActuator_idmock1": { + "id": "L4HActuator_idmock1", + "connected": True, + "openlevel": 51, + "movement": "stop", + } + } + + await async_setup_component(hass, HOMEASSISTANT_DOMAIN, {}) + await hass.services.async_call( + HOMEASSISTANT_DOMAIN, + SERVICE_UPDATE_ENTITY, + {ATTR_ENTITY_ID: COVER_ENTITY_ID}, + blocking=True, + ) + + state = hass.states.get(COVER_ENTITY_ID) + assert state + assert state.attributes.get(ATTR_CURRENT_POSITION) == 51 + assert state.state == STATE_OPEN + + async def test_cover_actions( hass: HomeAssistant, mock_dio_chacon_client: AsyncMock, @@ -100,7 +134,7 @@ async def test_cover_callbacks( mock_config_entry: MockConfigEntry, entity_registry: er.EntityRegistry, ) -> None: - """Test the creation and values of the Chacon Dio covers.""" + """Test the callbacks on the Chacon Dio covers.""" await setup_integration(hass, mock_config_entry)