2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Neato botvac connected vacuum cleaners."""
|
2019-12-09 10:05:22 +00:00
|
|
|
import logging
|
2016-11-19 08:14:40 +00:00
|
|
|
|
2021-08-07 10:22:08 +00:00
|
|
|
import aiohttp
|
2022-05-20 04:22:37 +00:00
|
|
|
from pybotvac import Account
|
2020-12-16 22:39:41 +00:00
|
|
|
from pybotvac.exceptions import NeatoException
|
2019-12-09 10:05:22 +00:00
|
|
|
import voluptuous as vol
|
2016-11-19 08:14:40 +00:00
|
|
|
|
2022-05-20 04:22:37 +00:00
|
|
|
from homeassistant.components.application_credentials import (
|
|
|
|
ClientCredential,
|
|
|
|
async_import_client_credential,
|
|
|
|
)
|
2021-04-10 05:41:29 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-06 03:06:35 +00:00
|
|
|
from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET, CONF_TOKEN, Platform
|
2021-04-22 20:23:36 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-10 05:41:29 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2020-12-16 22:39:41 +00:00
|
|
|
from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv
|
2021-04-22 20:23:36 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2016-11-19 08:14:40 +00:00
|
|
|
|
2022-05-20 04:22:37 +00:00
|
|
|
from . import api
|
2021-08-07 10:22:08 +00:00
|
|
|
from .const import NEATO_CONFIG, NEATO_DOMAIN, NEATO_LOGIN
|
|
|
|
from .hub import NeatoHub
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2019-02-14 04:35:12 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
2022-05-20 04:22:37 +00:00
|
|
|
vol.All(
|
|
|
|
cv.deprecated(NEATO_DOMAIN),
|
|
|
|
{
|
|
|
|
NEATO_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,
|
|
|
|
)
|
|
|
|
|
2021-12-06 03:06:35 +00:00
|
|
|
PLATFORMS = [Platform.CAMERA, Platform.VACUUM, Platform.SWITCH, Platform.SENSOR]
|
2020-12-16 22:39:41 +00:00
|
|
|
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2021-04-22 20:23:36 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the Neato component."""
|
2020-12-16 22:39:41 +00:00
|
|
|
hass.data[NEATO_DOMAIN] = {}
|
2019-10-06 11:05:51 +00:00
|
|
|
|
|
|
|
if NEATO_DOMAIN not in config:
|
|
|
|
return True
|
|
|
|
|
|
|
|
hass.data[NEATO_CONFIG] = config[NEATO_DOMAIN]
|
2022-05-20 04:22:37 +00:00
|
|
|
await async_import_client_credential(
|
2020-12-16 22:39:41 +00:00
|
|
|
hass,
|
2022-05-20 04:22:37 +00:00
|
|
|
NEATO_DOMAIN,
|
|
|
|
ClientCredential(
|
2020-12-16 22:39:41 +00:00
|
|
|
config[NEATO_DOMAIN][CONF_CLIENT_ID],
|
|
|
|
config[NEATO_DOMAIN][CONF_CLIENT_SECRET],
|
|
|
|
),
|
|
|
|
)
|
2022-05-20 04:22:37 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Configuration of Neato integration in YAML is deprecated and "
|
|
|
|
"will be removed in a future release; Your existing OAuth "
|
|
|
|
"Application Credentials have been imported into the UI "
|
|
|
|
"automatically and can be safely removed from your "
|
|
|
|
"configuration.yaml file"
|
|
|
|
)
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2020-12-16 22:39:41 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-04-22 20:23:36 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-12-16 22:39:41 +00:00
|
|
|
"""Set up config entry."""
|
|
|
|
if CONF_TOKEN not in entry.data:
|
2021-04-10 05:41:29 +00:00
|
|
|
raise ConfigEntryAuthFailed
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2020-12-16 22:39:41 +00:00
|
|
|
implementation = (
|
|
|
|
await config_entry_oauth2_flow.async_get_config_entry_implementation(
|
|
|
|
hass, entry
|
|
|
|
)
|
|
|
|
)
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2021-08-07 10:22:08 +00:00
|
|
|
session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
|
|
|
|
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 (401, 403):
|
|
|
|
raise ConfigEntryAuthFailed("Token not valid, trigger renewal") from ex
|
2021-08-07 17:15:25 +00:00
|
|
|
raise ConfigEntryNotReady from ex
|
2021-08-07 10:22:08 +00:00
|
|
|
|
2021-07-09 17:12:51 +00:00
|
|
|
neato_session = api.ConfigEntryAuth(hass, entry, implementation)
|
2020-12-16 22:39:41 +00:00
|
|
|
hass.data[NEATO_DOMAIN][entry.entry_id] = neato_session
|
|
|
|
hub = NeatoHub(hass, Account(neato_session))
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2021-08-07 10:22:08 +00:00
|
|
|
await hub.async_update_entry_unique_id(entry)
|
|
|
|
|
2019-10-06 18:10:11 +00:00
|
|
|
try:
|
|
|
|
await hass.async_add_executor_job(hub.update_robots)
|
2020-12-16 22:39:41 +00:00
|
|
|
except NeatoException as ex:
|
2019-10-06 18:10:11 +00:00
|
|
|
_LOGGER.debug("Failed to connect to Neato API")
|
2020-08-28 11:50:32 +00:00
|
|
|
raise ConfigEntryNotReady from ex
|
2020-07-14 20:59:03 +00:00
|
|
|
|
|
|
|
hass.data[NEATO_LOGIN] = hub
|
2019-10-06 18:10:11 +00:00
|
|
|
|
2021-04-27 18:42:21 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2019-10-06 11:05:51 +00:00
|
|
|
|
|
|
|
return True
|
2016-11-19 08:14:40 +00:00
|
|
|
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2021-08-07 10:22:08 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2019-10-06 11:05:51 +00:00
|
|
|
"""Unload config entry."""
|
2021-04-27 18:42:21 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2020-12-16 22:39:41 +00:00
|
|
|
if unload_ok:
|
|
|
|
hass.data[NEATO_DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|