From c963128306e407ce16eb21131c319760a91b6d26 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 7 Jan 2020 13:25:57 +0100 Subject: [PATCH] Upgrade to use new version of HASS-nabucasa (#30506) * Upgrade to use new version of HASS-nabucasa * Update hass-nabucasa to 0.31 --- homeassistant/components/cloud/__init__.py | 2 -- homeassistant/components/cloud/const.py | 1 - .../components/cloud/google_config.py | 18 ++--------- homeassistant/components/cloud/manifest.json | 2 +- homeassistant/package_constraints.txt | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/cloud/test_google_config.py | 13 ++++---- tests/components/cloud/test_http_api.py | 32 ++++++++++--------- tests/components/cloud/test_init.py | 2 -- 10 files changed, 31 insertions(+), 45 deletions(-) diff --git a/homeassistant/components/cloud/__init__.py b/homeassistant/components/cloud/__init__.py index 45748110fe4..0a0b9f0fe88 100644 --- a/homeassistant/components/cloud/__init__.py +++ b/homeassistant/components/cloud/__init__.py @@ -33,7 +33,6 @@ from .const import ( CONF_FILTER, CONF_GOOGLE_ACTIONS, CONF_GOOGLE_ACTIONS_REPORT_STATE_URL, - CONF_GOOGLE_ACTIONS_SYNC_URL, CONF_RELAYER, CONF_REMOTE_API_URL, CONF_SUBSCRIPTION_INFO_URL, @@ -93,7 +92,6 @@ CONFIG_SCHEMA = vol.Schema( vol.Optional(CONF_USER_POOL_ID): str, vol.Optional(CONF_REGION): str, vol.Optional(CONF_RELAYER): str, - vol.Optional(CONF_GOOGLE_ACTIONS_SYNC_URL): vol.Url(), vol.Optional(CONF_SUBSCRIPTION_INFO_URL): vol.Url(), vol.Optional(CONF_CLOUDHOOK_CREATE_URL): vol.Url(), vol.Optional(CONF_REMOTE_API_URL): vol.Url(), diff --git a/homeassistant/components/cloud/const.py b/homeassistant/components/cloud/const.py index 406263c85f8..3d930f0c2e5 100644 --- a/homeassistant/components/cloud/const.py +++ b/homeassistant/components/cloud/const.py @@ -31,7 +31,6 @@ CONF_FILTER = "filter" CONF_GOOGLE_ACTIONS = "google_actions" CONF_RELAYER = "relayer" CONF_USER_POOL_ID = "user_pool_id" -CONF_GOOGLE_ACTIONS_SYNC_URL = "google_actions_sync_url" CONF_SUBSCRIPTION_INFO_URL = "subscription_info_url" CONF_CLOUDHOOK_CREATE_URL = "cloudhook_create_url" CONF_REMOTE_API_URL = "remote_api_url" diff --git a/homeassistant/components/cloud/google_config.py b/homeassistant/components/cloud/google_config.py index 639540fbf03..1074aaa68b3 100644 --- a/homeassistant/components/cloud/google_config.py +++ b/homeassistant/components/cloud/google_config.py @@ -2,7 +2,7 @@ import asyncio import logging -import async_timeout +from hass_nabucasa import cloud_api from hass_nabucasa.google_report_state import ErrorResponse from homeassistant.components.google_assistant.helpers import AbstractConfig @@ -126,21 +126,9 @@ class CloudGoogleConfig(AbstractConfig): if self._sync_entities_lock.locked(): return 200 - websession = self.hass.helpers.aiohttp_client.async_get_clientsession() - async with self._sync_entities_lock: - with async_timeout.timeout(10): - await self._cloud.auth.async_check_token() - - _LOGGER.debug("Requesting sync") - - with async_timeout.timeout(30): - req = await websession.post( - self._cloud.google_actions_sync_url, - headers={"authorization": self._cloud.id_token}, - ) - _LOGGER.debug("Finished requesting syncing: %s", req.status) - return req.status + resp = await cloud_api.async_google_actions_request_sync(self._cloud) + return resp.status async def _async_prefs_updated(self, prefs): """Handle updated preferences.""" diff --git a/homeassistant/components/cloud/manifest.json b/homeassistant/components/cloud/manifest.json index 0714f2e4ba1..34ef7a6dfa5 100644 --- a/homeassistant/components/cloud/manifest.json +++ b/homeassistant/components/cloud/manifest.json @@ -2,7 +2,7 @@ "domain": "cloud", "name": "Home Assistant Cloud", "documentation": "https://www.home-assistant.io/integrations/cloud", - "requirements": ["hass-nabucasa==0.30"], + "requirements": ["hass-nabucasa==0.31"], "dependencies": ["http", "webhook"], "after_dependencies": ["alexa", "google_assistant"], "codeowners": ["@home-assistant/cloud"] diff --git a/homeassistant/package_constraints.txt b/homeassistant/package_constraints.txt index 8347567246e..99648ea604b 100644 --- a/homeassistant/package_constraints.txt +++ b/homeassistant/package_constraints.txt @@ -10,7 +10,7 @@ certifi>=2019.11.28 cryptography==2.8 defusedxml==0.6.0 distro==1.4.0 -hass-nabucasa==0.30 +hass-nabucasa==0.31 home-assistant-frontend==20191204.1 importlib-metadata==1.3.0 jinja2>=2.10.3 diff --git a/requirements_all.txt b/requirements_all.txt index 372cd4e9d15..c10ab7e440b 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -649,7 +649,7 @@ habitipy==0.2.0 hangups==0.4.9 # homeassistant.components.cloud -hass-nabucasa==0.30 +hass-nabucasa==0.31 # homeassistant.components.mqtt hbmqtt==0.9.5 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 5ae63fa957f..1b1c5bf9bbe 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -226,7 +226,7 @@ ha-ffmpeg==2.0 hangups==0.4.9 # homeassistant.components.cloud -hass-nabucasa==0.30 +hass-nabucasa==0.31 # homeassistant.components.mqtt hbmqtt==0.9.5 diff --git a/tests/components/cloud/test_google_config.py b/tests/components/cloud/test_google_config.py index 830751029d7..4f08c4c37d0 100644 --- a/tests/components/cloud/test_google_config.py +++ b/tests/components/cloud/test_google_config.py @@ -36,19 +36,20 @@ async def test_google_update_report_state(hass, cloud_prefs): async def test_sync_entities(aioclient_mock, hass, cloud_prefs): """Test sync devices.""" - aioclient_mock.post("http://example.com", status=404) config = CloudGoogleConfig( hass, GACTIONS_SCHEMA({}), "mock-user-id", cloud_prefs, - Mock( - google_actions_sync_url="http://example.com", - auth=Mock(async_check_token=Mock(side_effect=mock_coro)), - ), + Mock(auth=Mock(async_check_token=Mock(side_effect=mock_coro)),), ) - assert await config.async_sync_entities("user") == 404 + with patch( + "hass_nabucasa.cloud_api.async_google_actions_request_sync", + return_value=mock_coro(Mock(status=404)), + ) as mock_request_sync: + assert await config.async_sync_entities("user") == 404 + assert len(mock_request_sync.mock_calls) == 1 async def test_google_update_expose_trigger_sync(hass, cloud_prefs): diff --git a/tests/components/cloud/test_http_api.py b/tests/components/cloud/test_http_api.py index b82b2b5481e..dbc936b9216 100644 --- a/tests/components/cloud/test_http_api.py +++ b/tests/components/cloud/test_http_api.py @@ -1,7 +1,7 @@ """Tests for the HTTP API for the cloud component.""" import asyncio from ipaddress import ip_network -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, Mock, patch from hass_nabucasa import thingtalk from hass_nabucasa.auth import Unauthenticated, UnknownError @@ -21,7 +21,6 @@ from . import mock_cloud, mock_cloud_prefs from tests.common import mock_coro from tests.components.google_assistant import MockConfig -GOOGLE_ACTIONS_SYNC_URL = "https://api-test.hass.io/google_actions_sync" SUBSCRIPTION_INFO_URL = "https://api-test.hass.io/subscription_info" @@ -59,7 +58,6 @@ def setup_api(hass, aioclient_mock): "user_pool_id": "user_pool_id", "region": "region", "relayer": "relayer", - "google_actions_sync_url": GOOGLE_ACTIONS_SYNC_URL, "subscription_info_url": SUBSCRIPTION_INFO_URL, "google_actions": {"filter": {"include_domains": "light"}}, "alexa": { @@ -85,22 +83,26 @@ def mock_cognito(): yield mock_cog() -async def test_google_actions_sync( - mock_cognito, mock_cloud_login, cloud_client, aioclient_mock -): +async def test_google_actions_sync(mock_cognito, mock_cloud_login, cloud_client): """Test syncing Google Actions.""" - aioclient_mock.post(GOOGLE_ACTIONS_SYNC_URL) - req = await cloud_client.post("/api/cloud/google_actions/sync") - assert req.status == 200 + with patch( + "hass_nabucasa.cloud_api.async_google_actions_request_sync", + return_value=mock_coro(Mock(status=200)), + ) as mock_request_sync: + req = await cloud_client.post("/api/cloud/google_actions/sync") + assert req.status == 200 + assert len(mock_request_sync.mock_calls) == 1 -async def test_google_actions_sync_fails( - mock_cognito, mock_cloud_login, cloud_client, aioclient_mock -): +async def test_google_actions_sync_fails(mock_cognito, mock_cloud_login, cloud_client): """Test syncing Google Actions gone bad.""" - aioclient_mock.post(GOOGLE_ACTIONS_SYNC_URL, status=403) - req = await cloud_client.post("/api/cloud/google_actions/sync") - assert req.status == 403 + with patch( + "hass_nabucasa.cloud_api.async_google_actions_request_sync", + return_value=mock_coro(Mock(status=500)), + ) as mock_request_sync: + req = await cloud_client.post("/api/cloud/google_actions/sync") + assert req.status == 500 + assert len(mock_request_sync.mock_calls) == 1 async def test_login_view(hass, cloud_client): diff --git a/tests/components/cloud/test_init.py b/tests/components/cloud/test_init.py index 912c6f2cd8d..9dd8695b9b2 100644 --- a/tests/components/cloud/test_init.py +++ b/tests/components/cloud/test_init.py @@ -28,7 +28,6 @@ async def test_constructor_loads_info_from_config(hass): "user_pool_id": "test-user_pool_id", "region": "test-region", "relayer": "test-relayer", - "google_actions_sync_url": "http://test-google_actions_sync_url", "subscription_info_url": "http://test-subscription-info-url", "cloudhook_create_url": "http://test-cloudhook_create_url", "remote_api_url": "http://test-remote_api_url", @@ -46,7 +45,6 @@ async def test_constructor_loads_info_from_config(hass): assert cl.user_pool_id == "test-user_pool_id" assert cl.region == "test-region" assert cl.relayer == "test-relayer" - assert cl.google_actions_sync_url == "http://test-google_actions_sync_url" assert cl.subscription_info_url == "http://test-subscription-info-url" assert cl.cloudhook_create_url == "http://test-cloudhook_create_url" assert cl.remote_api_url == "http://test-remote_api_url"