From d98114d2ab78c95b9fc63e4c3debe53106d3bcb9 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 18 Oct 2019 18:08:54 -0700 Subject: [PATCH] Guard cloud check (#27901) * Guard cloud check * Fix pos args --- .../components/owntracks/config_flow.py | 5 +++- .../components/smartthings/smartapp.py | 28 ++++++++++++++----- homeassistant/helpers/config_entry_flow.py | 5 +++- .../components/owntracks/test_config_flow.py | 1 + .../smartthings/test_config_flow.py | 2 ++ tests/components/smartthings/test_init.py | 1 + 6 files changed, 33 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/owntracks/config_flow.py b/homeassistant/components/owntracks/config_flow.py index 67553ef608ff..343a6d90b520 100644 --- a/homeassistant/components/owntracks/config_flow.py +++ b/homeassistant/components/owntracks/config_flow.py @@ -78,7 +78,10 @@ class OwnTracksFlow(config_entries.ConfigFlow): async def _get_webhook_id(self): """Generate webhook ID.""" webhook_id = self.hass.components.webhook.async_generate_id() - if self.hass.components.cloud.async_active_subscription(): + if ( + "cloud" in self.hass.config.components + and self.hass.components.cloud.async_active_subscription() + ): webhook_url = await self.hass.components.cloud.async_create_cloudhook( webhook_id ) diff --git a/homeassistant/components/smartthings/smartapp.py b/homeassistant/components/smartthings/smartapp.py index d205c1d245cb..ecd4da5dcabc 100644 --- a/homeassistant/components/smartthings/smartapp.py +++ b/homeassistant/components/smartthings/smartapp.py @@ -22,7 +22,7 @@ from pysmartthings import ( SubscriptionEntity, ) -from homeassistant.components import cloud, webhook +from homeassistant.components import webhook from homeassistant.const import CONF_WEBHOOK_ID from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.dispatcher import ( @@ -88,7 +88,10 @@ async def validate_installed_app(api, installed_app_id: str): def validate_webhook_requirements(hass: HomeAssistantType) -> bool: """Ensure HASS is setup properly to receive webhooks.""" - if cloud.async_active_subscription(hass): + if ( + "cloud" in hass.config.components + and hass.components.cloud.async_active_subscription() + ): return True if hass.data[DOMAIN][CONF_CLOUDHOOK_URL] is not None: return True @@ -102,7 +105,11 @@ def get_webhook_url(hass: HomeAssistantType) -> str: Return the cloudhook if available, otherwise local webhook. """ cloudhook_url = hass.data[DOMAIN][CONF_CLOUDHOOK_URL] - if cloud.async_active_subscription(hass) and cloudhook_url is not None: + if ( + "cloud" in hass.config.components + and hass.components.cloud.async_active_subscription() + and cloudhook_url is not None + ): return cloudhook_url return webhook.async_generate_url(hass, hass.data[DOMAIN][CONF_WEBHOOK_ID]) @@ -222,10 +229,11 @@ async def setup_smartapp_endpoint(hass: HomeAssistantType): cloudhook_url = config.get(CONF_CLOUDHOOK_URL) if ( cloudhook_url is None - and cloud.async_active_subscription(hass) + and "cloud" in hass.config.components + and hass.components.cloud.async_active_subscription() and not hass.config_entries.async_entries(DOMAIN) ): - cloudhook_url = await cloud.async_create_cloudhook( + cloudhook_url = await hass.components.cloud.async_create_cloudhook( hass, config[CONF_WEBHOOK_ID] ) config[CONF_CLOUDHOOK_URL] = cloudhook_url @@ -273,8 +281,14 @@ async def unload_smartapp_endpoint(hass: HomeAssistantType): return # Remove the cloudhook if it was created cloudhook_url = hass.data[DOMAIN][CONF_CLOUDHOOK_URL] - if cloudhook_url and cloud.async_is_logged_in(hass): - await cloud.async_delete_cloudhook(hass, hass.data[DOMAIN][CONF_WEBHOOK_ID]) + if ( + cloudhook_url + and "cloud" in hass.config.components + and hass.components.cloud.async_is_logged_in() + ): + await hass.components.cloud.async_delete_cloudhook( + hass, hass.data[DOMAIN][CONF_WEBHOOK_ID] + ) # Remove cloudhook from storage store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY) await store.async_save( diff --git a/homeassistant/helpers/config_entry_flow.py b/homeassistant/helpers/config_entry_flow.py index 88aae3721b1a..374ef7958464 100644 --- a/homeassistant/helpers/config_entry_flow.py +++ b/homeassistant/helpers/config_entry_flow.py @@ -124,7 +124,10 @@ class WebhookFlowHandler(config_entries.ConfigFlow): webhook_id = self.hass.components.webhook.async_generate_id() - if self.hass.components.cloud.async_active_subscription(): + if ( + "cloud" in self.hass.config.components + and self.hass.components.cloud.async_active_subscription() + ): webhook_url = await self.hass.components.cloud.async_create_cloudhook( webhook_id ) diff --git a/tests/components/owntracks/test_config_flow.py b/tests/components/owntracks/test_config_flow.py index 76863c616984..c4e2a54f69a0 100644 --- a/tests/components/owntracks/test_config_flow.py +++ b/tests/components/owntracks/test_config_flow.py @@ -43,6 +43,7 @@ async def test_config_flow_unload(hass): async def test_with_cloud_sub(hass): """Test creating a config flow while subscribed.""" + hass.config.components.add("cloud") with patch( "homeassistant.components.cloud.async_active_subscription", return_value=True ), patch( diff --git a/tests/components/smartthings/test_config_flow.py b/tests/components/smartthings/test_config_flow.py index fce0129a7bf3..521f1c6a6a89 100644 --- a/tests/components/smartthings/test_config_flow.py +++ b/tests/components/smartthings/test_config_flow.py @@ -205,6 +205,8 @@ async def test_cloudhook_app_created_then_show_wait_form( hass, app, app_oauth_client, smartthings_mock ): """Test SmartApp is created with a cloudhoko and shows wait form.""" + hass.config.components.add("cloud") + # Unload the endpoint so we can reload it under the cloud. await smartapp.unload_smartapp_endpoint(hass) diff --git a/tests/components/smartthings/test_init.py b/tests/components/smartthings/test_init.py index 9749ab9bb71e..b8cd65f5a0b9 100644 --- a/tests/components/smartthings/test_init.py +++ b/tests/components/smartthings/test_init.py @@ -268,6 +268,7 @@ async def test_remove_entry(hass, config_entry, smartthings_mock): async def test_remove_entry_cloudhook(hass, config_entry, smartthings_mock): """Test that the installed app, app, and cloudhook are removed up.""" + hass.config.components.add("cloud") # Arrange config_entry.add_to_hass(hass) hass.data[DOMAIN][CONF_CLOUDHOOK_URL] = "https://test.cloud"