diff --git a/homeassistant/components/intent_script/__init__.py b/homeassistant/components/intent_script/__init__.py index b3e7c44f086..d5bec0573b8 100644 --- a/homeassistant/components/intent_script/__init__.py +++ b/homeassistant/components/intent_script/__init__.py @@ -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() diff --git a/tests/components/intent_script/test_init.py b/tests/components/intent_script/test_init.py index a68b2a9be24..fe694607def 100644 --- a/tests/components/intent_script/test_init.py +++ b/tests/components/intent_script/test_init.py @@ -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")