From 1adb5040e7836e0d15df67da2e1f47f3403125dc Mon Sep 17 00:00:00 2001 From: Steve Rhoades Date: Tue, 1 Aug 2017 22:53:36 -0700 Subject: [PATCH] Feature alexa launch request (#8730) * Add support for LaunchRequest alexa intent * Support LaunchRequest for multiple skills * formatting * adding tests to cover launch request * formatting --- homeassistant/components/alexa.py | 15 +++++------ tests/components/test_alexa.py | 43 +++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/alexa.py b/homeassistant/components/alexa.py index 3547e86dad1..25b6537e255 100644 --- a/homeassistant/components/alexa.py +++ b/homeassistant/components/alexa.py @@ -128,19 +128,18 @@ class AlexaIntentsView(http.HomeAssistantView): alexa_intent_info = req.get('intent') alexa_response = AlexaResponse(hass, alexa_intent_info) - if req_type == 'LaunchRequest': - alexa_response.add_speech( - SpeechType.plaintext, - "Hello, and welcome to the future. How may I help?") - return self.json(alexa_response) - - if req_type != 'IntentRequest': + if req_type != 'IntentRequest' and req_type != 'LaunchRequest': _LOGGER.warning('Received unsupported request: %s', req_type) return self.json_message( 'Received unsupported request: {}'.format(req_type), HTTP_BAD_REQUEST) - intent_name = alexa_intent_info['name'] + if req_type == 'LaunchRequest': + intent_name = data.get('session', {}) \ + .get('application', {}) \ + .get('applicationId') + else: + intent_name = alexa_intent_info['name'] try: intent_response = yield from intent.async_handle( diff --git a/tests/components/test_alexa.py b/tests/components/test_alexa.py index 4e082441064..3789e7ab615 100644 --- a/tests/components/test_alexa.py +++ b/tests/components/test_alexa.py @@ -100,6 +100,12 @@ def alexa_client(loop, hass, test_client): }, "entity_id": "switch.test", } + }, + APPLICATION_ID: { + "speech": { + "type": "plain", + "text": "LaunchRequest has been received.", + } } } })) @@ -140,8 +146,41 @@ def test_intent_launch_request(alexa_client): } req = yield from _intent_req(alexa_client, data) assert req.status == 200 - resp = yield from req.json() - assert "outputSpeech" in resp["response"] + data = yield from req.json() + text = data.get("response", {}).get("outputSpeech", + {}).get("text") + assert text == "LaunchRequest has been received." + + +@asyncio.coroutine +def test_intent_launch_request_not_configured(alexa_client): + """Test the launch of a request.""" + data = { + "version": "1.0", + "session": { + "new": True, + "sessionId": SESSION_ID, + "application": { + "applicationId": + 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00000' + }, + "attributes": {}, + "user": { + "userId": "amzn1.account.AM3B00000000000000000000000" + } + }, + "request": { + "type": "LaunchRequest", + "requestId": REQUEST_ID, + "timestamp": "2015-05-13T12:34:56Z" + } + } + req = yield from _intent_req(alexa_client, data) + assert req.status == 200 + data = yield from req.json() + text = data.get("response", {}).get("outputSpeech", + {}).get("text") + assert text == "This intent is not yet configured within Home Assistant." @asyncio.coroutine