Clean up Netatmo webhook handler (#47037)

pull/47055/head
Tobias Sauerwein 2021-02-25 16:53:59 +01:00 committed by GitHub
parent 125206adbf
commit 4e09d7ee0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 17 deletions

View File

@ -43,7 +43,7 @@ from .const import (
OAUTH2_TOKEN,
)
from .data_handler import NetatmoDataHandler
from .webhook import handle_webhook
from .webhook import async_handle_webhook
_LOGGER = logging.getLogger(__name__)
@ -157,18 +157,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
try:
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):
"""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(

View File

@ -1,14 +1,13 @@
"""The Netatmo integration."""
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 .const import (
ATTR_EVENT_TYPE,
ATTR_FACE_URL,
ATTR_IS_KNOWN,
ATTR_NAME,
ATTR_PERSONS,
DATA_DEVICE_IDS,
DATA_PERSONS,
@ -20,13 +19,13 @@ from .const import (
_LOGGER = logging.getLogger(__name__)
EVENT_TYPE_MAP = {
SUBEVENT_TYPE_MAP = {
"outdoor": "",
"therm_mode": "",
}
async def handle_webhook(hass, webhook_id, request):
async def async_handle_webhook(hass, webhook_id, request):
"""Handle webhook callback."""
try:
data = await request.json()
@ -38,17 +37,17 @@ async def handle_webhook(hass, webhook_id, request):
event_type = data.get(ATTR_EVENT_TYPE)
if event_type in EVENT_TYPE_MAP:
await async_send_event(hass, event_type, data)
if event_type in SUBEVENT_TYPE_MAP:
async_send_event(hass, event_type, data)
for event_data in data.get(EVENT_TYPE_MAP[event_type], []):
await async_evaluate_event(hass, event_data)
for event_data in data.get(SUBEVENT_TYPE_MAP[event_type], []):
async_evaluate_event(hass, event_data)
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."""
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_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:
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."""
_LOGGER.debug("%s: %s", event_type, data)
async_dispatcher_send(