Refactor Alexa API, fix thermostats (#17969)

* Refactor Alexa API to use objects for requests

This introduces _AlexaDirective to stand in for the previous model of passing
basic dict and list data structures to and from handlers. This gives a more
expressive platform for functionality common to most or all handlers.

I had two use cases in mind:

1) Most responses should include current properties. In the case of locks and
thermostats, the response must include the properties or Alexa will give the
user a vague error like "Hmm, $device is not responding." Locks currently work,
but thermostats do not. I wanted a way to automatically include properties in
all responses. This is implemented in a subsequent commit.

2) The previous model had a 1:1 mapping between Alexa endpoints and Home
Assistant entities. This works most of the time, but sometimes it's not so
great. For example, my Z-wave thermostat shows as three devices in Alexa: one
for the temperature sensor, one for the heat, and one for the AC. I'd like to
merge these into one device from Alexa's perspective. I believe this will be
facilitated with the `endpoint` attribute on `_AlexaDirective`.

* Include properties in all Alexa responses

The added _AlexaResponse class provides a richer vocabulary for handlers.

Among that vocabulary is .merge_context_properties(), which is invoked
automatically for any request directed at an endpoint. This adds all supported
properties to the response as recommended by the Alexa API docs, and in some
cases (locks, thermostats at least) the user will get an error "Hmm, $device is
not responding" if properties are not provided in the response.

* Fix setting temperature with Alexa thermostats

Fixes https://github.com/home-assistant/home-assistant/issues/16577
This commit is contained in:
Phil Frost 2018-10-29 22:16:35 -04:00 committed by GitHub
parent 98163504fb
commit e16793013a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 572 additions and 306 deletions

View file

@ -227,11 +227,9 @@ def async_handle_message(hass, cloud, handler_name, payload):
@asyncio.coroutine
def async_handle_alexa(hass, cloud, payload):
"""Handle an incoming IoT message for Alexa."""
if not cloud.alexa_enabled:
return alexa.turned_off_response(payload)
result = yield from alexa.async_handle_message(
hass, cloud.alexa_config, payload)
hass, cloud.alexa_config, payload,
enabled=cloud.alexa_enabled)
return result