2020-01-11 11:20:00 +00:00
|
|
|
"""The Netatmo integration."""
|
2021-07-21 21:36:57 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-01-07 07:01:27 +00:00
|
|
|
from datetime import datetime
|
2021-10-26 14:09:10 +00:00
|
|
|
from http import HTTPStatus
|
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
|
|
|
|
2021-10-26 14:09:10 +00:00
|
|
|
import aiohttp
|
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
|
2022-05-17 04:09:48 +00:00
|
|
|
from homeassistant.components.application_credentials import (
|
|
|
|
ClientCredential,
|
|
|
|
async_import_client_credential,
|
|
|
|
)
|
2020-03-11 00:08:59 +00:00
|
|
|
from homeassistant.components.webhook import (
|
2022-01-14 12:01:14 +00:00
|
|
|
async_generate_url as webhook_generate_url,
|
2020-03-11 00:08:59 +00:00
|
|
|
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,
|
2022-01-26 14:46:08 +00:00
|
|
|
EVENT_HOMEASSISTANT_STARTED,
|
2020-03-11 00:08:59 +00:00
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
2020-02-06 17:26:51 +00:00
|
|
|
)
|
2022-01-06 02:14:42 +00:00
|
|
|
from homeassistant.core import CoreState, Event, HomeAssistant, ServiceCall
|
2021-10-26 14:09:10 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2021-05-20 12:59:19 +00:00
|
|
|
from homeassistant.helpers import (
|
|
|
|
aiohttp_client,
|
|
|
|
config_entry_oauth2_flow,
|
|
|
|
config_validation as cv,
|
|
|
|
)
|
2022-02-16 15:42:45 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
2021-01-28 14:30:10 +00:00
|
|
|
from homeassistant.helpers.event import async_call_later
|
2021-08-18 11:22:05 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2016-06-10 06:31:36 +00:00
|
|
|
|
2022-05-17 04:09:48 +00:00
|
|
|
from . import api
|
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,
|
2021-10-26 14:09:10 +00:00
|
|
|
NETATMO_SCOPES,
|
2021-05-20 17:28:21 +00:00
|
|
|
PLATFORMS,
|
|
|
|
WEBHOOK_DEACTIVATION,
|
|
|
|
WEBHOOK_PUSH_TYPE,
|
2020-03-11 00:08:59 +00:00
|
|
|
)
|
2020-08-04 18:46:46 +00:00
|
|
|
from .data_handler import NetatmoDataHandler
|
2021-02-25 15:53:59 +00:00
|
|
|
from .webhook import async_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(
|
2022-05-17 04:09:48 +00:00
|
|
|
vol.All(
|
|
|
|
cv.deprecated(DOMAIN),
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2022-01-04 15:52:27 +00:00
|
|
|
MAX_WEBHOOK_RETRIES = 3
|
|
|
|
|
2019-02-17 11:31:47 +00:00
|
|
|
|
2021-08-18 11:22:05 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2020-01-11 11:20:00 +00:00
|
|
|
"""Set up the Netatmo component."""
|
2021-01-28 14:30:10 +00:00
|
|
|
hass.data[DOMAIN] = {
|
|
|
|
DATA_PERSONS: {},
|
|
|
|
DATA_DEVICE_IDS: {},
|
|
|
|
DATA_SCHEDULES: {},
|
|
|
|
DATA_HOMES: {},
|
|
|
|
DATA_EVENTS: {},
|
|
|
|
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
|
|
|
|
2022-05-17 04:09:48 +00:00
|
|
|
await async_import_client_credential(
|
2020-01-11 11:20:00 +00:00
|
|
|
hass,
|
2022-05-17 04:09:48 +00:00
|
|
|
DOMAIN,
|
|
|
|
ClientCredential(
|
2020-01-11 11:20:00 +00:00
|
|
|
config[DOMAIN][CONF_CLIENT_ID],
|
|
|
|
config[DOMAIN][CONF_CLIENT_SECRET],
|
|
|
|
),
|
|
|
|
)
|
2022-05-17 04:09:48 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Configuration of Netatmo integration in YAML is deprecated and "
|
|
|
|
"will be removed in a future release; Your existing configuration "
|
|
|
|
"(including OAuth Application Credentials) have been imported into "
|
|
|
|
"the UI automatically and can be safely removed from your "
|
|
|
|
"configuration.yaml file"
|
|
|
|
)
|
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
|
|
|
|
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-01-11 11:20:00 +00:00
|
|
|
"""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)
|
|
|
|
|
2021-05-20 12:59:19 +00:00
|
|
|
session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
|
2021-10-26 14:09:10 +00:00
|
|
|
try:
|
|
|
|
await session.async_ensure_token_valid()
|
|
|
|
except aiohttp.ClientResponseError as ex:
|
|
|
|
_LOGGER.debug("API error: %s (%s)", ex.code, ex.message)
|
|
|
|
if ex.code in (
|
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
HTTPStatus.UNAUTHORIZED,
|
|
|
|
HTTPStatus.FORBIDDEN,
|
|
|
|
):
|
|
|
|
raise ConfigEntryAuthFailed("Token not valid, trigger renewal") from ex
|
|
|
|
raise ConfigEntryNotReady from ex
|
|
|
|
|
|
|
|
if sorted(session.token["scope"]) != sorted(NETATMO_SCOPES):
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Scope is invalid: %s != %s", session.token["scope"], NETATMO_SCOPES
|
|
|
|
)
|
|
|
|
raise ConfigEntryAuthFailed("Token scope not valid, trigger renewal")
|
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
2021-05-20 12:59:19 +00:00
|
|
|
AUTH: api.AsyncConfigEntryNetatmoAuth(
|
|
|
|
aiohttp_client.async_get_clientsession(hass), session
|
|
|
|
)
|
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
|
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2016-10-09 15:45:12 +00:00
|
|
|
|
2022-01-07 07:01:27 +00:00
|
|
|
async def unregister_webhook(
|
|
|
|
call_or_event_or_dt: ServiceCall | Event | datetime | None,
|
|
|
|
) -> None:
|
2020-08-04 18:46:46 +00:00
|
|
|
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])
|
2021-01-28 14:30:10 +00:00
|
|
|
async_dispatcher_send(
|
|
|
|
hass,
|
|
|
|
f"signal-{DOMAIN}-webhook-None",
|
2021-05-20 17:28:21 +00:00
|
|
|
{"type": "None", "data": {WEBHOOK_PUSH_TYPE: WEBHOOK_DEACTIVATION}},
|
2021-01-28 14:30:10 +00:00
|
|
|
)
|
2020-03-11 00:08:59 +00:00
|
|
|
webhook_unregister(hass, entry.data[CONF_WEBHOOK_ID])
|
2021-06-23 12:56:20 +00:00
|
|
|
try:
|
|
|
|
await hass.data[DOMAIN][entry.entry_id][AUTH].async_dropwebhook()
|
|
|
|
except pyatmo.ApiError:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"No webhook to be dropped for %s", entry.data[CONF_WEBHOOK_ID]
|
|
|
|
)
|
2020-03-11 00:08:59 +00:00
|
|
|
|
2022-01-07 07:01:27 +00:00
|
|
|
async def register_webhook(
|
|
|
|
call_or_event_or_dt: ServiceCall | Event | datetime | None,
|
|
|
|
) -> None:
|
2020-03-11 00:08:59 +00:00
|
|
|
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)
|
|
|
|
|
2022-01-14 15:35:35 +00:00
|
|
|
if cloud.async_active_subscription(hass):
|
2022-02-16 15:42:45 +00:00
|
|
|
webhook_url = await async_cloudhook_generate_url(hass, entry)
|
2020-03-11 00:08:59 +00:00
|
|
|
else:
|
2022-01-14 12:01:14 +00:00
|
|
|
webhook_url = webhook_generate_url(hass, entry.data[CONF_WEBHOOK_ID])
|
2020-03-11 00:08:59 +00:00
|
|
|
|
2021-05-20 17:28:21 +00:00
|
|
|
if entry.data[
|
|
|
|
"auth_implementation"
|
|
|
|
] == cloud.DOMAIN and not webhook_url.startswith("https://"):
|
2020-08-04 18:46:46 +00:00
|
|
|
_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
|
|
|
|
|
2022-02-16 15:42:45 +00:00
|
|
|
webhook_register(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
"Netatmo",
|
|
|
|
entry.data[CONF_WEBHOOK_ID],
|
|
|
|
async_handle_webhook,
|
|
|
|
)
|
2021-01-28 14:30:10 +00:00
|
|
|
|
2022-02-16 15:42:45 +00:00
|
|
|
try:
|
2021-05-20 12:59:19 +00:00
|
|
|
await hass.data[DOMAIN][entry.entry_id][AUTH].async_addwebhook(webhook_url)
|
2020-03-11 00:08:59 +00:00
|
|
|
_LOGGER.info("Register Netatmo webhook: %s", webhook_url)
|
|
|
|
except pyatmo.ApiError as err:
|
|
|
|
_LOGGER.error("Error during webhook registration - %s", err)
|
|
|
|
|
2021-04-20 09:03:07 +00:00
|
|
|
entry.async_on_unload(
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, unregister_webhook)
|
|
|
|
)
|
2020-03-11 00:08:59 +00:00
|
|
|
|
2022-02-16 15:42:45 +00:00
|
|
|
async def manage_cloudhook(state: cloud.CloudConnectionState) -> None:
|
|
|
|
if state is cloud.CloudConnectionState.CLOUD_CONNECTED:
|
|
|
|
await register_webhook(None)
|
|
|
|
|
|
|
|
if state is cloud.CloudConnectionState.CLOUD_DISCONNECTED:
|
|
|
|
await unregister_webhook(None)
|
|
|
|
async_call_later(hass, 30, register_webhook)
|
|
|
|
|
|
|
|
if cloud.async_active_subscription(hass):
|
|
|
|
if cloud.async_is_connected(hass):
|
|
|
|
await register_webhook(None)
|
|
|
|
cloud.async_listen_connection_change(hass, manage_cloudhook)
|
|
|
|
|
2020-08-04 18:46:46 +00:00
|
|
|
else:
|
2022-02-16 15:42:45 +00:00
|
|
|
if hass.state == CoreState.running:
|
|
|
|
await register_webhook(None)
|
|
|
|
else:
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, register_webhook)
|
2020-08-04 18:46:46 +00:00
|
|
|
|
|
|
|
hass.services.async_register(DOMAIN, "register_webhook", register_webhook)
|
|
|
|
hass.services.async_register(DOMAIN, "unregister_webhook", unregister_webhook)
|
|
|
|
|
2021-05-28 11:36:22 +00:00
|
|
|
entry.add_update_listener(async_config_entry_updated)
|
|
|
|
|
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
|
|
|
|
2022-02-16 15:42:45 +00:00
|
|
|
async def async_cloudhook_generate_url(hass: HomeAssistant, entry: ConfigEntry) -> str:
|
|
|
|
"""Generate the full URL for a webhook_id."""
|
|
|
|
if CONF_CLOUDHOOK_URL not in entry.data:
|
|
|
|
webhook_url = await cloud.async_create_cloudhook(
|
|
|
|
hass, entry.data[CONF_WEBHOOK_ID]
|
|
|
|
)
|
|
|
|
data = {**entry.data, CONF_CLOUDHOOK_URL: webhook_url}
|
|
|
|
hass.config_entries.async_update_entry(entry, data=data)
|
|
|
|
return webhook_url
|
|
|
|
return str(entry.data[CONF_CLOUDHOOK_URL])
|
|
|
|
|
|
|
|
|
2021-05-28 11:36:22 +00:00
|
|
|
async def async_config_entry_updated(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
|
|
|
"""Handle signals of config entry being updated."""
|
|
|
|
async_dispatcher_send(hass, f"signal-{DOMAIN}-public-update-{entry.entry_id}")
|
|
|
|
|
|
|
|
|
2021-07-21 21:36:57 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-01-11 11:20:00 +00:00
|
|
|
"""Unload a config entry."""
|
2021-10-26 14:09:10 +00:00
|
|
|
data = hass.data[DOMAIN]
|
|
|
|
|
2020-03-20 14:22:27 +00:00
|
|
|
if CONF_WEBHOOK_ID in entry.data:
|
2021-05-20 12:59:19 +00:00
|
|
|
webhook_unregister(hass, entry.data[CONF_WEBHOOK_ID])
|
2021-10-26 14:09:10 +00:00
|
|
|
await data[entry.entry_id][AUTH].async_dropwebhook()
|
2021-03-19 14:26:36 +00:00
|
|
|
_LOGGER.info("Unregister Netatmo webhook")
|
2020-08-04 18:46:46 +00:00
|
|
|
|
2021-04-27 18:42:21 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2020-03-20 14:22:27 +00:00
|
|
|
|
2021-10-26 14:09:10 +00:00
|
|
|
if unload_ok and entry.entry_id in data:
|
|
|
|
data.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
|
|
|
|
|
|
|
|
2021-07-21 21:36:57 +00:00
|
|
|
async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
2020-03-11 00:08:59 +00:00
|
|
|
"""Cleanup when entry is removed."""
|
2022-01-14 15:35:35 +00:00
|
|
|
if CONF_WEBHOOK_ID in entry.data and cloud.async_active_subscription(hass):
|
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
|