diff --git a/custom_components/smartthings/smartapp.py b/custom_components/smartthings/smartapp.py index 8aab27a..ca62a87 100644 --- a/custom_components/smartthings/smartapp.py +++ b/custom_components/smartthings/smartapp.py @@ -22,7 +22,7 @@ SubscriptionEntity, ) -from homeassistant.components import webhook +from homeassistant.components import webhook, cloud from homeassistant.const import CONF_WEBHOOK_ID from homeassistant.core import HomeAssistant from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -30,6 +30,7 @@ async_dispatcher_connect, async_dispatcher_send, ) +from homeassistant.helpers.storage import Store from homeassistant.helpers.network import NoURLAvailableError, get_url from .const import ( @@ -93,7 +94,7 @@ async def validate_installed_app(api, installed_app_id: str): def validate_webhook_requirements(hass: HomeAssistant) -> bool: """Ensure Home Assistant is setup properly to receive webhooks.""" - if hass.components.cloud.async_active_subscription(): + if cloud.async_active_subscription(hass): return True if hass.data[DOMAIN][CONF_CLOUDHOOK_URL] is not None: return True @@ -107,7 +108,7 @@ def get_webhook_url(hass: HomeAssistant) -> str: Return the cloudhook if available, otherwise local webhook. """ cloudhook_url = hass.data[DOMAIN][CONF_CLOUDHOOK_URL] - if hass.components.cloud.async_active_subscription() and cloudhook_url is not None: + if cloud.async_active_subscription(hass) and cloudhook_url is not None: return cloudhook_url return webhook.async_generate_url(hass, hass.data[DOMAIN][CONF_WEBHOOK_ID]) @@ -209,7 +210,7 @@ async def setup_smartapp_endpoint(hass: HomeAssistant): return # Get/create config to store a unique id for this hass instance. - store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY) + store = Store(hass, STORAGE_VERSION, STORAGE_KEY) config = await store.async_load() if not config: # Create config @@ -229,11 +230,11 @@ async def setup_smartapp_endpoint(hass: HomeAssistant): cloudhook_url = config.get(CONF_CLOUDHOOK_URL) if ( cloudhook_url is None - and hass.components.cloud.async_active_subscription() + and cloud.async_active_subscription(hass) and not hass.config_entries.async_entries(DOMAIN) ): - cloudhook_url = await hass.components.cloud.async_create_cloudhook( - config[CONF_WEBHOOK_ID] + cloudhook_url = await cloud.async_create_cloudhook( + hass, config[CONF_WEBHOOK_ID] ) config[CONF_CLOUDHOOK_URL] = cloudhook_url await store.async_save(config) @@ -279,9 +280,9 @@ async def unload_smartapp_endpoint(hass: HomeAssistant): return # Remove the cloudhook if it was created cloudhook_url = hass.data[DOMAIN][CONF_CLOUDHOOK_URL] - if cloudhook_url and hass.components.cloud.async_is_logged_in(): - await hass.components.cloud.async_delete_cloudhook( - hass.data[DOMAIN][CONF_WEBHOOK_ID] + if cloudhook_url and cloud.async_is_logged_in(hass): + await cloud.async_delete_cloudhook( + hass, hass.data[DOMAIN][CONF_WEBHOOK_ID] ) # Remove cloudhook from storage store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY)