2019-03-17 03:44:05 +00:00
|
|
|
"""The totalconnect component."""
|
2021-10-27 17:15:13 +00:00
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from total_connect_client.client import TotalConnectClient
|
|
|
|
from total_connect_client.exceptions import AuthenticationError, TotalConnectError
|
2019-07-14 07:24:40 +00:00
|
|
|
|
2021-04-10 05:41:29 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-04 13:10:01 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
2020-04-13 02:29:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-10 05:41:29 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
2019-12-09 13:41:48 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-10-27 17:15:13 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
2019-07-14 07:24:40 +00:00
|
|
|
|
2021-02-22 09:39:10 +00:00
|
|
|
from .const import CONF_USERCODES, DOMAIN
|
2020-04-13 02:29:57 +00:00
|
|
|
|
2021-12-04 13:10:01 +00:00
|
|
|
PLATFORMS = [Platform.ALARM_CONTROL_PANEL, Platform.BINARY_SENSOR]
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2021-12-21 11:46:10 +00:00
|
|
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
2021-10-27 17:15:13 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=30)
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-04-13 02:29:57 +00:00
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-04-13 02:29:57 +00:00
|
|
|
"""Set up upon config entry in user interface."""
|
|
|
|
conf = entry.data
|
2019-07-14 07:24:40 +00:00
|
|
|
username = conf[CONF_USERNAME]
|
|
|
|
password = conf[CONF_PASSWORD]
|
|
|
|
|
2021-02-22 09:39:10 +00:00
|
|
|
if CONF_USERCODES not in conf:
|
|
|
|
# should only happen for those who used UI before we added usercodes
|
2021-04-10 05:41:29 +00:00
|
|
|
raise ConfigEntryAuthFailed("No usercodes in TotalConnect configuration")
|
2021-02-22 09:39:10 +00:00
|
|
|
|
|
|
|
temp_codes = conf[CONF_USERCODES]
|
2021-05-14 13:49:18 +00:00
|
|
|
usercodes = {int(code): temp_codes[code] for code in temp_codes}
|
2019-07-14 07:24:40 +00:00
|
|
|
|
2021-11-15 17:32:35 +00:00
|
|
|
try:
|
|
|
|
client = await hass.async_add_executor_job(
|
|
|
|
TotalConnectClient, username, password, usercodes
|
|
|
|
)
|
|
|
|
except AuthenticationError as exception:
|
|
|
|
raise ConfigEntryAuthFailed("TotalConnect authentication failed") from exception
|
2019-07-14 07:24:40 +00:00
|
|
|
|
2021-10-27 17:15:13 +00:00
|
|
|
coordinator = TotalConnectDataUpdateCoordinator(hass, client)
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2019-07-14 07:24:40 +00:00
|
|
|
|
2021-10-27 17:15:13 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = coordinator
|
2021-04-27 20:19:57 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2019-07-14 07:24:40 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-01-02 06:06:11 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-04-13 02:29:57 +00:00
|
|
|
"""Unload a config entry."""
|
2021-04-27 20:19:57 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2020-04-13 02:29:57 +00:00
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2019-07-14 07:24:40 +00:00
|
|
|
|
2020-04-13 02:29:57 +00:00
|
|
|
return unload_ok
|
2021-10-27 17:15:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TotalConnectDataUpdateCoordinator(DataUpdateCoordinator):
|
|
|
|
"""Class to fetch data from TotalConnect."""
|
|
|
|
|
|
|
|
def __init__(self, hass: HomeAssistant, client):
|
|
|
|
"""Initialize."""
|
|
|
|
self.hass = hass
|
|
|
|
self.client = client
|
|
|
|
super().__init__(
|
|
|
|
hass, logger=_LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL
|
|
|
|
)
|
|
|
|
|
|
|
|
async def _async_update_data(self):
|
|
|
|
"""Update data."""
|
|
|
|
await self.hass.async_add_executor_job(self.sync_update_data)
|
|
|
|
|
|
|
|
def sync_update_data(self):
|
|
|
|
"""Fetch synchronous data from TotalConnect."""
|
|
|
|
try:
|
|
|
|
for location_id in self.client.locations:
|
|
|
|
self.client.locations[location_id].get_panel_meta_data()
|
|
|
|
except AuthenticationError as exception:
|
|
|
|
# should only encounter if password changes during operation
|
|
|
|
raise ConfigEntryAuthFailed(
|
|
|
|
"TotalConnect authentication failed"
|
|
|
|
) from exception
|
|
|
|
except TotalConnectError as exception:
|
|
|
|
raise UpdateFailed(exception) from exception
|
|
|
|
except ValueError as exception:
|
|
|
|
raise UpdateFailed("Unknown state from TotalConnect") from exception
|