Clean up Netatmo webhook handler (#47037)
parent
125206adbf
commit
4e09d7ee0f
|
@ -43,7 +43,7 @@ from .const import (
|
||||||
OAUTH2_TOKEN,
|
OAUTH2_TOKEN,
|
||||||
)
|
)
|
||||||
from .data_handler import NetatmoDataHandler
|
from .data_handler import NetatmoDataHandler
|
||||||
from .webhook import handle_webhook
|
from .webhook import async_handle_webhook
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -157,18 +157,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
webhook_register(
|
webhook_register(
|
||||||
hass, DOMAIN, "Netatmo", entry.data[CONF_WEBHOOK_ID], handle_webhook
|
hass,
|
||||||
|
DOMAIN,
|
||||||
|
"Netatmo",
|
||||||
|
entry.data[CONF_WEBHOOK_ID],
|
||||||
|
async_handle_webhook,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def handle_event(event):
|
async def handle_event(event):
|
||||||
"""Handle webhook events."""
|
"""Handle webhook events."""
|
||||||
if event["data"]["push_type"] == "webhook_activation":
|
if event["data"]["push_type"] == "webhook_activation":
|
||||||
if activation_listener is not None:
|
if activation_listener is not None:
|
||||||
_LOGGER.debug("sub called")
|
|
||||||
activation_listener()
|
activation_listener()
|
||||||
|
|
||||||
if activation_timeout is not None:
|
if activation_timeout is not None:
|
||||||
_LOGGER.debug("Unsub called")
|
|
||||||
activation_timeout()
|
activation_timeout()
|
||||||
|
|
||||||
activation_listener = async_dispatcher_connect(
|
activation_listener = async_dispatcher_connect(
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
"""The Netatmo integration."""
|
"""The Netatmo integration."""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.const import ATTR_DEVICE_ID, ATTR_ID
|
from homeassistant.const import ATTR_DEVICE_ID, ATTR_ID, ATTR_NAME
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_EVENT_TYPE,
|
ATTR_EVENT_TYPE,
|
||||||
ATTR_FACE_URL,
|
ATTR_FACE_URL,
|
||||||
ATTR_IS_KNOWN,
|
ATTR_IS_KNOWN,
|
||||||
ATTR_NAME,
|
|
||||||
ATTR_PERSONS,
|
ATTR_PERSONS,
|
||||||
DATA_DEVICE_IDS,
|
DATA_DEVICE_IDS,
|
||||||
DATA_PERSONS,
|
DATA_PERSONS,
|
||||||
|
@ -20,13 +19,13 @@ from .const import (
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
EVENT_TYPE_MAP = {
|
SUBEVENT_TYPE_MAP = {
|
||||||
"outdoor": "",
|
"outdoor": "",
|
||||||
"therm_mode": "",
|
"therm_mode": "",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async def handle_webhook(hass, webhook_id, request):
|
async def async_handle_webhook(hass, webhook_id, request):
|
||||||
"""Handle webhook callback."""
|
"""Handle webhook callback."""
|
||||||
try:
|
try:
|
||||||
data = await request.json()
|
data = await request.json()
|
||||||
|
@ -38,17 +37,17 @@ async def handle_webhook(hass, webhook_id, request):
|
||||||
|
|
||||||
event_type = data.get(ATTR_EVENT_TYPE)
|
event_type = data.get(ATTR_EVENT_TYPE)
|
||||||
|
|
||||||
if event_type in EVENT_TYPE_MAP:
|
if event_type in SUBEVENT_TYPE_MAP:
|
||||||
await async_send_event(hass, event_type, data)
|
async_send_event(hass, event_type, data)
|
||||||
|
|
||||||
for event_data in data.get(EVENT_TYPE_MAP[event_type], []):
|
for event_data in data.get(SUBEVENT_TYPE_MAP[event_type], []):
|
||||||
await async_evaluate_event(hass, event_data)
|
async_evaluate_event(hass, event_data)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
await async_evaluate_event(hass, data)
|
async_evaluate_event(hass, data)
|
||||||
|
|
||||||
|
|
||||||
async def async_evaluate_event(hass, event_data):
|
def async_evaluate_event(hass, event_data):
|
||||||
"""Evaluate events from webhook."""
|
"""Evaluate events from webhook."""
|
||||||
event_type = event_data.get(ATTR_EVENT_TYPE)
|
event_type = event_data.get(ATTR_EVENT_TYPE)
|
||||||
|
|
||||||
|
@ -62,13 +61,13 @@ async def async_evaluate_event(hass, event_data):
|
||||||
person_event_data[ATTR_IS_KNOWN] = person.get(ATTR_IS_KNOWN)
|
person_event_data[ATTR_IS_KNOWN] = person.get(ATTR_IS_KNOWN)
|
||||||
person_event_data[ATTR_FACE_URL] = person.get(ATTR_FACE_URL)
|
person_event_data[ATTR_FACE_URL] = person.get(ATTR_FACE_URL)
|
||||||
|
|
||||||
await async_send_event(hass, event_type, person_event_data)
|
async_send_event(hass, event_type, person_event_data)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
await async_send_event(hass, event_type, event_data)
|
async_send_event(hass, event_type, event_data)
|
||||||
|
|
||||||
|
|
||||||
async def async_send_event(hass, event_type, data):
|
def async_send_event(hass, event_type, data):
|
||||||
"""Send events."""
|
"""Send events."""
|
||||||
_LOGGER.debug("%s: %s", event_type, data)
|
_LOGGER.debug("%s: %s", event_type, data)
|
||||||
async_dispatcher_send(
|
async_dispatcher_send(
|
||||||
|
|
Loading…
Reference in New Issue