Use action response in intent_script speech template (#96256)
parent
5e483c5573
commit
cc0491e85d
|
@ -2,7 +2,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from typing import TypedDict
|
from typing import Any, TypedDict
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
|
@ -144,7 +144,7 @@ class ScriptIntentHandler(intent.IntentHandler):
|
||||||
card: _IntentCardData | None = self.config.get(CONF_CARD)
|
card: _IntentCardData | None = self.config.get(CONF_CARD)
|
||||||
action: script.Script | None = self.config.get(CONF_ACTION)
|
action: script.Script | None = self.config.get(CONF_ACTION)
|
||||||
is_async_action: bool = self.config[CONF_ASYNC_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()
|
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)
|
action.async_run(slots, intent_obj.context)
|
||||||
)
|
)
|
||||||
else:
|
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()
|
response = intent_obj.create_response()
|
||||||
|
|
||||||
|
|
|
@ -95,6 +95,38 @@ async def test_intent_script_wait_response(hass: HomeAssistant) -> None:
|
||||||
assert response.card["simple"]["content"] == "Content for Paulus"
|
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:
|
async def test_intent_script_falsy_reprompt(hass: HomeAssistant) -> None:
|
||||||
"""Test intent scripts work."""
|
"""Test intent scripts work."""
|
||||||
calls = async_mock_service(hass, "test", "service")
|
calls = async_mock_service(hass, "test", "service")
|
||||||
|
|
Loading…
Reference in New Issue