From b09f5b67436d8db44825d146b78ddce391d4469c Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 28 Apr 2019 00:42:06 -0700 Subject: [PATCH] Only include agent user ID in SYNC responses (#23497) --- homeassistant/components/cloud/client.py | 14 ++++---------- .../components/google_assistant/helpers.py | 6 +++++- .../components/google_assistant/smart_home.py | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/cloud/client.py b/homeassistant/components/cloud/client.py index 5bbd7bb48fa..f47eae74986 100644 --- a/homeassistant/components/cloud/client.py +++ b/homeassistant/components/cloud/client.py @@ -100,10 +100,13 @@ class CloudClient(Interface): return google_conf['filter'](entity.entity_id) + username = self._hass.data[DOMAIN].claims["cognito:username"] + self._google_config = ga_h.Config( should_expose=should_expose, secure_devices_pin=self._prefs.google_secure_devices_pin, entity_config=google_conf.get(CONF_ENTITY_CONFIG), + agent_user_id=username, ) # Set it to the latest. @@ -149,19 +152,10 @@ class CloudClient(Interface): if not self._prefs.google_enabled: return ga.turned_off_response(payload) - answer = await ga.async_handle_message( + return await ga.async_handle_message( self._hass, self.google_config, self.prefs.cloud_user, payload ) - # Fix AgentUserId - try: - cloud = self._hass.data[DOMAIN] - answer['payload']['agentUserId'] = cloud.claims['cognito:username'] - except (TypeError, KeyError): - return ga.turned_off_response(payload) - - return answer - async def async_webhook_message( self, payload: Dict[Any, Any]) -> Dict[Any, Any]: """Process cloud webhook message to client.""" diff --git a/homeassistant/components/google_assistant/helpers.py b/homeassistant/components/google_assistant/helpers.py index 71cce9de500..4d3f2855b31 100644 --- a/homeassistant/components/google_assistant/helpers.py +++ b/homeassistant/components/google_assistant/helpers.py @@ -20,12 +20,16 @@ class Config: """Hold the configuration for Google Assistant.""" def __init__(self, should_expose, - entity_config=None, secure_devices_pin=None): + entity_config=None, secure_devices_pin=None, + agent_user_id=None): """Initialize the configuration.""" self.should_expose = should_expose self.entity_config = entity_config or {} self.secure_devices_pin = secure_devices_pin + # Agent User Id to use for query responses + self.agent_user_id = agent_user_id + class RequestData: """Hold data associated with a particular request.""" diff --git a/homeassistant/components/google_assistant/smart_home.py b/homeassistant/components/google_assistant/smart_home.py index 37f35edf645..1ec47bbedd6 100644 --- a/homeassistant/components/google_assistant/smart_home.py +++ b/homeassistant/components/google_assistant/smart_home.py @@ -99,7 +99,7 @@ async def async_devices_sync(hass, data, payload): devices.append(serialized) response = { - 'agentUserId': data.context.user_id, + 'agentUserId': data.config.agent_user_id or data.context.user_id, 'devices': devices, }