Fix Cloud Google/Alexa check (#46681)

This commit is contained in:
Paulus Schoutsen 2021-02-16 21:49:53 -08:00 committed by GitHub
parent 58f6db0127
commit b956a571f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 5 deletions

View file

@ -62,7 +62,11 @@ class AlexaConfig(alexa_config.AbstractConfig):
@property
def enabled(self):
"""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
def supports_auth(self):

View file

@ -2,7 +2,7 @@
import asyncio
import logging
from hass_nabucasa import cloud_api
from hass_nabucasa import Cloud, cloud_api
from hass_nabucasa.google_report_state import ErrorResponse
from homeassistant.components.google_assistant.helpers import AbstractConfig
@ -28,7 +28,9 @@ _LOGGER = logging.getLogger(__name__)
class CloudGoogleConfig(AbstractConfig):
"""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."""
super().__init__(hass)
self._config = config
@ -43,7 +45,11 @@ class CloudGoogleConfig(AbstractConfig):
@property
def enabled(self):
"""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
def entity_config(self):

View file

@ -43,7 +43,20 @@ def mock_cloud_login(hass, mock_cloud_setup):
hass.data[const.DOMAIN].id_token = jwt.encode(
{
"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",
},
"test",

View file

@ -215,3 +215,16 @@ async def test_alexa_update_report_state(hass, cloud_prefs):
await hass.async_block_till_done()
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

View file

@ -192,3 +192,16 @@ async def test_google_config_expose_entity_prefs(mock_conf, cloud_prefs):
google_default_expose=["sensor"],
)
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