diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index 92100eaddaf..368a997c62e 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -618,6 +618,13 @@ class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity): super()._update_callback() return + async def async_update(self) -> None: + """Update the entity.""" + LOGGER.info( + "Entity %s comes from a sleeping device, update is not possible", + self.entity_id, + ) + class ShellySleepingRpcAttributeEntity(ShellyRpcAttributeEntity): """Helper class to represent a sleeping rpc attribute.""" @@ -654,3 +661,10 @@ class ShellySleepingRpcAttributeEntity(ShellyRpcAttributeEntity): ) elif entry is not None: self._attr_name = cast(str, entry.original_name) + + async def async_update(self) -> None: + """Update the entity.""" + LOGGER.info( + "Entity %s comes from a sleeping device, update is not possible", + self.entity_id, + ) diff --git a/tests/components/shelly/test_sensor.py b/tests/components/shelly/test_sensor.py index a738113f18f..380f4f5999e 100644 --- a/tests/components/shelly/test_sensor.py +++ b/tests/components/shelly/test_sensor.py @@ -1,9 +1,15 @@ """Tests for Shelly sensor platform.""" from freezegun.api import FrozenDateTimeFactory +import pytest +from homeassistant.components.homeassistant import ( + DOMAIN as HA_DOMAIN, + SERVICE_UPDATE_ENTITY, +) from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.shelly.const import DOMAIN from homeassistant.const import ( + ATTR_ENTITY_ID, ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE, STATE_UNAVAILABLE, @@ -12,6 +18,7 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant, State from homeassistant.helpers.entity_registry import async_get +from homeassistant.setup import async_setup_component from . import ( init_integration, @@ -448,3 +455,76 @@ async def test_rpc_em1_sensors( entry = registry.async_get("sensor.test_name_em1_total_active_energy") assert entry assert entry.unique_id == "123456789ABC-em1data:1-total_act_energy" + + +async def test_rpc_sleeping_update_entity_service( + hass: HomeAssistant, mock_rpc_device, caplog: pytest.LogCaptureFixture +) -> None: + """Test RPC sleeping device when the update_entity service is used.""" + await async_setup_component(hass, "homeassistant", {}) + + entity_id = f"{SENSOR_DOMAIN}.test_name_temperature" + await init_integration(hass, 2, sleep_period=1000) + + # Entity should be created when device is online + assert hass.states.get(entity_id) is None + + # Make device online + mock_rpc_device.mock_update() + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state.state == "22.9" + + await hass.services.async_call( + HA_DOMAIN, + SERVICE_UPDATE_ENTITY, + service_data={ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + await hass.async_block_till_done() + + # Entity should be available after update_entity service call + state = hass.states.get(entity_id) + assert state.state == "22.9" + + assert ( + "Entity sensor.test_name_temperature comes from a sleeping device" + in caplog.text + ) + + +async def test_block_sleeping_update_entity_service( + hass: HomeAssistant, mock_block_device, caplog: pytest.LogCaptureFixture +) -> None: + """Test block sleeping device when the update_entity service is used.""" + await async_setup_component(hass, "homeassistant", {}) + + entity_id = f"{SENSOR_DOMAIN}.test_name_temperature" + await init_integration(hass, 1, sleep_period=1000) + + # Sensor should be created when device is online + assert hass.states.get(entity_id) is None + + # Make device online + mock_block_device.mock_update() + await hass.async_block_till_done() + + assert hass.states.get(entity_id).state == "22.1" + + await hass.services.async_call( + HA_DOMAIN, + SERVICE_UPDATE_ENTITY, + service_data={ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + await hass.async_block_till_done() + + # Entity should be available after update_entity service call + state = hass.states.get(entity_id) + assert state.state == "22.1" + + assert ( + "Entity sensor.test_name_temperature comes from a sleeping device" + in caplog.text + )