mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 00:37:13 +00:00
Migrate Alexa smart home to registry (#9616)
* Migrate Alexa smart home to registry * Fix tests
This commit is contained in:
parent
44838937d1
commit
2df433eb0a
@ -6,7 +6,9 @@ from uuid import uuid4
|
|||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_SUPPORTED_FEATURES, ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF)
|
ATTR_SUPPORTED_FEATURES, ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF)
|
||||||
from homeassistant.components import switch, light
|
from homeassistant.components import switch, light
|
||||||
|
from homeassistant.util.decorator import Registry
|
||||||
|
|
||||||
|
HANDLERS = Registry()
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
ATTR_HEADER = 'header'
|
ATTR_HEADER = 'header'
|
||||||
@ -27,27 +29,13 @@ MAPPING_COMPONENT = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def mapping_api_function(name):
|
|
||||||
"""Return function pointer to api function for name.
|
|
||||||
|
|
||||||
Async friendly.
|
|
||||||
"""
|
|
||||||
mapping = {
|
|
||||||
'DiscoverAppliancesRequest': async_api_discovery,
|
|
||||||
'TurnOnRequest': async_api_turn_on,
|
|
||||||
'TurnOffRequest': async_api_turn_off,
|
|
||||||
'SetPercentageRequest': async_api_set_percentage,
|
|
||||||
}
|
|
||||||
return mapping.get(name, None)
|
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_handle_message(hass, message):
|
def async_handle_message(hass, message):
|
||||||
"""Handle incoming API messages."""
|
"""Handle incoming API messages."""
|
||||||
assert int(message[ATTR_HEADER][ATTR_PAYLOAD_VERSION]) == 2
|
assert int(message[ATTR_HEADER][ATTR_PAYLOAD_VERSION]) == 2
|
||||||
|
|
||||||
# Do we support this API request?
|
# Do we support this API request?
|
||||||
funct_ref = mapping_api_function(message[ATTR_HEADER][ATTR_NAME])
|
funct_ref = HANDLERS.get(message[ATTR_HEADER][ATTR_NAME])
|
||||||
if not funct_ref:
|
if not funct_ref:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Unsupported API request %s", message[ATTR_HEADER][ATTR_NAME])
|
"Unsupported API request %s", message[ATTR_HEADER][ATTR_NAME])
|
||||||
@ -64,7 +52,7 @@ def api_message(name, namespace, payload=None):
|
|||||||
payload = payload or {}
|
payload = payload or {}
|
||||||
return {
|
return {
|
||||||
ATTR_HEADER: {
|
ATTR_HEADER: {
|
||||||
ATTR_MESSAGE_ID: uuid4(),
|
ATTR_MESSAGE_ID: str(uuid4()),
|
||||||
ATTR_NAME: name,
|
ATTR_NAME: name,
|
||||||
ATTR_NAMESPACE: namespace,
|
ATTR_NAMESPACE: namespace,
|
||||||
ATTR_PAYLOAD_VERSION: '2',
|
ATTR_PAYLOAD_VERSION: '2',
|
||||||
@ -81,6 +69,7 @@ def api_error(request, exc='DriverInternalError'):
|
|||||||
return api_message(exc, request[ATTR_HEADER][ATTR_NAMESPACE])
|
return api_message(exc, request[ATTR_HEADER][ATTR_NAMESPACE])
|
||||||
|
|
||||||
|
|
||||||
|
@HANDLERS.register('DiscoverAppliancesRequest')
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_api_discovery(hass, request):
|
def async_api_discovery(hass, request):
|
||||||
"""Create a API formatted discovery response.
|
"""Create a API formatted discovery response.
|
||||||
@ -146,6 +135,7 @@ def extract_entity(funct):
|
|||||||
return async_api_entity_wrapper
|
return async_api_entity_wrapper
|
||||||
|
|
||||||
|
|
||||||
|
@HANDLERS.register('TurnOnRequest')
|
||||||
@extract_entity
|
@extract_entity
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_api_turn_on(hass, request, entity):
|
def async_api_turn_on(hass, request, entity):
|
||||||
@ -157,6 +147,7 @@ def async_api_turn_on(hass, request, entity):
|
|||||||
return api_message('TurnOnConfirmation', 'Alexa.ConnectedHome.Control')
|
return api_message('TurnOnConfirmation', 'Alexa.ConnectedHome.Control')
|
||||||
|
|
||||||
|
|
||||||
|
@HANDLERS.register('TurnOffRequest')
|
||||||
@extract_entity
|
@extract_entity
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_api_turn_off(hass, request, entity):
|
def async_api_turn_off(hass, request, entity):
|
||||||
@ -168,6 +159,7 @@ def async_api_turn_off(hass, request, entity):
|
|||||||
return api_message('TurnOffConfirmation', 'Alexa.ConnectedHome.Control')
|
return api_message('TurnOffConfirmation', 'Alexa.ConnectedHome.Control')
|
||||||
|
|
||||||
|
|
||||||
|
@HANDLERS.register('SetPercentageRequest')
|
||||||
@extract_entity
|
@extract_entity
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_api_set_percentage(hass, request, entity):
|
def async_api_set_percentage(hass, request, entity):
|
||||||
|
@ -19,19 +19,6 @@ def test_create_api_message():
|
|||||||
assert msg['payload'] == {}
|
assert msg['payload'] == {}
|
||||||
|
|
||||||
|
|
||||||
def test_mapping_api_funct():
|
|
||||||
"""Test function ref from mapping function."""
|
|
||||||
assert smart_home.mapping_api_function('notExists') is None
|
|
||||||
assert smart_home.mapping_api_function('DiscoverAppliancesRequest') == \
|
|
||||||
smart_home.async_api_discovery
|
|
||||||
assert smart_home.mapping_api_function('TurnOnRequest') == \
|
|
||||||
smart_home.async_api_turn_on
|
|
||||||
assert smart_home.mapping_api_function('TurnOffRequest') == \
|
|
||||||
smart_home.async_api_turn_off
|
|
||||||
assert smart_home.mapping_api_function('SetPercentageRequest') == \
|
|
||||||
smart_home.async_api_set_percentage
|
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_wrong_version(hass):
|
def test_wrong_version(hass):
|
||||||
"""Test with wrong version."""
|
"""Test with wrong version."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user