Fix Cloud Google/Alexa check (#46681)
This commit is contained in:
parent
58f6db0127
commit
b956a571f4
5 changed files with 54 additions and 5 deletions
|
@ -62,7 +62,11 @@ class AlexaConfig(alexa_config.AbstractConfig):
|
||||||
@property
|
@property
|
||||||
def enabled(self):
|
def enabled(self):
|
||||||
"""Return if Alexa is enabled."""
|
"""Return if Alexa is enabled."""
|
||||||
return self._prefs.alexa_enabled
|
return (
|
||||||
|
self._cloud.is_logged_in
|
||||||
|
and not self._cloud.subscription_expired
|
||||||
|
and self._prefs.alexa_enabled
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supports_auth(self):
|
def supports_auth(self):
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from hass_nabucasa import cloud_api
|
from hass_nabucasa import Cloud, cloud_api
|
||||||
from hass_nabucasa.google_report_state import ErrorResponse
|
from hass_nabucasa.google_report_state import ErrorResponse
|
||||||
|
|
||||||
from homeassistant.components.google_assistant.helpers import AbstractConfig
|
from homeassistant.components.google_assistant.helpers import AbstractConfig
|
||||||
|
@ -28,7 +28,9 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
class CloudGoogleConfig(AbstractConfig):
|
class CloudGoogleConfig(AbstractConfig):
|
||||||
"""HA Cloud Configuration for Google Assistant."""
|
"""HA Cloud Configuration for Google Assistant."""
|
||||||
|
|
||||||
def __init__(self, hass, config, cloud_user: str, prefs: CloudPreferences, cloud):
|
def __init__(
|
||||||
|
self, hass, config, cloud_user: str, prefs: CloudPreferences, cloud: Cloud
|
||||||
|
):
|
||||||
"""Initialize the Google config."""
|
"""Initialize the Google config."""
|
||||||
super().__init__(hass)
|
super().__init__(hass)
|
||||||
self._config = config
|
self._config = config
|
||||||
|
@ -43,7 +45,11 @@ class CloudGoogleConfig(AbstractConfig):
|
||||||
@property
|
@property
|
||||||
def enabled(self):
|
def enabled(self):
|
||||||
"""Return if Google is enabled."""
|
"""Return if Google is enabled."""
|
||||||
return self._cloud.is_logged_in and self._prefs.google_enabled
|
return (
|
||||||
|
self._cloud.is_logged_in
|
||||||
|
and not self._cloud.subscription_expired
|
||||||
|
and self._prefs.google_enabled
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def entity_config(self):
|
def entity_config(self):
|
||||||
|
|
|
@ -43,7 +43,20 @@ def mock_cloud_login(hass, mock_cloud_setup):
|
||||||
hass.data[const.DOMAIN].id_token = jwt.encode(
|
hass.data[const.DOMAIN].id_token = jwt.encode(
|
||||||
{
|
{
|
||||||
"email": "hello@home-assistant.io",
|
"email": "hello@home-assistant.io",
|
||||||
"custom:sub-exp": "2018-01-03",
|
"custom:sub-exp": "2300-01-03",
|
||||||
|
"cognito:username": "abcdefghjkl",
|
||||||
|
},
|
||||||
|
"test",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_expired_cloud_login(hass, mock_cloud_setup):
|
||||||
|
"""Mock cloud is logged in."""
|
||||||
|
hass.data[const.DOMAIN].id_token = jwt.encode(
|
||||||
|
{
|
||||||
|
"email": "hello@home-assistant.io",
|
||||||
|
"custom:sub-exp": "2018-01-01",
|
||||||
"cognito:username": "abcdefghjkl",
|
"cognito:username": "abcdefghjkl",
|
||||||
},
|
},
|
||||||
"test",
|
"test",
|
||||||
|
|
|
@ -215,3 +215,16 @@ async def test_alexa_update_report_state(hass, cloud_prefs):
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert len(mock_sync.mock_calls) == 1
|
assert len(mock_sync.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_enabled_requires_valid_sub(hass, mock_expired_cloud_login, cloud_prefs):
|
||||||
|
"""Test that alexa config enabled requires a valid Cloud sub."""
|
||||||
|
assert cloud_prefs.alexa_enabled
|
||||||
|
assert hass.data["cloud"].is_logged_in
|
||||||
|
assert hass.data["cloud"].subscription_expired
|
||||||
|
|
||||||
|
config = alexa_config.AlexaConfig(
|
||||||
|
hass, ALEXA_SCHEMA({}), "mock-user-id", cloud_prefs, hass.data["cloud"]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert not config.enabled
|
||||||
|
|
|
@ -192,3 +192,16 @@ async def test_google_config_expose_entity_prefs(mock_conf, cloud_prefs):
|
||||||
google_default_expose=["sensor"],
|
google_default_expose=["sensor"],
|
||||||
)
|
)
|
||||||
assert not mock_conf.should_expose(state)
|
assert not mock_conf.should_expose(state)
|
||||||
|
|
||||||
|
|
||||||
|
def test_enabled_requires_valid_sub(hass, mock_expired_cloud_login, cloud_prefs):
|
||||||
|
"""Test that google config enabled requires a valid Cloud sub."""
|
||||||
|
assert cloud_prefs.google_enabled
|
||||||
|
assert hass.data["cloud"].is_logged_in
|
||||||
|
assert hass.data["cloud"].subscription_expired
|
||||||
|
|
||||||
|
config = CloudGoogleConfig(
|
||||||
|
hass, GACTIONS_SCHEMA({}), "mock-user-id", cloud_prefs, hass.data["cloud"]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert not config.enabled
|
||||||
|
|
Loading…
Add table
Reference in a new issue