Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions custom_components/smartthings/smartapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
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
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.storage import Store
from homeassistant.helpers.network import NoURLAvailableError, get_url

from .const import (
Expand Down Expand Up @@ -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
Expand All @@ -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])

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down