2020-02-20 23:29:46 +00:00
|
|
|
"""Support for SmartHab device integration."""
|
2019-05-13 10:35:31 +00:00
|
|
|
import logging
|
|
|
|
|
2019-10-20 10:14:07 +00:00
|
|
|
import pysmarthab
|
2019-05-13 10:35:31 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-07-05 19:20:51 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2019-05-13 10:35:31 +00:00
|
|
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
2021-04-22 17:58:02 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-07-05 19:20:51 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2019-05-13 10:35:31 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "smarthab"
|
|
|
|
DATA_HUB = "hub"
|
2021-03-02 20:43:59 +00:00
|
|
|
PLATFORMS = ["light", "cover"]
|
2019-05-13 10:35:31 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
2021-05-14 13:46:49 +00:00
|
|
|
vol.All(
|
|
|
|
cv.deprecated(DOMAIN),
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_EMAIL): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2019-05-13 10:35:31 +00:00
|
|
|
|
|
|
|
|
2020-07-05 19:20:51 +00:00
|
|
|
async def async_setup(hass, config) -> bool:
|
2019-05-13 10:35:31 +00:00
|
|
|
"""Set up the SmartHab platform."""
|
|
|
|
|
2020-07-05 19:20:51 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2019-05-13 10:35:31 +00:00
|
|
|
|
2020-10-03 01:26:08 +00:00
|
|
|
if DOMAIN not in config:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if not hass.config_entries.async_entries(DOMAIN):
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data=config[DOMAIN],
|
|
|
|
)
|
2020-07-05 19:20:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-04-22 17:58:02 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
2020-07-05 19:20:51 +00:00
|
|
|
"""Set up config entry for SmartHab integration."""
|
|
|
|
|
2019-05-13 10:35:31 +00:00
|
|
|
# Assign configuration variables
|
2020-07-05 19:20:51 +00:00
|
|
|
username = entry.data[CONF_EMAIL]
|
|
|
|
password = entry.data[CONF_PASSWORD]
|
2019-05-13 10:35:31 +00:00
|
|
|
|
|
|
|
# Setup connection with SmartHab API
|
|
|
|
hub = pysmarthab.SmartHab()
|
|
|
|
|
|
|
|
try:
|
2020-07-05 19:20:51 +00:00
|
|
|
await hub.async_login(username, password)
|
2020-08-28 11:50:32 +00:00
|
|
|
except pysmarthab.RequestFailedException as err:
|
2020-07-05 19:20:51 +00:00
|
|
|
_LOGGER.exception("Error while trying to reach SmartHab API")
|
2020-08-28 11:50:32 +00:00
|
|
|
raise ConfigEntryNotReady from err
|
2019-05-13 10:35:31 +00:00
|
|
|
|
|
|
|
# Pass hub object to child platforms
|
2020-07-05 19:20:51 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = {DATA_HUB: hub}
|
2019-05-13 10:35:31 +00:00
|
|
|
|
2021-04-27 20:10:04 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2019-05-13 10:35:31 +00:00
|
|
|
|
|
|
|
return True
|
2020-07-05 19:20:51 +00:00
|
|
|
|
|
|
|
|
2021-04-22 17:58:02 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
2020-07-05 19:20:51 +00:00
|
|
|
"""Unload config entry from SmartHab integration."""
|
2021-04-27 20:10:04 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
if unload_ok:
|
2020-07-05 19:20:51 +00:00
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2021-04-27 20:10:04 +00:00
|
|
|
return unload_ok
|