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
pull/72986/head
w35l3y 2022-06-03 10:32:22 -03:00 committed by GitHub
parent 88129dbe91
commit beab6e2e5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 12 deletions

View File

@ -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"]

View File

@ -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(

View File

@ -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):

View File

@ -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": '<speak><amazon:effect name="whispered">Good morning {{ name }}</amazon:effect></speak>',
},
"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"]
== '<speak><amazon:effect name="whispered">Good morning Paulus</amazon:effect></speak>'
)
assert not (response.reprompt)
assert response.card["simple"]["title"] == "Hello Paulus"
assert response.card["simple"]["content"] == "Content for Paulus"