2020-04-08 21:29:59 +00:00
|
|
|
"""The flume integration."""
|
|
|
|
from pyflume import FlumeAuth, FlumeDeviceList
|
|
|
|
from requests import Session
|
|
|
|
from requests.exceptions import RequestException
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-05-30 15:27:20 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_CLIENT_ID,
|
|
|
|
CONF_CLIENT_SECRET,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2020-04-08 21:29:59 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-03 17:30:22 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2020-04-08 21:29:59 +00:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
BASE_TOKEN_FILENAME,
|
|
|
|
DOMAIN,
|
|
|
|
FLUME_AUTH,
|
|
|
|
FLUME_DEVICES,
|
|
|
|
FLUME_HTTP_SESSION,
|
|
|
|
PLATFORMS,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-05-03 17:30:22 +00:00
|
|
|
def _setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Config entry set up in executor."""
|
2020-04-08 21:29:59 +00:00
|
|
|
config = entry.data
|
|
|
|
|
|
|
|
username = config[CONF_USERNAME]
|
|
|
|
password = config[CONF_PASSWORD]
|
|
|
|
client_id = config[CONF_CLIENT_ID]
|
|
|
|
client_secret = config[CONF_CLIENT_SECRET]
|
|
|
|
flume_token_full_path = hass.config.path(f"{BASE_TOKEN_FILENAME}-{username}")
|
|
|
|
|
|
|
|
http_session = Session()
|
|
|
|
|
|
|
|
try:
|
2021-05-03 17:30:22 +00:00
|
|
|
flume_auth = FlumeAuth(
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
client_id,
|
|
|
|
client_secret,
|
|
|
|
flume_token_file=flume_token_full_path,
|
|
|
|
http_session=http_session,
|
2020-04-08 21:29:59 +00:00
|
|
|
)
|
2021-05-03 17:30:22 +00:00
|
|
|
flume_devices = FlumeDeviceList(flume_auth, http_session=http_session)
|
2020-08-28 11:50:32 +00:00
|
|
|
except RequestException as ex:
|
|
|
|
raise ConfigEntryNotReady from ex
|
2021-07-15 04:44:57 +00:00
|
|
|
except Exception as ex:
|
2021-05-03 17:30:22 +00:00
|
|
|
raise ConfigEntryAuthFailed from ex
|
|
|
|
|
|
|
|
return flume_auth, flume_devices, http_session
|
|
|
|
|
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-05-03 17:30:22 +00:00
|
|
|
"""Set up flume from a config entry."""
|
|
|
|
|
|
|
|
flume_auth, flume_devices, http_session = await hass.async_add_executor_job(
|
|
|
|
_setup_entry, hass, entry
|
|
|
|
)
|
2020-04-08 21:29:59 +00:00
|
|
|
|
2021-05-03 17:30:22 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
|
2020-04-08 21:29:59 +00:00
|
|
|
FLUME_DEVICES: flume_devices,
|
|
|
|
FLUME_AUTH: flume_auth,
|
|
|
|
FLUME_HTTP_SESSION: http_session,
|
|
|
|
}
|
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2020-04-08 21:29:59 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-10-06 08:48:11 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-04-08 21:29:59 +00:00
|
|
|
"""Unload a config entry."""
|
2021-04-27 06:46:49 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2020-04-08 21:29:59 +00:00
|
|
|
|
|
|
|
hass.data[DOMAIN][entry.entry_id][FLUME_HTTP_SESSION].close()
|
|
|
|
|
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|