Fix google sync ()

pull/30522/head^2
Paulus Schoutsen 2020-01-06 22:00:39 +01:00 committed by GitHub
parent a84741392b
commit 05374d7c85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 39 additions and 7 deletions
homeassistant/components
tests/components
google_assistant

View File

@ -77,6 +77,12 @@ class CloudGoogleConfig(AbstractConfig):
"""Return Cloud User account.""" """Return Cloud User account."""
return self._user return self._user
async def async_initialize(self):
"""Perform async initialization of config."""
await super().async_initialize()
# Remove bad data that was there until 0.103.6 - Jan 6, 2020
self._store.pop_agent_user_id(self._user)
def should_expose(self, state): def should_expose(self, state):
"""If a state object should be exposed.""" """If a state object should be exposed."""
return self._should_expose_entity_id(state.entity_id) return self._should_expose_entity_id(state.entity_id)
@ -93,6 +99,15 @@ class CloudGoogleConfig(AbstractConfig):
entity_config = entity_configs.get(entity_id, {}) entity_config = entity_configs.get(entity_id, {})
return entity_config.get(PREF_SHOULD_EXPOSE, DEFAULT_SHOULD_EXPOSE) return entity_config.get(PREF_SHOULD_EXPOSE, DEFAULT_SHOULD_EXPOSE)
@property
def agent_user_id(self):
"""Return Agent User Id to use for query responses."""
return self._cloud.username
def get_agent_user_id(self, context):
"""Get agent user ID making request."""
return self.agent_user_id
def should_2fa(self, state): def should_2fa(self, state):
"""If an entity should be checked for 2FA.""" """If an entity should be checked for 2FA."""
entity_configs = self._prefs.google_entity_configs entity_configs = self._prefs.google_entity_configs

View File

@ -175,7 +175,7 @@ class GoogleActionsSyncView(HomeAssistantView):
hass = request.app["hass"] hass = request.app["hass"]
cloud: Cloud = hass.data[DOMAIN] cloud: Cloud = hass.data[DOMAIN]
gconf = await cloud.client.get_google_config() gconf = await cloud.client.get_google_config()
status = await gconf.async_sync_entities(gconf.cloud_user) status = await gconf.async_sync_entities(gconf.agent_user_id)
return self.json({}, status_code=status) return self.json({}, status_code=status)

View File

@ -1,4 +1,5 @@
"""Helper classes for Google Assistant integration.""" """Helper classes for Google Assistant integration."""
from abc import ABC, abstractmethod
from asyncio import gather from asyncio import gather
from collections.abc import Mapping from collections.abc import Mapping
import logging import logging
@ -35,7 +36,7 @@ SYNC_DELAY = 15
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
class AbstractConfig: class AbstractConfig(ABC):
"""Hold the configuration for Google Assistant.""" """Hold the configuration for Google Assistant."""
_unsub_report_state = None _unsub_report_state = None
@ -95,9 +96,13 @@ class AbstractConfig:
"""Return the user ID to be used for actions received via the local SDK.""" """Return the user ID to be used for actions received via the local SDK."""
raise NotImplementedError raise NotImplementedError
@abstractmethod
def get_agent_user_id(self, context):
"""Get agent user ID from context."""
@abstractmethod
def should_expose(self, state) -> bool: def should_expose(self, state) -> bool:
"""Return if entity should be exposed.""" """Return if entity should be exposed."""
raise NotImplementedError
def should_2fa(self, state): def should_2fa(self, state):
"""If an entity should have 2FA checked.""" """If an entity should have 2FA checked."""

View File

@ -121,6 +121,10 @@ class GoogleConfig(AbstractConfig):
return is_default_exposed or explicit_expose return is_default_exposed or explicit_expose
def get_agent_user_id(self, context):
"""Get agent user ID making request."""
return context.user_id
def should_2fa(self, state): def should_2fa(self, state):
"""If an entity should have 2FA checked.""" """If an entity should have 2FA checked."""
return True return True

View File

@ -78,7 +78,7 @@ async def async_devices_sync(hass, data, payload):
EVENT_SYNC_RECEIVED, {"request_id": data.request_id}, context=data.context EVENT_SYNC_RECEIVED, {"request_id": data.request_id}, context=data.context
) )
agent_user_id = data.context.user_id agent_user_id = data.config.get_agent_user_id(data.context)
devices = await asyncio.gather( devices = await asyncio.gather(
*( *(

View File

@ -103,13 +103,17 @@ async def test_handler_google_actions(hass):
reqid = "5711642932632160983" reqid = "5711642932632160983"
data = {"requestId": reqid, "inputs": [{"intent": "action.devices.SYNC"}]} data = {"requestId": reqid, "inputs": [{"intent": "action.devices.SYNC"}]}
config = await cloud.client.get_google_config() with patch(
resp = await cloud.client.async_google_message(data) "hass_nabucasa.Cloud._decode_claims",
return_value={"cognito:username": "myUserName"},
):
await cloud.client.get_google_config()
resp = await cloud.client.async_google_message(data)
assert resp["requestId"] == reqid assert resp["requestId"] == reqid
payload = resp["payload"] payload = resp["payload"]
assert payload["agentUserId"] == config.cloud_user assert payload["agentUserId"] == "myUserName"
devices = payload["devices"] devices = payload["devices"]
assert len(devices) == 1 assert len(devices) == 1

View File

@ -64,6 +64,10 @@ class MockConfig(helpers.AbstractConfig):
"""Return local SDK webhook id.""" """Return local SDK webhook id."""
return self._local_sdk_user_id return self._local_sdk_user_id
def get_agent_user_id(self, context):
"""Get agent user ID making request."""
return context.user_id
def should_expose(self, state): def should_expose(self, state):
"""Expose it all.""" """Expose it all."""
return self._should_expose is None or self._should_expose(state) return self._should_expose is None or self._should_expose(state)