From 5ce0fec43f3262302870564fcedc2eca0c151f24 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 18 Aug 2026 18:11:14 +0200 Subject: [PATCH] Invalidate conversation slot-list cache when entity moves to other device (#179494) --- .../components/conversation/default_agent.py | 2 +- .../conversation/test_default_agent.py | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/conversation/default_agent.py b/homeassistant/components/conversation/default_agent.py index 2f0130a4bca3..70da46d493c4 100644 --- a/homeassistant/components/conversation/default_agent.py +++ b/homeassistant/components/conversation/default_agent.py @@ -82,7 +82,7 @@ _LOGGER = logging.getLogger(__name__) _DEFAULT_ERROR_TEXT = "Sorry, I couldn't understand that" -_ENTITY_REGISTRY_UPDATE_FIELDS = ["aliases", "name", "original_name"] +_ENTITY_REGISTRY_UPDATE_FIELDS = ["aliases", "device_id", "name", "original_name"] _DEVICE_REGISTRY_UPDATE_FIELDS = ["name", "name_by_user"] _DEFAULT_EXPOSED_ATTRIBUTES = {"device_class"} diff --git a/tests/components/conversation/test_default_agent.py b/tests/components/conversation/test_default_agent.py index fcf3212f0608..e4dd8c21ce7e 100644 --- a/tests/components/conversation/test_default_agent.py +++ b/tests/components/conversation/test_default_agent.py @@ -540,6 +540,60 @@ async def test_device_rename_refreshes_slot_list( assert len(calls) == 1 +@pytest.mark.usefixtures("init_components") +async def test_entity_moved_to_device_refreshes_slot_list( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, +) -> None: + """Test moving an entity to another device updates its matchable computed name.""" + config_entry = MockConfigEntry() + config_entry.add_to_hass(hass) + kitchen = device_registry.async_get_or_create( + config_entry_id=config_entry.entry_id, + connections=set(), + identifiers={("demo", "kitchen")}, + name="Kitchen", + ) + bedroom = device_registry.async_get_or_create( + config_entry_id=config_entry.entry_id, + connections=set(), + identifiers={("demo", "bedroom")}, + name="Bedroom", + ) + + light = entity_registry.async_get_or_create( + "light", + "demo", + "1234", + device_id=kitchen.id, + has_entity_name=True, + original_name="Light", + ) + hass.states.async_set(light.entity_id, "off") + expose_entity(hass, light.entity_id, True) + + # Populate the slot list cache: the current computed name matches. + calls = async_mock_service(hass, "light", "turn_on") + result = await conversation.async_converse( + hass, "turn on Kitchen Light", None, Context(), None + ) + assert result.response.response_type is intent.IntentResponseType.ACTION_DONE + assert len(calls) == 1 + + # Moving the light to the bedroom changes its computed name to "Bedroom Light". + entity_registry.async_update_entity(light.entity_id, device_id=bedroom.id) + await hass.async_block_till_done() + + # The new name is now matchable. + calls = async_mock_service(hass, "light", "turn_on") + result = await conversation.async_converse( + hass, "turn on Bedroom Light", None, Context(), None + ) + assert result.response.response_type is intent.IntentResponseType.ACTION_DONE + assert len(calls) == 1 + + @pytest.mark.usefixtures("init_components") async def test_trigger_sentences(hass: HomeAssistant) -> None: """Test registering/unregistering/matching a few trigger sentences."""