Only create cloud user if cloud in use (#29150)

* Only create cloud user if cloud in use

* Pass context to alexa

* Update requirements

* Fix handing & design pattern for 0.30

* fix tests

* Fix lint & tests

* rename internal user
This commit is contained in:
Paulus Schoutsen
2019-11-28 05:23:59 -08:00
committed by Pascal Vizeli
parent 5d5d053bce
commit b847d55077
15 changed files with 221 additions and 145 deletions

View File

@@ -7,7 +7,7 @@ import logging
import aiohttp
from hass_nabucasa.client import CloudClient as Interface
from homeassistant.core import callback
from homeassistant.core import callback, Context
from homeassistant.components.google_assistant import smart_home as ga
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.helpers.dispatcher import async_dispatcher_send
@@ -44,7 +44,6 @@ class CloudClient(Interface):
self.alexa_user_config = alexa_user_config
self._alexa_config = None
self._google_config = None
self.cloud = None
@property
def base_path(self) -> Path:
@@ -92,23 +91,22 @@ class CloudClient(Interface):
return self._alexa_config
@property
def google_config(self) -> google_config.CloudGoogleConfig:
async def get_google_config(self) -> google_config.CloudGoogleConfig:
"""Return Google config."""
if not self._google_config:
assert self.cloud is not None
cloud_user = await self._prefs.get_cloud_user()
self._google_config = google_config.CloudGoogleConfig(
self._hass, self.google_user_config, self._prefs, self.cloud
self._hass, self.google_user_config, cloud_user, self._prefs, self.cloud
)
return self._google_config
async def async_initialize(self, cloud) -> None:
"""Initialize the client."""
self.cloud = cloud
if not self.cloud.is_logged_in:
return
async def logged_in(self) -> None:
"""When user logs in."""
await self.prefs.async_set_username(self.cloud.username)
if self.alexa_config.enabled and self.alexa_config.should_report_state:
try:
@@ -116,14 +114,18 @@ class CloudClient(Interface):
except alexa_errors.NoTokenAvailable:
pass
if self.google_config.enabled:
self.google_config.async_enable_local_sdk()
if self._prefs.google_enabled:
gconf = await self.get_google_config()
if self.google_config.should_report_state:
self.google_config.async_enable_report_state()
gconf.async_enable_local_sdk()
if gconf.should_report_state:
gconf.async_enable_report_state()
async def cleanups(self) -> None:
"""Cleanup some stuff after logout."""
await self.prefs.async_set_username(None)
self._google_config = None
@callback
@@ -141,8 +143,13 @@ class CloudClient(Interface):
async def async_alexa_message(self, payload: Dict[Any, Any]) -> Dict[Any, Any]:
"""Process cloud alexa message to client."""
cloud_user = await self._prefs.get_cloud_user()
return await alexa_sh.async_handle_message(
self._hass, self.alexa_config, payload, enabled=self._prefs.alexa_enabled
self._hass,
self.alexa_config,
payload,
context=Context(user_id=cloud_user),
enabled=self._prefs.alexa_enabled,
)
async def async_google_message(self, payload: Dict[Any, Any]) -> Dict[Any, Any]:
@@ -150,8 +157,10 @@ class CloudClient(Interface):
if not self._prefs.google_enabled:
return ga.turned_off_response(payload)
gconf = await self.get_google_config()
return await ga.async_handle_message(
self._hass, self.google_config, self.prefs.cloud_user, payload
self._hass, gconf, gconf.cloud_user, payload
)
async def async_webhook_message(self, payload: Dict[Any, Any]) -> Dict[Any, Any]: