async def unregister_webhook(self, event: Event | None = None) -> None: """Remove / Unregister webhook for toon.""" _LOGGER.debug("Unregistering Toon webhook (%s)", self.entry.data[CONF_WEBHOOK_ID]) try: await self.toon.unsubscribe_webhook(self.entry.entry_id) except ToonError as err: _LOGGER.error("Failed unregistering Toon webhook - %s", err) webhook_unregister(self.opp, self.entry.data[CONF_WEBHOOK_ID])
async def async_unload_entry(opp, entry): """Unload a mobile app entry.""" unload_ok = await opp.config_entries.async_unload_platforms( entry, PLATFORMS) if not unload_ok: return False webhook_id = entry.data[CONF_WEBHOOK_ID] webhook_unregister(opp, webhook_id) del opp.data[DOMAIN][DATA_CONFIG_ENTRIES][webhook_id] del opp.data[DOMAIN][DATA_DEVICES][webhook_id] await opp_notify.async_reload(opp, DOMAIN) return True
async def async_unload_entry(opp: OpenPeerPower, entry: ConfigEntry): """Unload a config entry.""" if CONF_WEBHOOK_ID in entry.data: webhook_unregister(opp, entry.data[CONF_WEBHOOK_ID]) await opp.data[DOMAIN][entry.entry_id][AUTH].async_dropwebhook() _LOGGER.info("Unregister Netatmo webhook") await opp.data[DOMAIN][entry.entry_id][DATA_HANDLER].async_cleanup() unload_ok = await opp.config_entries.async_unload_platforms( entry, PLATFORMS) if unload_ok: opp.data[DOMAIN].pop(entry.entry_id) return unload_ok
async def async_unload_entry(opp, entry): """Unload a mobile app entry.""" unload_ok = all( await asyncio.gather( *[ opp.config_entries.async_forward_entry_unload(entry, component) for component in PLATFORMS ] ) ) if not unload_ok: return False webhook_unregister(opp, entry.data[CONF_WEBHOOK_ID]) return True
async def unregister_webhook(_): if CONF_WEBHOOK_ID not in entry.data: return _LOGGER.debug("Unregister Netatmo webhook (%s)", entry.data[CONF_WEBHOOK_ID]) async_dispatcher_send( opp, f"signal-{DOMAIN}-webhook-None", { "type": "None", "data": { WEBHOOK_PUSH_TYPE: WEBHOOK_DEACTIVATION } }, ) webhook_unregister(opp, entry.data[CONF_WEBHOOK_ID]) await opp.data[DOMAIN][entry.entry_id][AUTH].async_dropwebhook()
async def register_webhook(self, event: Event | None = None) -> None: """Register a webhook with Toon to get live updates.""" if CONF_WEBHOOK_ID not in self.entry.data: data = {**self.entry.data, CONF_WEBHOOK_ID: secrets.token_hex()} self.opp.config_entries.async_update_entry(self.entry, data=data) if self.opp.components.cloud.async_active_subscription(): if CONF_CLOUDHOOK_URL not in self.entry.data: webhook_url = await self.opp.components.cloud.async_create_cloudhook( self.entry.data[CONF_WEBHOOK_ID]) data = {**self.entry.data, CONF_CLOUDHOOK_URL: webhook_url} self.opp.config_entries.async_update_entry(self.entry, data=data) else: webhook_url = self.entry.data[CONF_CLOUDHOOK_URL] else: webhook_url = self.opp.components.webhook.async_generate_url( self.entry.data[CONF_WEBHOOK_ID]) # Ensure the webhook is not registered already webhook_unregister(self.opp, self.entry.data[CONF_WEBHOOK_ID]) webhook_register( self.opp, DOMAIN, "Toon", self.entry.data[CONF_WEBHOOK_ID], self.handle_webhook, ) try: await self.toon.subscribe_webhook( application_id=self.entry.entry_id, url=webhook_url) _LOGGER.info("Registered Toon webhook: %s", webhook_url) except ToonError as err: _LOGGER.error("Error during webhook registration - %s", err) self.opp.bus.async_listen_once(EVENT_OPENPEERPOWER_STOP, self.unregister_webhook)