Google Assistant: Create and pass context to service calls (#21551)

* Google Assistant: Create and pass context to service calls

* Refactor request data into separate object and pass to execute.
This commit is contained in:
Penny Wood authored and Paulus Schoutsen committed 2019-03-05 20:00:53 -08:00
1 parent fc1ee9be43
commit d1038ea79f
8 files changed
+345 -265

No files matched your search

@@ -36,7 +36,7 @@ from .const import (
ERR_UNKNOWN_ERROR,
EVENT_COMMAND_RECEIVED, EVENT_SYNC_RECEIVED, EVENT_QUERY_RECEIVED
)
from .helpers import SmartHomeError
from .helpers import SmartHomeError, RequestData
HANDLERS = Registry()
_LOGGER = logging.getLogger(__name__)
@@ -87,7 +87,8 @@ class _GoogleEntity:
domain = state.domain
features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
return [Trait(self.hass, state, self.config) for Trait in trait.TRAITS
return [Trait(self.hass, state, self.config)
for Trait in trait.TRAITS
if Trait.supported(domain, features)]
async def sync_serialize(self):
@@ -178,7 +179,7 @@ class _GoogleEntity:
return attrs
async def execute(self, command, params):
async def execute(self, command, data, params):
"""Execute a command.
https://developers.google.com/actions/smarthome/create-app#actiondevicesexecute
@@ -186,7 +187,7 @@ class _GoogleEntity:
executed = False
for trt in self.traits():
if trt.can_execute(command, params):
await trt.execute(command, params)
await trt.execute(command, data, params)
executed = True
break
@@ -202,9 +203,13 @@ class _GoogleEntity:
self.state = self.hass.states.get(self.entity_id)
async def async_handle_message(hass, config, message):
async def async_handle_message(hass, config, user_id, message):
"""Handle incoming API messages."""
response = await _process(hass, config, message)
request_id = message.get('requestId') # type: str
data = RequestData(config, user_id, request_id)
response = await _process(hass, data, message)
if response and 'errorCode' in response['payload']:
_LOGGER.error('Error handling message %s: %s',
@@ -213,14 +218,13 @@ async def async_handle_message(hass, config, message):
return response
async def _process(hass, config, message):
async def _process(hass, data, message):
"""Process a message."""
request_id = message.get('requestId') # type: str
inputs = message.get('inputs') # type: list
if len(inputs) != 1:
return {
'requestId': request_id,
'requestId': data.request_id,
'payload': {'errorCode': ERR_PROTOCOL_ERROR}
}
@@ -228,49 +232,49 @@ async def _process(hass, config, message):
if handler is None:
return {
'requestId': request_id,
'requestId': data.request_id,
'payload': {'errorCode': ERR_PROTOCOL_ERROR}
}
try:
result = await handler(hass, config, request_id,
inputs[0].get('payload'))
result = await handler(hass, data, inputs[0].get('payload'))
except SmartHomeError as err:
return {
'requestId': request_id,
'requestId': data.request_id,
'payload': {'errorCode': err.code}
}
except Exception: # pylint: disable=broad-except
_LOGGER.exception('Unexpected error')
return {
'requestId': request_id,
'requestId': data.request_id,
'payload': {'errorCode': ERR_UNKNOWN_ERROR}
}
if result is None:
return None
return {'requestId': request_id, 'payload': result}
return {'requestId': data.request_id, 'payload': result}
@HANDLERS.register('action.devices.SYNC')
async def async_devices_sync(hass, config, request_id, payload):
async def async_devices_sync(hass, data, 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
})
hass.bus.async_fire(
EVENT_SYNC_RECEIVED,
{'request_id': data.request_id},
context=data.context)
devices = []
for state in hass.states.async_all():
if state.entity_id in CLOUD_NEVER_EXPOSED_ENTITIES:
continue
if not config.should_expose(state):
if not data.config.should_expose(state):
continue
entity = _GoogleEntity(hass, config, state)
entity = _GoogleEntity(hass, data.config, state)
serialized = await entity.sync_serialize()
if serialized is None:
@@ -280,7 +284,7 @@ async def async_devices_sync(hass, config, request_id, payload):
devices.append(serialized)
response = {
'agentUserId': config.agent_user_id,
'agentUserId': data.context.user_id,
'devices': devices,
}
@@ -288,7 +292,7 @@ async def async_devices_sync(hass, config, request_id, payload):
@HANDLERS.register('action.devices.QUERY')
async def async_devices_query(hass, config, request_id, payload):
async def async_devices_query(hass, data, payload):
"""Handle action.devices.QUERY request.
https://developers.google.com/actions/smarthome/create-app#actiondevicesquery
@@ -298,23 +302,27 @@ async def async_devices_query(hass, config, request_id, payload):
devid = device['id']
state = hass.states.get(devid)
hass.bus.async_fire(EVENT_QUERY_RECEIVED, {
'request_id': request_id,
ATTR_ENTITY_ID: devid,
})
hass.bus.async_fire(
EVENT_QUERY_RECEIVED,
{
'request_id': data.request_id,
ATTR_ENTITY_ID: devid,
},
context=data.context)
if not state:
# If we can't find a state, the device is offline
devices[devid] = {'online': False}
continue
devices[devid] = _GoogleEntity(hass, config, state).query_serialize()
entity = _GoogleEntity(hass, data.config, state)
devices[devid] = entity.query_serialize()
return {'devices': devices}
@HANDLERS.register('action.devices.EXECUTE')
async def handle_devices_execute(hass, config, request_id, payload):
async def handle_devices_execute(hass, data, payload):
"""Handle action.devices.EXECUTE request.
https://developers.google.com/actions/smarthome/create-app#actiondevicesexecute
@@ -327,11 +335,14 @@ async def handle_devices_execute(hass, config, request_id, 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
})
hass.bus.async_fire(
EVENT_COMMAND_RECEIVED,
{
'request_id': data.request_id,
ATTR_ENTITY_ID: entity_id,
'execution': execution
},
context=data.context)
# Happens if error occurred. Skip entity for further processing
if entity_id in results:
@@ -348,10 +359,11 @@ async def handle_devices_execute(hass, config, request_id, payload):
}
continue
entities[entity_id] = _GoogleEntity(hass, config, state)
entities[entity_id] = _GoogleEntity(hass, data.config, state)
try:
await entities[entity_id].execute(execution['command'],
data,
execution.get('params', {}))
except SmartHomeError as err:
results[entity_id] = {
@@ -378,7 +390,7 @@ async def handle_devices_execute(hass, config, request_id, payload):
@HANDLERS.register('action.devices.DISCONNECT')
async def async_devices_disconnect(hass, config, request_id, payload):
async def async_devices_disconnect(hass, data, payload):
"""Handle action.devices.DISCONNECT request.
https://developers.google.com/actions/smarthome/create#actiondevicesdisconnect