2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Spider Smart devices."""
|
2018-07-25 09:51:48 +00:00
|
|
|
import logging
|
|
|
|
|
2020-11-13 15:03:08 +00:00
|
|
|
from spiderpy.spiderapi import SpiderApi, SpiderApiException, UnauthorizedException
|
2018-07-25 09:51:48 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2022-01-02 15:29:52 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME
|
2022-01-02 15:29:52 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-11-13 15:03:08 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2018-07-25 09:51:48 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-02 15:29:52 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2018-07-25 09:51:48 +00:00
|
|
|
|
2020-08-04 20:37:20 +00:00
|
|
|
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, PLATFORMS
|
2018-07-25 09:51:48 +00:00
|
|
|
|
2020-08-04 20:37:20 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-07-27 04:43:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
2021-10-18 16:56:10 +00:00
|
|
|
vol.All(
|
|
|
|
cv.deprecated(DOMAIN),
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
|
|
|
|
): cv.time_period,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2018-07-25 09:51:48 +00:00
|
|
|
|
|
|
|
|
2022-01-02 15:29:52 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2020-08-04 20:37:20 +00:00
|
|
|
"""Set up a config entry."""
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
if DOMAIN not in config:
|
|
|
|
return True
|
2018-07-25 09:51:48 +00:00
|
|
|
|
2020-08-04 20:37:20 +00:00
|
|
|
conf = config[DOMAIN]
|
2018-07-25 09:51:48 +00:00
|
|
|
|
2020-08-04 20:37:20 +00:00
|
|
|
if not hass.config_entries.async_entries(DOMAIN):
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
|
|
|
|
)
|
|
|
|
)
|
2018-07-25 09:51:48 +00:00
|
|
|
|
2020-08-04 20:37:20 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-01-02 15:29:52 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-08-04 20:37:20 +00:00
|
|
|
"""Set up Spider via config entry."""
|
|
|
|
try:
|
2020-11-13 15:03:08 +00:00
|
|
|
api = await hass.async_add_executor_job(
|
|
|
|
SpiderApi,
|
|
|
|
entry.data[CONF_USERNAME],
|
|
|
|
entry.data[CONF_PASSWORD],
|
|
|
|
entry.data[CONF_SCAN_INTERVAL],
|
2020-08-04 20:37:20 +00:00
|
|
|
)
|
2018-07-25 09:51:48 +00:00
|
|
|
except UnauthorizedException:
|
2020-11-13 15:03:08 +00:00
|
|
|
_LOGGER.error("Authorization failed")
|
2018-07-25 09:51:48 +00:00
|
|
|
return False
|
2020-11-13 15:03:08 +00:00
|
|
|
except SpiderApiException as err:
|
|
|
|
_LOGGER.error("Can't connect to the Spider API: %s", err)
|
|
|
|
raise ConfigEntryNotReady from err
|
|
|
|
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = api
|
2020-08-04 20:37:20 +00:00
|
|
|
|
2021-04-27 20:10:04 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2020-08-04 20:37:20 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-01-02 15:29:52 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-08-04 20:37:20 +00:00
|
|
|
"""Unload Spider entry."""
|
2021-04-27 20:10:04 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2020-08-04 20:37:20 +00:00
|
|
|
if not unload_ok:
|
|
|
|
return False
|
|
|
|
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return True
|