Treat an empty domain list as no domain filter in intents (#195383)

This commit is contained in:
Apertur3
2026-11-30 06:47:29 +00:00
committed by GitHub
parent cd0b8e400f
commit 294f8753c7
3 changed files with 78 additions and 4 deletions
+5 -4
View File
@@ -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"])
+17
View File
@@ -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", {})
+56
View File
@@ -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(