Add entity matching to intent_script (#120973)

This commit is contained in:
Artur Pragacz
2024-08-19 22:01:35 +02:00
committed by GitHub
parent 254aa8c9ea
commit 407e4f6ca2
2 changed files with 138 additions and 2 deletions

View File

@@ -6,7 +6,12 @@ from homeassistant import config as hass_config
from homeassistant.components.intent_script import DOMAIN
from homeassistant.const import SERVICE_RELOAD
from homeassistant.core import HomeAssistant
from homeassistant.helpers import intent
from homeassistant.helpers import (
area_registry as ar,
entity_registry as er,
floor_registry as fr,
intent,
)
from homeassistant.setup import async_setup_component
from tests.common import async_mock_service, get_fixture_path
@@ -197,6 +202,84 @@ async def test_intent_script_falsy_reprompt(hass: HomeAssistant) -> None:
assert response.card["simple"]["content"] == "Content for Paulus"
async def test_intent_script_targets(
hass: HomeAssistant,
area_registry: ar.AreaRegistry,
entity_registry: er.EntityRegistry,
floor_registry: fr.FloorRegistry,
) -> None:
"""Test intent scripts work."""
calls = async_mock_service(hass, "test", "service")
await async_setup_component(
hass,
"intent_script",
{
"intent_script": {
"Targets": {
"description": "Intent to control a test service.",
"action": {
"service": "test.service",
"data_template": {
"targets": "{{ targets if targets is defined }}",
},
},
"speech": {
"text": "{{ targets.entities[0] if targets is defined }}"
},
}
}
},
)
floor_1 = floor_registry.async_create("first floor")
kitchen = area_registry.async_get_or_create("kitchen")
area_registry.async_update(kitchen.id, floor_id=floor_1.floor_id)
entity_registry.async_get_or_create(
"light", "demo", "1234", suggested_object_id="kitchen"
)
entity_registry.async_update_entity("light.kitchen", area_id=kitchen.id)
hass.states.async_set("light.kitchen", "off")
response = await intent.async_handle(
hass,
"test",
"Targets",
{"name": {"value": "kitchen"}, "domain": {"value": "light"}},
)
assert len(calls) == 1
assert calls[0].data["targets"] == {"entities": ["light.kitchen"]}
assert response.speech["plain"]["speech"] == "light.kitchen"
calls.clear()
response = await intent.async_handle(
hass,
"test",
"Targets",
{
"area": {"value": "kitchen"},
"floor": {"value": "first floor"},
},
)
assert len(calls) == 1
assert calls[0].data["targets"] == {
"entities": ["light.kitchen"],
"areas": ["kitchen"],
"floors": ["first_floor"],
}
calls.clear()
response = await intent.async_handle(
hass,
"test",
"Targets",
{"device_class": {"value": "door"}},
)
assert len(calls) == 1
assert calls[0].data["targets"] == ""
calls.clear()
async def test_reload(hass: HomeAssistant) -> None:
"""Verify we can reload intent config."""