diff --git a/homeassistant/components/intent/__init__.py b/homeassistant/components/intent/__init__.py index 001f2515ebf..85fdf5c88c3 100644 --- a/homeassistant/components/intent/__init__.py +++ b/homeassistant/components/intent/__init__.py @@ -239,6 +239,8 @@ class GetStateIntentHandler(intent.IntentHandler): vol.Optional("domain"): vol.All(cv.ensure_list, [cv.string]), vol.Optional("device_class"): vol.All(cv.ensure_list, [cv.string]), vol.Optional("state"): vol.All(cv.ensure_list, [cv.string]), + vol.Optional("preferred_area_id"): cv.string, + vol.Optional("preferred_floor_id"): cv.string, } async def async_handle(self, intent_obj: intent.Intent) -> intent.IntentResponse: @@ -280,7 +282,13 @@ class GetStateIntentHandler(intent.IntentHandler): device_classes=device_classes, assistant=intent_obj.assistant, ) - match_result = intent.async_match_targets(hass, match_constraints) + match_preferences = intent.MatchTargetsPreferences( + area_id=slots.get("preferred_area_id", {}).get("value"), + floor_id=slots.get("preferred_floor_id", {}).get("value"), + ) + match_result = intent.async_match_targets( + hass, match_constraints, match_preferences + ) if ( (not match_result.is_match) and (match_result.no_match_reason is not None) diff --git a/tests/components/conversation/test_default_agent.py b/tests/components/conversation/test_default_agent.py index 729ef004d9e..8eef4215fd3 100644 --- a/tests/components/conversation/test_default_agent.py +++ b/tests/components/conversation/test_default_agent.py @@ -2680,3 +2680,71 @@ async def test_config_sentences_priority( data = result.as_dict() assert data["response"]["response_type"] == "action_done" assert data["response"]["speech"]["plain"]["speech"] == "custom response" + + +async def test_query_same_name_different_areas( + hass: HomeAssistant, + init_components, + area_registry: ar.AreaRegistry, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, +) -> None: + """Test asking a question about entities with the same name in different areas.""" + entry = MockConfigEntry(domain="test") + entry.add_to_hass(hass) + + kitchen_device = device_registry.async_get_or_create( + config_entry_id=entry.entry_id, + connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + ) + + kitchen_area = area_registry.async_create("kitchen") + device_registry.async_update_device(kitchen_device.id, area_id=kitchen_area.id) + + kitchen_light = entity_registry.async_get_or_create( + "light", + "demo", + "1234", + ) + entity_registry.async_update_entity( + kitchen_light.entity_id, area_id=kitchen_area.id + ) + hass.states.async_set( + kitchen_light.entity_id, + "on", + attributes={ATTR_FRIENDLY_NAME: "overhead light"}, + ) + + bedroom_area = area_registry.async_create("bedroom") + bedroom_light = entity_registry.async_get_or_create( + "light", + "demo", + "5678", + ) + entity_registry.async_update_entity( + bedroom_light.entity_id, area_id=bedroom_area.id + ) + hass.states.async_set( + bedroom_light.entity_id, + "off", + attributes={ATTR_FRIENDLY_NAME: "overhead light"}, + ) + + # Should fail without a preferred area (duplicate name) + result = await conversation.async_converse( + hass, "is the overhead light on?", None, Context(), None + ) + assert result.response.response_type == intent.IntentResponseType.ERROR + + # Succeeds using area from device (kitchen) + result = await conversation.async_converse( + hass, + "is the overhead light on?", + None, + Context(), + None, + device_id=kitchen_device.id, + ) + assert result.response.response_type == intent.IntentResponseType.QUERY_ANSWER + assert len(result.response.matched_states) == 1 + assert result.response.matched_states[0].entity_id == kitchen_light.entity_id