diff --git a/custom_components/loqed/const.py b/custom_components/loqed/const.py index 6b1c031..59011f2 100644 --- a/custom_components/loqed/const.py +++ b/custom_components/loqed/const.py @@ -2,3 +2,4 @@ DOMAIN = "loqed" +CONF_CLOUDHOOK_URL = "cloudhook_url" diff --git a/custom_components/loqed/coordinator.py b/custom_components/loqed/coordinator.py index 18ac64a..42e0d52 100644 --- a/custom_components/loqed/coordinator.py +++ b/custom_components/loqed/coordinator.py @@ -6,14 +6,13 @@ import async_timeout from loqedAPI import loqed -from homeassistant.components import webhook +from homeassistant.components import cloud, webhook from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_NAME, CONF_WEBHOOK_ID from homeassistant.core import HomeAssistant -from homeassistant.helpers.network import get_url from homeassistant.helpers.update_coordinator import DataUpdateCoordinator -from .const import DOMAIN +from .const import CONF_CLOUDHOOK_URL, DOMAIN _LOGGER = logging.getLogger(__name__) @@ -115,9 +114,15 @@ async def ensure_webhooks(self) -> None: webhook.async_register( self.hass, DOMAIN, "Loqed", webhook_id, self._handle_webhook ) - webhook_path = webhook.async_generate_path(webhook_id) - webhook_url = f"{get_url(self.hass)}{webhook_path}" - _LOGGER.info("Webhook URL: %s", webhook_url) + + if cloud.async_active_subscription(self.hass): + webhook_url = await async_cloudhook_generate_url(self.hass, self._entry) + else: + webhook_url = webhook.async_generate_url( + self.hass, self._entry.data[CONF_WEBHOOK_ID] + ) + + _LOGGER.debug("Webhook URL: %s", webhook_url) webhooks = await self.lock.getWebhooks() @@ -130,19 +135,22 @@ async def ensure_webhooks(self) -> None: webhooks = await self.lock.getWebhooks() webhook_index = next(x["id"] for x in webhooks if x["url"] == webhook_url) - _LOGGER.info("Webhook got index %s", webhook_index) + _LOGGER.debug("Webhook got index %s", webhook_index) async def remove_webhooks(self) -> None: """Remove webhook from LOQED bridge.""" webhook_id = self._entry.data[CONF_WEBHOOK_ID] - webhook_path = webhook.async_generate_path(webhook_id) - webhook_url = f"{get_url(self.hass)}{webhook_path}" + + if CONF_CLOUDHOOK_URL in self._entry.data: + webhook_url = self._entry.data[CONF_CLOUDHOOK_URL] + else: + webhook_url = webhook.async_generate_url(self.hass, webhook_id) webhook.async_unregister( self.hass, webhook_id, ) - _LOGGER.info("Webhook URL: %s", webhook_url) + _LOGGER.debug("Webhook URL: %s", webhook_url) webhooks = await self.lock.getWebhooks() @@ -152,3 +160,15 @@ async def remove_webhooks(self) -> None: if webhook_index: await self.lock.deleteWebhook(webhook_index) + + +async def async_cloudhook_generate_url(hass: HomeAssistant, entry: ConfigEntry) -> str: + """Generate the full URL for a webhook_id.""" + if CONF_CLOUDHOOK_URL not in entry.data: + webhook_url = await cloud.async_create_cloudhook( + hass, entry.data[CONF_WEBHOOK_ID] + ) + data = {**entry.data, CONF_CLOUDHOOK_URL: webhook_url} + hass.config_entries.async_update_entry(entry, data=data) + return webhook_url + return str(entry.data[CONF_CLOUDHOOK_URL]) diff --git a/custom_components/loqed/manifest.json b/custom_components/loqed/manifest.json index 511690b..26479af 100644 --- a/custom_components/loqed/manifest.json +++ b/custom_components/loqed/manifest.json @@ -3,7 +3,7 @@ "name": "LOQED Touch Smart Lock", "codeowners": ["@mikewoudenberg"], "config_flow": true, - "dependencies": ["webhook"], + "dependencies": ["cloud", "webhook"], "documentation": "https://www.home-assistant.io/integrations/loqed", "iot_class": "local_push", "issue_tracker": "https://github.com/mikewoudenberg/homeassistant-loqed/issues",