Skip to content

Commit

Permalink
Uses cloudhook when user has cloud subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewoudenberg committed Jul 4, 2023
1 parent 18e2b37 commit 4a09cda
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions custom_components/loqed/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@


DOMAIN = "loqed"
CONF_CLOUDHOOK_URL = "cloudhook_url"
40 changes: 30 additions & 10 deletions custom_components/loqed/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

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

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

Expand All @@ -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])
2 changes: 1 addition & 1 deletion custom_components/loqed/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 4a09cda

Please sign in to comment.