Skip to content

Commit

Permalink
Respond to Alexa scene activation correctly
Browse files Browse the repository at this point in the history
The API documentation[1] specifies that Alexa.SceneController Activate
must get a ActivationStarted response. Responding with just a `Response`
will elicit a "Hmm... $scene is not responding" from Alexa.

[1]: https://developer.amazon.com/docs/smarthome/provide-scenes-in-a-smart-home-skill.html
  • Loading branch information
bitglue committed Jan 23, 2018
1 parent d478517 commit f35e711
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
46 changes: 45 additions & 1 deletion homeassistant/components/alexa/smart_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
import logging
import math
from datetime import datetime
from uuid import uuid4

from homeassistant.components import (
Expand Down Expand Up @@ -70,6 +71,39 @@
}


class _Cause(object):
"""Possible causes for property changes.
https://developer.amazon.com/docs/smarthome/state-reporting-for-a-smart-home-skill.html#cause-object
"""

# Indicates that the event was caused by a customer interaction with an
# application. For example, a customer switches on a light, or locks a door
# using the Alexa app or an app provided by a device vendor.
APP_INTERACTION = 'APP_INTERACTION'

# Indicates that the event was caused by a physical interaction with an
# endpoint. For example manually switching on a light or manually locking a
# door lock
PHYSICAL_INTERACTION = 'PHYSICAL_INTERACTION'

# Indicates that the event was caused by the periodic poll of an appliance,
# which found a change in value. For example, you might poll a temperature
# sensor every hour, and send the updated temperature to Alexa.
PERIODIC_POLL = 'PERIODIC_POLL'

# Indicates that the event was caused by the application of a device rule.
# For example, a customer configures a rule to switch on a light if a
# motion sensor detects motion. In this case, Alexa receives an event from
# the motion sensor, and another event from the light to indicate that its
# state change was caused by the rule.
RULE_TRIGGER = 'RULE_TRIGGER'

# Indicates that the event was caused by a voice interaction with Alexa.
# For example a user speaking to their Echo device.
VOICE_INTERACTION = 'VOICE_INTERACTION'


class Config:
"""Hold the configuration for Alexa."""

Expand Down Expand Up @@ -400,7 +434,17 @@ def async_api_activate(hass, config, request, entity):
ATTR_ENTITY_ID: entity.entity_id
}, blocking=False)

return api_message(request)
payload = {
'cause': {'type': _Cause.VOICE_INTERACTION},
'timestamp': '%sZ' % (datetime.utcnow().isoformat(),)
}

return api_message(
request,
name='ActivationStarted',
namespace='Alexa.SceneController',
payload=payload,
)


@HANDLERS.register(('Alexa.PercentageController', 'SetPercentage'))
Expand Down
4 changes: 3 additions & 1 deletion tests/components/alexa/test_smart_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,9 @@ def test_api_activate(hass, domain):

assert len(call) == 1
assert call[0].data['entity_id'] == '{}.test'.format(domain)
assert msg['header']['name'] == 'Response'
assert msg['header']['name'] == 'ActivationStarted'
assert msg['payload']['cause']['type'] == 'VOICE_INTERACTION'
assert 'timestamp' in msg['payload']


@asyncio.coroutine
Expand Down

0 comments on commit f35e711

Please sign in to comment.