1 """Provides the coordinator for a LOQED lock."""
5 from typing
import TypedDict
7 from aiohttp.web
import Request
8 from loqedAPI
import loqed
16 from .const
import CONF_CLOUDHOOK_URL, DOMAIN
18 _LOGGER = logging.getLogger(__name__)
22 """Properties in a battery update message."""
27 battery_percentage: int
31 """Properties in a battery update message."""
34 requested_state_numeric: int
42 """Properties in a battery update message."""
45 go_to_state_numeric: int
53 """Properties returned by the status endpoint of the bridhge."""
55 battery_percentage: int
57 battery_type_numeric: int
58 battery_voltage: float
60 bolt_state_numeric: int
72 """Data update coordinator for the loqed platform."""
81 """Initialize the Loqed Data Update coordinator."""
82 super().
__init__(hass, _LOGGER, name=
"Loqed sensors")
89 """Fetch data from API endpoint."""
90 async
with asyncio.timeout(10):
91 return await self.
_api_api.async_get_lock_details()
94 self, hass: HomeAssistant, webhook_id: str, request: Request
96 """Handle incoming Loqed messages."""
97 _LOGGER.debug(
"Callback received: %s", request.headers)
98 received_ts = request.headers[
"TIMESTAMP"]
99 received_hash = request.headers[
"HASH"]
100 body = await request.text()
102 _LOGGER.debug(
"Callback body: %s", body)
104 event_data = await self.
locklock.receiveWebhook(body, received_hash, received_ts)
105 if "error" in event_data:
106 _LOGGER.warning(
"Incorrect callback received:: %s", event_data)
112 """Register webhook on LOQED bridge."""
113 webhook_id = self.
_entry_entry.data[CONF_WEBHOOK_ID]
115 webhook.async_register(
119 if cloud.async_active_subscription(self.
hasshass):
122 webhook_url = webhook.async_generate_url(
123 self.
hasshass, self.
_entry_entry.data[CONF_WEBHOOK_ID]
126 _LOGGER.debug(
"Webhook URL: %s", webhook_url)
128 webhooks = await self.
locklock.getWebhooks()
130 webhook_index = next(
131 (x[
"id"]
for x
in webhooks
if x[
"url"] == webhook_url),
None
134 if not webhook_index:
135 await self.
locklock.registerWebhook(webhook_url)
136 webhooks = await self.
locklock.getWebhooks()
137 webhook_index = next(x[
"id"]
for x
in webhooks
if x[
"url"] == webhook_url)
139 _LOGGER.debug(
"Webhook got index %s", webhook_index)
142 """Remove webhook from LOQED bridge."""
143 webhook_id = self.
_entry_entry.data[CONF_WEBHOOK_ID]
145 if CONF_CLOUDHOOK_URL
in self.
_entry_entry.data:
146 webhook_url = self.
_entry_entry.data[CONF_CLOUDHOOK_URL]
148 webhook_url = webhook.async_generate_url(self.
hasshass, webhook_id)
150 webhook.async_unregister(
154 _LOGGER.debug(
"Webhook URL: %s", webhook_url)
156 webhooks = await self.
locklock.getWebhooks()
158 webhook_index = next(
159 (x[
"id"]
for x
in webhooks
if x[
"url"] == webhook_url),
None
163 await self.
locklock.deleteWebhook(webhook_index)
167 """Generate the full URL for a webhook_id."""
168 if CONF_CLOUDHOOK_URL
not in entry.data:
169 webhook_url = await cloud.async_create_cloudhook(
170 hass, entry.data[CONF_WEBHOOK_ID]
172 data = {**entry.data, CONF_CLOUDHOOK_URL: webhook_url}
173 hass.config_entries.async_update_entry(entry, data=data)
175 return str(entry.data[CONF_CLOUDHOOK_URL])
None _handle_webhook(self, HomeAssistant hass, str webhook_id, Request request)
StatusMessage _async_update_data(self)
None ensure_webhooks(self)
None __init__(self, HomeAssistant hass, loqed.LoqedAPI api, loqed.Lock lock, ConfigEntry entry)
None remove_webhooks(self)
None async_update_listeners(self)
str async_cloudhook_generate_url(HomeAssistant hass, ConfigEntry entry)