mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Unregister webhook if it can't be established successfully (#42791)
This commit is contained in:
parent
f0e4510213
commit
a4c8cb6f84
@ -21,6 +21,11 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import CoreState, HomeAssistant
|
from homeassistant.core import CoreState, HomeAssistant
|
||||||
from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv
|
from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv
|
||||||
|
from homeassistant.helpers.dispatcher import (
|
||||||
|
async_dispatcher_connect,
|
||||||
|
async_dispatcher_send,
|
||||||
|
)
|
||||||
|
from homeassistant.helpers.event import async_call_later
|
||||||
|
|
||||||
from . import api, config_flow
|
from . import api, config_flow
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -54,18 +59,19 @@ CONFIG_SCHEMA = vol.Schema(
|
|||||||
extra=vol.ALLOW_EXTRA,
|
extra=vol.ALLOW_EXTRA,
|
||||||
)
|
)
|
||||||
|
|
||||||
PLATFORMS = ["camera", "climate", "sensor"]
|
PLATFORMS = ["camera", "climate", "light", "sensor"]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: dict):
|
async def async_setup(hass: HomeAssistant, config: dict):
|
||||||
"""Set up the Netatmo component."""
|
"""Set up the Netatmo component."""
|
||||||
hass.data[DOMAIN] = {}
|
hass.data[DOMAIN] = {
|
||||||
hass.data[DOMAIN][DATA_PERSONS] = {}
|
DATA_PERSONS: {},
|
||||||
hass.data[DOMAIN][DATA_DEVICE_IDS] = {}
|
DATA_DEVICE_IDS: {},
|
||||||
hass.data[DOMAIN][DATA_SCHEDULES] = {}
|
DATA_SCHEDULES: {},
|
||||||
hass.data[DOMAIN][DATA_HOMES] = {}
|
DATA_HOMES: {},
|
||||||
hass.data[DOMAIN][DATA_EVENTS] = {}
|
DATA_EVENTS: {},
|
||||||
hass.data[DOMAIN][DATA_CAMERAS] = {}
|
DATA_CAMERAS: {},
|
||||||
|
}
|
||||||
|
|
||||||
if DOMAIN not in config:
|
if DOMAIN not in config:
|
||||||
return True
|
return True
|
||||||
@ -114,6 +120,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
if CONF_WEBHOOK_ID not in entry.data:
|
if CONF_WEBHOOK_ID not in entry.data:
|
||||||
return
|
return
|
||||||
_LOGGER.debug("Unregister Netatmo webhook (%s)", entry.data[CONF_WEBHOOK_ID])
|
_LOGGER.debug("Unregister Netatmo webhook (%s)", entry.data[CONF_WEBHOOK_ID])
|
||||||
|
async_dispatcher_send(
|
||||||
|
hass,
|
||||||
|
f"signal-{DOMAIN}-webhook-None",
|
||||||
|
{"type": "None", "data": {"push_type": "webhook_deactivation"}},
|
||||||
|
)
|
||||||
webhook_unregister(hass, entry.data[CONF_WEBHOOK_ID])
|
webhook_unregister(hass, entry.data[CONF_WEBHOOK_ID])
|
||||||
|
|
||||||
async def register_webhook(event):
|
async def register_webhook(event):
|
||||||
@ -148,13 +159,30 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
webhook_register(
|
webhook_register(
|
||||||
hass, DOMAIN, "Netatmo", entry.data[CONF_WEBHOOK_ID], handle_webhook
|
hass, DOMAIN, "Netatmo", entry.data[CONF_WEBHOOK_ID], handle_webhook
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def handle_event(event):
|
||||||
|
"""Handle webhook events."""
|
||||||
|
if event["data"]["push_type"] == "webhook_activation":
|
||||||
|
if activation_listener is not None:
|
||||||
|
_LOGGER.debug("sub called")
|
||||||
|
activation_listener()
|
||||||
|
|
||||||
|
if activation_timeout is not None:
|
||||||
|
_LOGGER.debug("Unsub called")
|
||||||
|
activation_timeout()
|
||||||
|
|
||||||
|
activation_listener = async_dispatcher_connect(
|
||||||
|
hass,
|
||||||
|
f"signal-{DOMAIN}-webhook-None",
|
||||||
|
handle_event,
|
||||||
|
)
|
||||||
|
|
||||||
|
activation_timeout = async_call_later(hass, 10, unregister_webhook)
|
||||||
|
|
||||||
await hass.async_add_executor_job(
|
await hass.async_add_executor_job(
|
||||||
hass.data[DOMAIN][entry.entry_id][AUTH].addwebhook, webhook_url
|
hass.data[DOMAIN][entry.entry_id][AUTH].addwebhook, webhook_url
|
||||||
)
|
)
|
||||||
_LOGGER.info("Register Netatmo webhook: %s", webhook_url)
|
_LOGGER.info("Register Netatmo webhook: %s", webhook_url)
|
||||||
hass.async_create_task(
|
|
||||||
hass.config_entries.async_forward_entry_setup(entry, "light")
|
|
||||||
)
|
|
||||||
except pyatmo.ApiError as err:
|
except pyatmo.ApiError as err:
|
||||||
_LOGGER.error("Error during webhook registration - %s", err)
|
_LOGGER.error("Error during webhook registration - %s", err)
|
||||||
|
|
||||||
|
@ -107,6 +107,10 @@ class NetatmoDataHandler:
|
|||||||
_LOGGER.info("%s webhook successfully registered", MANUFACTURER)
|
_LOGGER.info("%s webhook successfully registered", MANUFACTURER)
|
||||||
self._webhook = True
|
self._webhook = True
|
||||||
|
|
||||||
|
elif event["data"]["push_type"] == "webhook_deactivation":
|
||||||
|
_LOGGER.info("%s webhook unregistered", MANUFACTURER)
|
||||||
|
self._webhook = False
|
||||||
|
|
||||||
elif event["data"]["push_type"] == "NACamera-connection":
|
elif event["data"]["push_type"] == "NACamera-connection":
|
||||||
_LOGGER.debug("%s camera reconnected", MANUFACTURER)
|
_LOGGER.debug("%s camera reconnected", MANUFACTURER)
|
||||||
self._data_classes[CAMERA_DATA_CLASS_NAME][NEXT_SCAN] = time()
|
self._data_classes[CAMERA_DATA_CLASS_NAME][NEXT_SCAN] = time()
|
||||||
@ -118,6 +122,7 @@ class NetatmoDataHandler:
|
|||||||
partial(data_class, **kwargs),
|
partial(data_class, **kwargs),
|
||||||
self._auth,
|
self._auth,
|
||||||
)
|
)
|
||||||
|
|
||||||
for update_callback in self._data_classes[data_class_entry][
|
for update_callback in self._data_classes[data_class_entry][
|
||||||
"subscriptions"
|
"subscriptions"
|
||||||
]:
|
]:
|
||||||
|
@ -53,9 +53,6 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||||||
|
|
||||||
for camera in all_cameras:
|
for camera in all_cameras:
|
||||||
if camera["type"] == "NOC":
|
if camera["type"] == "NOC":
|
||||||
if not data_handler.webhook:
|
|
||||||
raise PlatformNotReady
|
|
||||||
|
|
||||||
_LOGGER.debug("Adding camera light %s %s", camera["id"], camera["name"])
|
_LOGGER.debug("Adding camera light %s %s", camera["id"], camera["name"])
|
||||||
entities.append(
|
entities.append(
|
||||||
NetatmoLight(
|
NetatmoLight(
|
||||||
@ -126,6 +123,11 @@ class NetatmoLight(NetatmoBase, LightEntity):
|
|||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
"""If the webhook is not established, mark as unavailable."""
|
||||||
|
return bool(self.data_handler.webhook)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if light is on."""
|
"""Return true if light is on."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user