2020-01-11 11:20:00 +00:00
|
|
|
"""The Netatmo integration."""
|
|
|
|
import asyncio
|
2019-12-09 10:50:59 +00:00
|
|
|
import logging
|
2016-09-11 19:27:58 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
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,
|
|
|
|
CONF_DISCOVERY,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2020-01-11 11:20:00 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
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
|
|
|
|
from .const import AUTH, DATA_PERSONS, DOMAIN, OAUTH2_AUTHORIZE, OAUTH2_TOKEN
|
2019-04-26 15:15:37 +00:00
|
|
|
|
2016-06-10 06:31:36 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-02-06 17:26:51 +00:00
|
|
|
CONF_SECRET_KEY = "secret_key"
|
|
|
|
CONF_WEBHOOKS = "webhooks"
|
|
|
|
|
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,
|
2020-02-06 17:26:51 +00:00
|
|
|
cv.deprecated(CONF_SECRET_KEY): cv.match_all,
|
|
|
|
cv.deprecated(CONF_USERNAME): cv.match_all,
|
|
|
|
cv.deprecated(CONF_WEBHOOKS): cv.match_all,
|
|
|
|
cv.deprecated(CONF_DISCOVERY): cv.match_all,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
|
|
|
|
2020-01-11 11:20:00 +00:00
|
|
|
PLATFORMS = ["binary_sensor", "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] = {}
|
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."""
|
|
|
|
implementation = await config_entry_oauth2_flow.async_get_config_entry_implementation(
|
|
|
|
hass, entry
|
|
|
|
)
|
2019-02-17 11:31:47 +00:00
|
|
|
|
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-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-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."""
|
|
|
|
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
|
|
|
)
|
|
|
|
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
|