2020-01-11 11:20:00 +00:00
|
|
|
"""The Netatmo integration."""
|
|
|
|
import asyncio
|
2019-12-09 10:50:59 +00:00
|
|
|
import logging
|
2020-03-11 00:08:59 +00:00
|
|
|
import secrets
|
2016-09-11 19:27:58 +00:00
|
|
|
|
2020-03-11 00:08:59 +00:00
|
|
|
import pyatmo
|
2016-09-11 19:27:58 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-03-11 00:08:59 +00:00
|
|
|
from homeassistant.components import cloud
|
|
|
|
from homeassistant.components.webhook import (
|
|
|
|
async_register as webhook_register,
|
|
|
|
async_unregister as webhook_unregister,
|
|
|
|
)
|
2020-01-11 11:20:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-02-06 17:26:51 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_CLIENT_ID,
|
|
|
|
CONF_CLIENT_SECRET,
|
2020-03-11 00:08:59 +00:00
|
|
|
CONF_WEBHOOK_ID,
|
|
|
|
EVENT_HOMEASSISTANT_START,
|
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
2020-02-06 17:26:51 +00:00
|
|
|
)
|
2020-08-04 18:46:46 +00:00
|
|
|
from homeassistant.core import CoreState, HomeAssistant
|
2020-01-11 11:20:00 +00:00
|
|
|
from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv
|
2016-06-10 06:31:36 +00:00
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
from . import api, config_flow
|
2020-03-11 00:08:59 +00:00
|
|
|
from .const import (
|
|
|
|
AUTH,
|
|
|
|
CONF_CLOUDHOOK_URL,
|
2020-09-04 18:21:42 +00:00
|
|
|
DATA_CAMERAS,
|
2020-03-20 14:22:27 +00:00
|
|
|
DATA_DEVICE_IDS,
|
2020-09-04 18:21:42 +00:00
|
|
|
DATA_EVENTS,
|
2020-08-04 18:46:46 +00:00
|
|
|
DATA_HANDLER,
|
|
|
|
DATA_HOMES,
|
2020-03-11 00:08:59 +00:00
|
|
|
DATA_PERSONS,
|
2020-08-04 18:46:46 +00:00
|
|
|
DATA_SCHEDULES,
|
2020-03-11 00:08:59 +00:00
|
|
|
DOMAIN,
|
|
|
|
OAUTH2_AUTHORIZE,
|
|
|
|
OAUTH2_TOKEN,
|
|
|
|
)
|
2020-08-04 18:46:46 +00:00
|
|
|
from .data_handler import NetatmoDataHandler
|
2020-03-11 00:08:59 +00:00
|
|
|
from .webhook import handle_webhook
|
2019-04-26 15:15:37 +00:00
|
|
|
|
2016-06-10 06:31:36 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
2020-01-11 11:20:00 +00:00
|
|
|
vol.Required(CONF_CLIENT_ID): cv.string,
|
|
|
|
vol.Required(CONF_CLIENT_SECRET): cv.string,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
|
|
|
|
2020-03-11 16:25:19 +00:00
|
|
|
PLATFORMS = ["camera", "climate", "sensor"]
|
2016-06-10 06:31:36 +00:00
|
|
|
|
2019-02-17 11:31:47 +00:00
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
|
|
"""Set up the Netatmo component."""
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
hass.data[DOMAIN][DATA_PERSONS] = {}
|
2020-03-20 14:22:27 +00:00
|
|
|
hass.data[DOMAIN][DATA_DEVICE_IDS] = {}
|
2020-08-04 18:46:46 +00:00
|
|
|
hass.data[DOMAIN][DATA_SCHEDULES] = {}
|
|
|
|
hass.data[DOMAIN][DATA_HOMES] = {}
|
2020-09-04 18:21:42 +00:00
|
|
|
hass.data[DOMAIN][DATA_EVENTS] = {}
|
|
|
|
hass.data[DOMAIN][DATA_CAMERAS] = {}
|
2019-02-17 11:31:47 +00:00
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
if DOMAIN not in config:
|
|
|
|
return True
|
2019-12-06 16:45:27 +00:00
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
config_flow.NetatmoFlowHandler.async_register_implementation(
|
|
|
|
hass,
|
|
|
|
config_entry_oauth2_flow.LocalOAuth2Implementation(
|
|
|
|
hass,
|
2019-12-06 16:45:27 +00:00
|
|
|
DOMAIN,
|
2020-01-11 11:20:00 +00:00
|
|
|
config[DOMAIN][CONF_CLIENT_ID],
|
|
|
|
config[DOMAIN][CONF_CLIENT_SECRET],
|
|
|
|
OAUTH2_AUTHORIZE,
|
|
|
|
OAUTH2_TOKEN,
|
|
|
|
),
|
|
|
|
)
|
2019-12-06 16:45:27 +00:00
|
|
|
|
2016-06-10 06:31:36 +00:00
|
|
|
return True
|
2016-10-09 15:45:12 +00:00
|
|
|
|
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Set up Netatmo from a config entry."""
|
2020-08-27 11:56:20 +00:00
|
|
|
implementation = (
|
|
|
|
await config_entry_oauth2_flow.async_get_config_entry_implementation(
|
|
|
|
hass, entry
|
|
|
|
)
|
2020-01-11 11:20:00 +00:00
|
|
|
)
|
2019-02-17 11:31:47 +00:00
|
|
|
|
2020-07-09 04:39:33 +00:00
|
|
|
# Set unique id if non was set (migration)
|
|
|
|
if not entry.unique_id:
|
|
|
|
hass.config_entries.async_update_entry(entry, unique_id=DOMAIN)
|
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
|
|
AUTH: api.ConfigEntryNetatmoAuth(hass, entry, implementation)
|
2019-02-17 11:31:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 18:46:46 +00:00
|
|
|
data_handler = NetatmoDataHandler(hass, entry)
|
|
|
|
await data_handler.async_setup()
|
|
|
|
hass.data[DOMAIN][entry.entry_id][DATA_HANDLER] = data_handler
|
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
for component in PLATFORMS:
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(entry, component)
|
|
|
|
)
|
2016-10-09 15:45:12 +00:00
|
|
|
|
2020-08-04 18:46:46 +00:00
|
|
|
async def unregister_webhook(_):
|
|
|
|
if CONF_WEBHOOK_ID not in entry.data:
|
|
|
|
return
|
2020-03-11 00:08:59 +00:00
|
|
|
_LOGGER.debug("Unregister Netatmo webhook (%s)", entry.data[CONF_WEBHOOK_ID])
|
|
|
|
webhook_unregister(hass, entry.data[CONF_WEBHOOK_ID])
|
|
|
|
|
|
|
|
async def register_webhook(event):
|
|
|
|
if CONF_WEBHOOK_ID not in entry.data:
|
|
|
|
data = {**entry.data, CONF_WEBHOOK_ID: secrets.token_hex()}
|
|
|
|
hass.config_entries.async_update_entry(entry, data=data)
|
|
|
|
|
|
|
|
if hass.components.cloud.async_active_subscription():
|
|
|
|
if CONF_CLOUDHOOK_URL not in entry.data:
|
|
|
|
webhook_url = await hass.components.cloud.async_create_cloudhook(
|
|
|
|
entry.data[CONF_WEBHOOK_ID]
|
|
|
|
)
|
|
|
|
data = {**entry.data, CONF_CLOUDHOOK_URL: webhook_url}
|
|
|
|
hass.config_entries.async_update_entry(entry, data=data)
|
|
|
|
else:
|
|
|
|
webhook_url = entry.data[CONF_CLOUDHOOK_URL]
|
|
|
|
else:
|
|
|
|
webhook_url = hass.components.webhook.async_generate_url(
|
|
|
|
entry.data[CONF_WEBHOOK_ID]
|
|
|
|
)
|
|
|
|
|
2020-08-04 18:46:46 +00:00
|
|
|
if entry.data["auth_implementation"] == "cloud" and not webhook_url.startswith(
|
|
|
|
"https://"
|
|
|
|
):
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Webhook not registered - "
|
|
|
|
"https and port 443 is required to register the webhook"
|
2020-03-11 00:08:59 +00:00
|
|
|
)
|
2020-08-04 18:46:46 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2020-03-11 00:08:59 +00:00
|
|
|
webhook_register(
|
|
|
|
hass, DOMAIN, "Netatmo", entry.data[CONF_WEBHOOK_ID], handle_webhook
|
|
|
|
)
|
2020-08-04 18:46:46 +00:00
|
|
|
await hass.async_add_executor_job(
|
|
|
|
hass.data[DOMAIN][entry.entry_id][AUTH].addwebhook, webhook_url
|
|
|
|
)
|
2020-03-11 00:08:59 +00:00
|
|
|
_LOGGER.info("Register Netatmo webhook: %s", webhook_url)
|
2020-08-04 18:46:46 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(entry, "light")
|
|
|
|
)
|
2020-03-11 00:08:59 +00:00
|
|
|
except pyatmo.ApiError as err:
|
|
|
|
_LOGGER.error("Error during webhook registration - %s", err)
|
|
|
|
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, unregister_webhook)
|
|
|
|
|
2020-08-04 18:46:46 +00:00
|
|
|
if hass.state == CoreState.running:
|
|
|
|
await register_webhook(None)
|
|
|
|
else:
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, register_webhook)
|
|
|
|
|
|
|
|
hass.services.async_register(DOMAIN, "register_webhook", register_webhook)
|
|
|
|
hass.services.async_register(DOMAIN, "unregister_webhook", unregister_webhook)
|
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
return True
|
2016-10-09 15:45:12 +00:00
|
|
|
|
2016-12-06 05:35:33 +00:00
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Unload a config entry."""
|
2020-03-20 14:22:27 +00:00
|
|
|
if CONF_WEBHOOK_ID in entry.data:
|
|
|
|
await hass.async_add_executor_job(
|
|
|
|
hass.data[DOMAIN][entry.entry_id][AUTH].dropwebhook
|
|
|
|
)
|
2020-08-04 18:46:46 +00:00
|
|
|
_LOGGER.info("Unregister Netatmo webhook.")
|
|
|
|
|
|
|
|
await hass.data[DOMAIN][entry.entry_id][DATA_HANDLER].async_cleanup()
|
2020-03-20 14:22:27 +00:00
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
unload_ok = all(
|
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
|
|
|
hass.config_entries.async_forward_entry_unload(entry, component)
|
|
|
|
for component in PLATFORMS
|
|
|
|
]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-01-11 11:20:00 +00:00
|
|
|
)
|
2020-03-20 14:22:27 +00:00
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2016-12-06 05:35:33 +00:00
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
return unload_ok
|
2020-03-11 00:08:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Cleanup when entry is removed."""
|
2020-08-04 18:46:46 +00:00
|
|
|
if (
|
|
|
|
CONF_WEBHOOK_ID in entry.data
|
|
|
|
and hass.components.cloud.async_active_subscription()
|
|
|
|
):
|
2020-03-11 00:08:59 +00:00
|
|
|
try:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Removing Netatmo cloudhook (%s)", entry.data[CONF_WEBHOOK_ID]
|
|
|
|
)
|
|
|
|
await cloud.async_delete_cloudhook(hass, entry.data[CONF_WEBHOOK_ID])
|
|
|
|
except cloud.CloudNotAvailable:
|
|
|
|
pass
|