Use action response in intent_script speech template (#96256)

pull/101660/head^2
Kostas Chatzikokolakis 2023-10-20 17:23:02 +03:00 committed by GitHub
parent 5e483c5573
commit cc0491e85d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 3 deletions

View File

@ -2,7 +2,7 @@
from __future__ import annotations
import logging
from typing import TypedDict
from typing import Any, TypedDict
import voluptuous as vol
@ -144,7 +144,7 @@ class ScriptIntentHandler(intent.IntentHandler):
card: _IntentCardData | None = self.config.get(CONF_CARD)
action: script.Script | None = self.config.get(CONF_ACTION)
is_async_action: bool = self.config[CONF_ASYNC_ACTION]
slots: dict[str, str] = {
slots: dict[str, Any] = {
key: value["value"] for key, value in intent_obj.slots.items()
}
@ -164,7 +164,11 @@ class ScriptIntentHandler(intent.IntentHandler):
action.async_run(slots, intent_obj.context)
)
else:
await action.async_run(slots, intent_obj.context)
action_res = await action.async_run(slots, intent_obj.context)
# if the action returns a response, make it available to the speech/reprompt templates below
if action_res and action_res.service_response is not None:
slots["action_response"] = action_res.service_response
response = intent_obj.create_response()

View File

@ -95,6 +95,38 @@ async def test_intent_script_wait_response(hass: HomeAssistant) -> None:
assert response.card["simple"]["content"] == "Content for Paulus"
async def test_intent_script_service_response(hass: HomeAssistant) -> None:
"""Test intent scripts work."""
calls = async_mock_service(
hass, "test", "service", response={"some_key": "some value"}
)
await async_setup_component(
hass,
"intent_script",
{
"intent_script": {
"HelloWorldServiceResponse": {
"action": [
{"service": "test.service", "response_variable": "result"},
{"stop": "", "response_variable": "result"},
],
"speech": {
"text": "The service returned {{ action_response.some_key }}"
},
}
}
},
)
response = await intent.async_handle(hass, "test", "HelloWorldServiceResponse")
assert len(calls) == 1
assert calls[0].return_response
assert response.speech["plain"]["speech"] == "The service returned some value"
async def test_intent_script_falsy_reprompt(hass: HomeAssistant) -> None:
"""Test intent scripts work."""
calls = async_mock_service(hass, "test", "service")