Skip to content

Commit

Permalink
Allow manually updating entity state in chacon dio (#124187)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cnico authored Aug 19, 2024
1 parent 96edaeb commit 16e52f0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
8 changes: 8 additions & 0 deletions homeassistant/components/chacon_dio/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ def callback_device_state(self, data: dict[str, Any]) -> None:
_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])
38 changes: 36 additions & 2 deletions tests/components/chacon_dio/test_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
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

Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 16e52f0

Please sign in to comment.