core/homeassistant/components/spider/__init__.py

87 lines
2.5 KiB
Python
Raw Normal View History

"""Support for Spider Smart devices."""
import logging
from spiderpy.spiderapi import SpiderApi, SpiderApiException, UnauthorizedException
import voluptuous as vol
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
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
2020-08-04 20:37:20 +00:00
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, PLATFORMS
2020-08-04 20:37:20 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
CONFIG_SCHEMA = vol.Schema(
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,
)
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
2020-08-04 20:37:20 +00:00
conf = config[DOMAIN]
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
)
)
2020-08-04 20:37:20 +00:00
return True
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:
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
)
except UnauthorizedException:
_LOGGER.error("Authorization failed")
return False
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
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
2020-08-04 20:37:20 +00:00
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2020-08-04 20:37:20 +00:00
"""Unload Spider entry."""
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