From beab6e2e5fcc5f9b254ab92c011ad39328072fbf Mon Sep 17 00:00:00 2001 From: w35l3y Date: Fri, 3 Jun 2022 10:32:22 -0300 Subject: [PATCH] Fix ended session when there isn't any response from the user (#72218) * Fix end session when there isn't any response This PR fixes #72153 * Added test case as requested https://github.com/home-assistant/core/pull/72218#discussion_r881584812 --- homeassistant/components/alexa/intent.py | 10 ++-- .../components/intent_script/__init__.py | 12 +++-- tests/components/alexa/test_intent.py | 7 ++- tests/components/intent_script/test_init.py | 48 +++++++++++++++++++ 4 files changed, 65 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/alexa/intent.py b/homeassistant/components/alexa/intent.py index 7352bbd995a..ef145a9ceb8 100644 --- a/homeassistant/components/alexa/intent.py +++ b/homeassistant/components/alexa/intent.py @@ -127,11 +127,6 @@ async def async_handle_message(hass, message): @HANDLERS.register("SessionEndedRequest") -async def async_handle_session_end(hass, message): - """Handle a session end request.""" - return None - - @HANDLERS.register("IntentRequest") @HANDLERS.register("LaunchRequest") async def async_handle_intent(hass, message): @@ -151,6 +146,11 @@ async def async_handle_intent(hass, message): intent_name = ( message.get("session", {}).get("application", {}).get("applicationId") ) + elif req["type"] == "SessionEndedRequest": + app_id = message.get("session", {}).get("application", {}).get("applicationId") + intent_name = f"{app_id}.{req['type']}" + alexa_response.variables["reason"] = req["reason"] + alexa_response.variables["error"] = req.get("error") else: intent_name = alexa_intent_info["name"] diff --git a/homeassistant/components/intent_script/__init__.py b/homeassistant/components/intent_script/__init__.py index d14aaf5a68b..e8c5c580708 100644 --- a/homeassistant/components/intent_script/__init__.py +++ b/homeassistant/components/intent_script/__init__.py @@ -99,11 +99,13 @@ class ScriptIntentHandler(intent.IntentHandler): speech[CONF_TYPE], ) - if reprompt is not None and reprompt[CONF_TEXT].template: - response.async_set_reprompt( - reprompt[CONF_TEXT].async_render(slots, parse_result=False), - reprompt[CONF_TYPE], - ) + if reprompt is not None: + text_reprompt = reprompt[CONF_TEXT].async_render(slots, parse_result=False) + if text_reprompt: + response.async_set_reprompt( + text_reprompt, + reprompt[CONF_TYPE], + ) if card is not None: response.async_set_card( diff --git a/tests/components/alexa/test_intent.py b/tests/components/alexa/test_intent.py index f15fa860c7b..9c71bc32e4d 100644 --- a/tests/components/alexa/test_intent.py +++ b/tests/components/alexa/test_intent.py @@ -490,8 +490,11 @@ async def test_intent_session_ended_request(alexa_client): req = await _intent_req(alexa_client, data) assert req.status == HTTPStatus.OK - text = await req.text() - assert text == "" + data = await req.json() + assert ( + data["response"]["outputSpeech"]["text"] + == "This intent is not yet configured within Home Assistant." + ) async def test_intent_from_built_in_intent_library(alexa_client): diff --git a/tests/components/intent_script/test_init.py b/tests/components/intent_script/test_init.py index 6f345522e63..39f865f4832 100644 --- a/tests/components/intent_script/test_init.py +++ b/tests/components/intent_script/test_init.py @@ -38,6 +38,8 @@ async def test_intent_script(hass): assert response.speech["plain"]["speech"] == "Good morning Paulus" + assert not (response.reprompt) + assert response.card["simple"]["title"] == "Hello Paulus" assert response.card["simple"]["content"] == "Content for Paulus" @@ -85,3 +87,49 @@ async def test_intent_script_wait_response(hass): assert response.card["simple"]["title"] == "Hello Paulus" assert response.card["simple"]["content"] == "Content for Paulus" + + +async def test_intent_script_falsy_reprompt(hass): + """Test intent scripts work.""" + calls = async_mock_service(hass, "test", "service") + + await async_setup_component( + hass, + "intent_script", + { + "intent_script": { + "HelloWorld": { + "action": { + "service": "test.service", + "data_template": {"hello": "{{ name }}"}, + }, + "card": { + "title": "Hello {{ name }}", + "content": "Content for {{ name }}", + }, + "speech": { + "type": "ssml", + "text": 'Good morning {{ name }}', + }, + "reprompt": {"text": "{{ null }}"}, + } + } + }, + ) + + response = await intent.async_handle( + hass, "test", "HelloWorld", {"name": {"value": "Paulus"}} + ) + + assert len(calls) == 1 + assert calls[0].data["hello"] == "Paulus" + + assert ( + response.speech["ssml"]["speech"] + == 'Good morning Paulus' + ) + + assert not (response.reprompt) + + assert response.card["simple"]["title"] == "Hello Paulus" + assert response.card["simple"]["content"] == "Content for Paulus"