Google assistant storage of connected agents (#29158)

* Make async_report_state take agent_user_id

* Attempt to store synced agents

* Drop now not needed initialization

* Make sure cloud uses the all sync on changed preferences

* Some more places to use all version of sync

* Get the agent_user_id from the request context if available

* Minor cleanup

* Remove the old fixed agent_user_id for cloud

Instead pass along cloud_user where appropriate.

* async_delay_save takes a function

* Adjust test for delayed store

* Remove unused save function

* Add login check.
This commit is contained in:
Joakim Plate authored and Paulus Schoutsen committed 2019-12-02 22:05:59 -08:00
1 parent 434b783b4c
commit 2569c4ae37
16 files changed
+260 -83

No files matched your search

@@ -42,12 +42,7 @@ class CloudGoogleConfig(AbstractConfig):
@property
def enabled(self):
"""Return if Google is enabled."""
return self._prefs.google_enabled
@property
def agent_user_id(self):
"""Return Agent User Id to use for query responses."""
return self._cloud.username
return self._cloud.is_logged_in and self._prefs.google_enabled
@property
def entity_config(self):
@@ -62,7 +57,7 @@ class CloudGoogleConfig(AbstractConfig):
@property
def should_report_state(self):
"""Return if states should be proactively reported."""
return self._prefs.google_report_state
return self._cloud.is_logged_in and self._prefs.google_report_state
@property
def local_sdk_webhook_id(self):
@@ -104,7 +99,7 @@ class CloudGoogleConfig(AbstractConfig):
entity_config = entity_configs.get(state.entity_id, {})
return not entity_config.get(PREF_DISABLE_2FA, DEFAULT_DISABLE_2FA)
async def async_report_state(self, message):
async def async_report_state(self, message, agent_user_id: str):
"""Send a state report to Google."""
try:
await self._cloud.google_report_state.async_send_message(message)
@@ -132,13 +127,6 @@ class CloudGoogleConfig(AbstractConfig):
_LOGGER.debug("Finished requesting syncing: %s", req.status)
return req.status
async def async_deactivate_report_state(self):
"""Turn off report state and disable further state reporting.
Called when the user disconnects their account from Google.
"""
await self._prefs.async_update(google_report_state=False)
async def _async_prefs_updated(self, prefs):
"""Handle updated preferences."""
if self.should_report_state != self.is_reporting_state:
@@ -149,7 +137,7 @@ class CloudGoogleConfig(AbstractConfig):
# State reporting is reported as a property on entities.
# So when we change it, we need to sync all entities.
await self.async_sync_entities(self.agent_user_id)
await self.async_sync_entities_all()
# If entity prefs are the same or we have filter in config.yaml,
# don't sync.
@@ -157,7 +145,7 @@ class CloudGoogleConfig(AbstractConfig):
self._cur_entity_prefs is not prefs.google_entity_configs
and self._config["filter"].empty_filter
):
self.async_schedule_google_sync(self.agent_user_id)
self.async_schedule_google_sync_all()
if self.enabled and not self.is_local_sdk_active:
self.async_enable_local_sdk()
@@ -173,4 +161,4 @@ class CloudGoogleConfig(AbstractConfig):
# Schedule a sync if a change was made to an entity that Google knows about
if self._should_expose_entity_id(entity_id):
await self.async_sync_entities(self.agent_user_id)
await self.async_sync_entities_all()