Fire events when Google Assistant commands come in #15139 (#20204)

This commit is contained in:
Penny Wood authored and Paulus Schoutsen committed 2019-02-27 11:33:34 -08:00
1 parent 9b3a3fc1ac
commit b87eb9d79e
4 files changed
+134 -9

No files matched your search

@@ -22,6 +22,8 @@ from .const import (
CONF_EXPOSE, CONF_ALIASES, CONF_ROOM_HINT, CONF_ALLOW_UNLOCK,
DEFAULT_ALLOW_UNLOCK
)
from .const import EVENT_COMMAND_RECEIVED, EVENT_SYNC_RECEIVED # noqa: F401
from .const import EVENT_QUERY_RECEIVED # noqa: F401
from .http import async_register_http
_LOGGER = logging.getLogger(__name__)
@@ -42,3 +42,8 @@ ERR_NOT_SUPPORTED = "notSupported"
ERR_PROTOCOL_ERROR = 'protocolError'
ERR_UNKNOWN_ERROR = 'unknownError'
ERR_FUNCTION_NOT_SUPPORTED = 'functionNotSupported'
# Event types
EVENT_COMMAND_RECEIVED = 'google_assistant_command_received'
EVENT_QUERY_RECEIVED = 'google_assistant_query_received'
EVENT_SYNC_RECEIVED = 'google_assistant_sync_received'
@@ -8,7 +8,7 @@ from homeassistant.util.decorator import Registry
from homeassistant.core import callback
from homeassistant.const import (
CLOUD_NEVER_EXPOSED_ENTITIES, CONF_NAME, STATE_UNAVAILABLE,
ATTR_SUPPORTED_FEATURES
ATTR_SUPPORTED_FEATURES, ATTR_ENTITY_ID,
)
from homeassistant.components import (
climate,
@@ -32,7 +32,8 @@ from .const import (
TYPE_THERMOSTAT, TYPE_FAN,
CONF_ALIASES, CONF_ROOM_HINT,
ERR_FUNCTION_NOT_SUPPORTED, ERR_PROTOCOL_ERROR, ERR_DEVICE_OFFLINE,
ERR_UNKNOWN_ERROR
ERR_UNKNOWN_ERROR,
EVENT_COMMAND_RECEIVED, EVENT_SYNC_RECEIVED, EVENT_QUERY_RECEIVED
)
from .helpers import SmartHomeError
@@ -214,7 +215,8 @@ async def _process(hass, config, message):
}
try:
result = await handler(hass, config, inputs[0].get('payload'))
result = await handler(hass, config, request_id,
inputs[0].get('payload'))
except SmartHomeError as err:
return {
'requestId': request_id,
@@ -233,11 +235,15 @@ async def _process(hass, config, message):
@HANDLERS.register('action.devices.SYNC')
async def async_devices_sync(hass, config, payload):
async def async_devices_sync(hass, config, request_id, payload):
"""Handle action.devices.SYNC request.
https://developers.google.com/actions/smarthome/create-app#actiondevicessync
"""
hass.bus.async_fire(EVENT_SYNC_RECEIVED, {
'request_id': request_id
})
devices = []
for state in hass.states.async_all():
if state.entity_id in CLOUD_NEVER_EXPOSED_ENTITIES:
@@ -255,14 +261,16 @@ async def async_devices_sync(hass, config, payload):
devices.append(serialized)
return {
response = {
'agentUserId': config.agent_user_id,
'devices': devices,
}
return response
@HANDLERS.register('action.devices.QUERY')
async def async_devices_query(hass, config, payload):
async def async_devices_query(hass, config, request_id, payload):
"""Handle action.devices.QUERY request.
https://developers.google.com/actions/smarthome/create-app#actiondevicesquery
@@ -272,6 +280,11 @@ async def async_devices_query(hass, config, payload):
devid = device['id']
state = hass.states.get(devid)
hass.bus.async_fire(EVENT_QUERY_RECEIVED, {
'request_id': request_id,
ATTR_ENTITY_ID: devid,
})
if not state:
# If we can't find a state, the device is offline
devices[devid] = {'online': False}
@@ -283,7 +296,7 @@ async def async_devices_query(hass, config, payload):
@HANDLERS.register('action.devices.EXECUTE')
async def handle_devices_execute(hass, config, payload):
async def handle_devices_execute(hass, config, request_id, payload):
"""Handle action.devices.EXECUTE request.
https://developers.google.com/actions/smarthome/create-app#actiondevicesexecute
@@ -296,6 +309,12 @@ async def handle_devices_execute(hass, config, payload):
command['execution']):
entity_id = device['id']
hass.bus.async_fire(EVENT_COMMAND_RECEIVED, {
'request_id': request_id,
ATTR_ENTITY_ID: entity_id,
'execution': execution
})
# Happens if error occurred. Skip entity for further processing
if entity_id in results:
continue
@@ -341,7 +360,7 @@ async def handle_devices_execute(hass, config, payload):
@HANDLERS.register('action.devices.DISCONNECT')
async def async_devices_disconnect(hass, config, payload):
async def async_devices_disconnect(hass, config, request_id, payload):
"""Handle action.devices.DISCONNECT request.
https://developers.google.com/actions/smarthome/create#actiondevicesdisconnect