core/homeassistant/components/totalconnect/__init__.py

83 lines
2.3 KiB
Python

"""The totalconnect component."""
import asyncio
import logging
from total_connect_client import TotalConnectClient
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
import homeassistant.helpers.config_validation as cv
from .const import CONF_USERCODES, DOMAIN
_LOGGER = logging.getLogger(__name__)
PLATFORMS = ["alarm_control_panel", "binary_sensor"]
CONFIG_SCHEMA = vol.Schema(
vol.All(
cv.deprecated(DOMAIN),
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
}
)
},
),
extra=vol.ALLOW_EXTRA,
)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up upon config entry in user interface."""
conf = entry.data
username = conf[CONF_USERNAME]
password = conf[CONF_PASSWORD]
if CONF_USERCODES not in conf:
# should only happen for those who used UI before we added usercodes
raise ConfigEntryAuthFailed("No usercodes in TotalConnect configuration")
temp_codes = conf[CONF_USERCODES]
usercodes = {}
for code in temp_codes:
usercodes[int(code)] = temp_codes[code]
client = await hass.async_add_executor_job(
TotalConnectClient.TotalConnectClient, username, password, usercodes
)
if not client.is_valid_credentials():
raise ConfigEntryAuthFailed("TotalConnect authentication failed")
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = client
for platform in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
return True
async def async_unload_entry(hass, entry: ConfigEntry):
"""Unload a config entry."""
unload_ok = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
]
)
)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok