diff --git a/homeassistant/helpers/intent.py b/homeassistant/helpers/intent.py index 81ae173a6ec7..5ade7f453ed5 100644 --- a/homeassistant/helpers/intent.py +++ b/homeassistant/helpers/intent.py @@ -589,8 +589,8 @@ def async_match_targets( # noqa: C901 filtered_by_domain = False if not states: - # Get all states and filter by domain - states = hass.states.async_all(constraints.domains) + # Get all states and filter by domain (an empty set is no filter, as below) + states = hass.states.async_all(constraints.domains or None) filtered_by_domain = True if not states: return MatchTargetsResult(False, MatchFailedReason.DOMAIN) @@ -1110,8 +1110,9 @@ class DynamicServiceIntentHandler(IntentHandler): domains: set[str] | None = self.required_domains device_classes: set[str] | None = None - if "domain" in slots: - domains = set(slots["domain"]["value"]) + # An empty list (some LLMs send "domain": []) keeps required_domains + if (domain_slot := slots.get("domain")) and domain_slot["value"]: + domains = set(domain_slot["value"]) if "device_class" in slots: device_classes = set(slots["device_class"]["value"]) diff --git a/tests/components/intent/test_init.py b/tests/components/intent/test_init.py index c2f4db234b90..9ef4c59a08c5 100644 --- a/tests/components/intent/test_init.py +++ b/tests/components/intent/test_init.py @@ -705,6 +705,23 @@ async def test_get_state_intent( ) +async def test_get_state_intent_empty_domain_list(hass: HomeAssistant) -> None: + """Test that an empty domain list in HassGetState means no domain filter.""" + assert await async_setup_component(hass, "homeassistant", {}) + assert await async_setup_component(hass, DOMAIN, {}) + hass.states.async_set("switch.bedroom", "off") + + result = await intent.async_handle( + hass, + "test", + "HassGetState", + {"name": {"value": "bedroom"}, "domain": {"value": []}}, + ) + + assert result.response_type is intent.IntentResponseType.QUERY_ANSWER + assert [s.entity_id for s in result.matched_states] == ["switch.bedroom"] + + async def test_set_position_intent_unsupported_domain(hass: HomeAssistant) -> None: """Test that HassSetPosition intent fails with unsupported domain.""" assert await async_setup_component(hass, "homeassistant", {}) diff --git a/tests/helpers/test_intent.py b/tests/helpers/test_intent.py index c72b6b3b948f..51fbeae5d624 100644 --- a/tests/helpers/test_intent.py +++ b/tests/helpers/test_intent.py @@ -890,6 +890,62 @@ async def test_service_intent_handler_required_domains(hass: HomeAssistant) -> N ) +async def test_service_handler_empty_domain_list(hass: HomeAssistant) -> None: + """Test that an empty domain list (sent by some LLMs) means no domain filter.""" + hass.states.async_set("light.kitchen", "off") + hass.states.async_set("switch.bedroom", "off") + + calls = async_mock_service(hass, "homeassistant", "turn_on") + intent.async_register( + hass, intent.ServiceIntentHandler("TestType", "homeassistant", "turn_on") + ) + intent.async_register( + hass, + intent.ServiceIntentHandler( + "LightType", "homeassistant", "turn_on", required_domains={"light"} + ), + ) + + result = await intent.async_handle( + hass, + "test", + "TestType", + slots={"name": {"value": "bedroom"}, "domain": {"value": []}}, + ) + assert result.response_type is intent.IntentResponseType.ACTION_DONE + assert calls[-1].data == {"entity_id": "switch.bedroom"} + + result = await intent.async_handle( + hass, + "test", + "LightType", + slots={"name": {"value": "kitchen"}, "domain": {"value": []}}, + ) + assert result.response_type is intent.IntentResponseType.ACTION_DONE + assert calls[-1].data == {"entity_id": "light.kitchen"} + + # required_domains still applies + with pytest.raises(intent.MatchFailedError) as err: + await intent.async_handle( + hass, + "test", + "LightType", + slots={"name": {"value": "bedroom"}, "domain": {"value": []}}, + ) + assert err.value.constraints.domains == {"light"} + + +async def test_async_match_targets_empty_domains(hass: HomeAssistant) -> None: + """Test that an empty domain set is no domain filter.""" + hass.states.async_set("switch.bedroom", "off") + + result = intent.async_match_targets( + hass, intent.MatchTargetsConstraints(name="bedroom", domains=set()) + ) + assert result.is_match + assert [s.entity_id for s in result.states] == ["switch.bedroom"] + + async def test_service_handler_empty_strings(hass: HomeAssistant) -> None: """Test that passing empty strings for filters fails in ServiceIntentHandler.""" handler = intent.ServiceIntentHandler(