From d3c5684cd05538e335c20ba9afa1372be0f6308c Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Sat, 28 Jun 2025 22:40:58 +0300 Subject: [PATCH] Fix Shelly Block entity removal (#147694) --- homeassistant/components/shelly/entity.py | 5 ++- tests/components/shelly/test_switch.py | 45 +++++++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index 587eb00b979..b80ac877a84 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -86,7 +86,10 @@ def async_setup_block_attribute_entities( coordinator.device.settings, block ): domain = sensor_class.__module__.split(".")[-1] - unique_id = f"{coordinator.mac}-{block.description}-{sensor_id}" + unique_id = sensor_class( + coordinator, block, sensor_id, description + ).unique_id + LOGGER.debug("Removing Shelly entity with unique_id: %s", unique_id) async_remove_shelly_entity(hass, domain, unique_id) else: entities.append( diff --git a/tests/components/shelly/test_switch.py b/tests/components/shelly/test_switch.py index 3234e3eb0b9..f1866d83e2a 100644 --- a/tests/components/shelly/test_switch.py +++ b/tests/components/shelly/test_switch.py @@ -40,6 +40,8 @@ from . import ( from tests.common import async_fire_time_changed, mock_restore_cache +DEVICE_BLOCK_ID = 4 +LIGHT_BLOCK_ID = 2 RELAY_BLOCK_ID = 0 GAS_VALVE_BLOCK_ID = 6 MOTION_BLOCK_ID = 3 @@ -326,14 +328,51 @@ async def test_block_device_mode_roller( async def test_block_device_app_type_light( - hass: HomeAssistant, mock_block_device: Mock, monkeypatch: pytest.MonkeyPatch + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + mock_block_device: Mock, + monkeypatch: pytest.MonkeyPatch, ) -> None: """Test block device in app type set to light mode.""" + switch_entity_id = "switch.test_name_channel_1" + light_entity_id = "light.test_name_channel_1" + + # Remove light blocks to prevent light entity creation + monkeypatch.setattr(mock_block_device.blocks[LIGHT_BLOCK_ID], "type", "sensor") + monkeypatch.delattr(mock_block_device.blocks[RELAY_BLOCK_ID], "red") + monkeypatch.delattr(mock_block_device.blocks[RELAY_BLOCK_ID], "green") + monkeypatch.delattr(mock_block_device.blocks[RELAY_BLOCK_ID], "blue") + monkeypatch.delattr(mock_block_device.blocks[RELAY_BLOCK_ID], "mode") + monkeypatch.delattr(mock_block_device.blocks[RELAY_BLOCK_ID], "gain") + monkeypatch.delattr(mock_block_device.blocks[RELAY_BLOCK_ID], "brightness") + monkeypatch.delattr(mock_block_device.blocks[RELAY_BLOCK_ID], "effect") + monkeypatch.delattr(mock_block_device.blocks[RELAY_BLOCK_ID], "colorTemp") + + await init_integration(hass, 1) + + # Entity is created as switch + assert hass.states.get(switch_entity_id) + assert hass.states.get(light_entity_id) is None + + # Generate config change from switch to light + monkeypatch.setattr(mock_block_device.blocks[DEVICE_BLOCK_ID], "cfgChanged", 1) + mock_block_device.mock_update() + monkeypatch.setitem( mock_block_device.settings["relays"][RELAY_BLOCK_ID], "appliance_type", "light" ) - await init_integration(hass, 1) - assert hass.states.get("switch.test_name_channel_1") is None + monkeypatch.setattr(mock_block_device.blocks[DEVICE_BLOCK_ID], "cfgChanged", 2) + mock_block_device.mock_update() + await hass.async_block_till_done() + + # Wait for debouncer + freezer.tick(timedelta(seconds=ENTRY_RELOAD_COOLDOWN)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + # Switch entity should be removed and light entity created + assert hass.states.get(switch_entity_id) is None + assert hass.states.get(light_entity_id) async def test_rpc_device_services(