2020-03-11 00:08:59 +00:00
|
|
|
"""The Netatmo integration."""
|
|
|
|
import logging
|
|
|
|
|
2021-02-24 18:20:40 +00:00
|
|
|
from homeassistant.const import ATTR_DEVICE_ID, ATTR_ID
|
2020-08-07 07:25:59 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
2020-03-11 00:08:59 +00:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
ATTR_EVENT_TYPE,
|
|
|
|
ATTR_FACE_URL,
|
|
|
|
ATTR_IS_KNOWN,
|
|
|
|
ATTR_NAME,
|
|
|
|
ATTR_PERSONS,
|
2021-02-24 18:20:40 +00:00
|
|
|
DATA_DEVICE_IDS,
|
2020-03-11 00:08:59 +00:00
|
|
|
DATA_PERSONS,
|
|
|
|
DEFAULT_PERSON,
|
|
|
|
DOMAIN,
|
2021-02-24 18:20:40 +00:00
|
|
|
EVENT_ID_MAP,
|
2020-03-11 00:08:59 +00:00
|
|
|
NETATMO_EVENT,
|
|
|
|
)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-08-04 18:46:46 +00:00
|
|
|
EVENT_TYPE_MAP = {
|
|
|
|
"outdoor": "",
|
|
|
|
"therm_mode": "",
|
|
|
|
}
|
|
|
|
|
2020-03-11 00:08:59 +00:00
|
|
|
|
|
|
|
async def handle_webhook(hass, webhook_id, request):
|
|
|
|
"""Handle webhook callback."""
|
|
|
|
try:
|
|
|
|
data = await request.json()
|
2020-07-09 04:39:33 +00:00
|
|
|
except ValueError as err:
|
|
|
|
_LOGGER.error("Error in data: %s", err)
|
2020-03-11 00:08:59 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
_LOGGER.debug("Got webhook data: %s", data)
|
|
|
|
|
|
|
|
event_type = data.get(ATTR_EVENT_TYPE)
|
|
|
|
|
2020-08-07 07:25:59 +00:00
|
|
|
if event_type in EVENT_TYPE_MAP:
|
2021-02-24 18:20:40 +00:00
|
|
|
await async_send_event(hass, event_type, data)
|
2020-08-07 07:25:59 +00:00
|
|
|
|
2020-08-04 18:46:46 +00:00
|
|
|
for event_data in data.get(EVENT_TYPE_MAP[event_type], []):
|
2021-02-24 18:20:40 +00:00
|
|
|
await async_evaluate_event(hass, event_data)
|
2020-08-04 18:46:46 +00:00
|
|
|
|
2020-03-11 00:08:59 +00:00
|
|
|
else:
|
2021-02-24 18:20:40 +00:00
|
|
|
await async_evaluate_event(hass, data)
|
2020-03-11 00:08:59 +00:00
|
|
|
|
|
|
|
|
2021-02-24 18:20:40 +00:00
|
|
|
async def async_evaluate_event(hass, event_data):
|
2020-03-11 00:08:59 +00:00
|
|
|
"""Evaluate events from webhook."""
|
|
|
|
event_type = event_data.get(ATTR_EVENT_TYPE)
|
|
|
|
|
|
|
|
if event_type == "person":
|
|
|
|
for person in event_data.get(ATTR_PERSONS):
|
|
|
|
person_event_data = dict(event_data)
|
|
|
|
person_event_data[ATTR_ID] = person.get(ATTR_ID)
|
|
|
|
person_event_data[ATTR_NAME] = hass.data[DOMAIN][DATA_PERSONS].get(
|
|
|
|
person_event_data[ATTR_ID], DEFAULT_PERSON
|
|
|
|
)
|
|
|
|
person_event_data[ATTR_IS_KNOWN] = person.get(ATTR_IS_KNOWN)
|
|
|
|
person_event_data[ATTR_FACE_URL] = person.get(ATTR_FACE_URL)
|
2020-08-07 07:25:59 +00:00
|
|
|
|
2021-02-24 18:20:40 +00:00
|
|
|
await async_send_event(hass, event_type, person_event_data)
|
2020-08-07 07:25:59 +00:00
|
|
|
|
2020-03-11 00:08:59 +00:00
|
|
|
else:
|
2021-02-24 18:20:40 +00:00
|
|
|
await async_send_event(hass, event_type, event_data)
|
2020-08-07 07:25:59 +00:00
|
|
|
|
|
|
|
|
2021-02-24 18:20:40 +00:00
|
|
|
async def async_send_event(hass, event_type, data):
|
2020-08-07 07:25:59 +00:00
|
|
|
"""Send events."""
|
2021-02-24 18:20:40 +00:00
|
|
|
_LOGGER.debug("%s: %s", event_type, data)
|
2020-08-07 07:25:59 +00:00
|
|
|
async_dispatcher_send(
|
|
|
|
hass,
|
|
|
|
f"signal-{DOMAIN}-webhook-{event_type}",
|
|
|
|
{"type": event_type, "data": data},
|
|
|
|
)
|
2021-02-24 18:20:40 +00:00
|
|
|
|
|
|
|
if event_type not in EVENT_ID_MAP:
|
|
|
|
return
|
|
|
|
|
|
|
|
data_device_id = data[EVENT_ID_MAP[event_type]]
|
|
|
|
|
|
|
|
hass.bus.async_fire(
|
|
|
|
event_type=NETATMO_EVENT,
|
|
|
|
event_data={
|
|
|
|
"type": event_type,
|
|
|
|
"data": data,
|
|
|
|
ATTR_DEVICE_ID: hass.data[DOMAIN][DATA_DEVICE_IDS].get(data_device_id),
|
|
|
|
},
|
|
|
|
)
|