From b956a571f4a136d0dac6ec46326cf640f7a8fc02 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 16 Feb 2021 21:49:53 -0800 Subject: [PATCH] Fix Cloud Google/Alexa check (#46681) --- homeassistant/components/cloud/alexa_config.py | 6 +++++- homeassistant/components/cloud/google_config.py | 12 +++++++++--- tests/components/cloud/conftest.py | 15 ++++++++++++++- tests/components/cloud/test_alexa_config.py | 13 +++++++++++++ tests/components/cloud/test_google_config.py | 13 +++++++++++++ 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/cloud/alexa_config.py b/homeassistant/components/cloud/alexa_config.py index 7abbefe85ff..2d4714b4c81 100644 --- a/homeassistant/components/cloud/alexa_config.py +++ b/homeassistant/components/cloud/alexa_config.py @@ -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): diff --git a/homeassistant/components/cloud/google_config.py b/homeassistant/components/cloud/google_config.py index 2ac0bc40252..dffa1e2f306 100644 --- a/homeassistant/components/cloud/google_config.py +++ b/homeassistant/components/cloud/google_config.py @@ -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): diff --git a/tests/components/cloud/conftest.py b/tests/components/cloud/conftest.py index 4755d470418..75276a9f2e2 100644 --- a/tests/components/cloud/conftest.py +++ b/tests/components/cloud/conftest.py @@ -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", diff --git a/tests/components/cloud/test_alexa_config.py b/tests/components/cloud/test_alexa_config.py index 966ef4b0af3..8e104f641b2 100644 --- a/tests/components/cloud/test_alexa_config.py +++ b/tests/components/cloud/test_alexa_config.py @@ -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 diff --git a/tests/components/cloud/test_google_config.py b/tests/components/cloud/test_google_config.py index f58ea1a415b..e1da6bbe0a8 100644 --- a/tests/components/cloud/test_google_config.py +++ b/tests/components/cloud/test_google_config.py @@ -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