2021-02-22 06:12:50 +00:00
|
|
|
"""The kmtronic integration."""
|
|
|
|
import asyncio
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
import async_timeout
|
|
|
|
from pykmtronic.auth import Auth
|
|
|
|
from pykmtronic.hub import KMTronicHubAPI
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-29 22:51:39 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-03-07 13:15:43 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
2021-02-22 06:12:50 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers import aiohttp_client
|
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
|
2021-03-08 21:56:24 +00:00
|
|
|
from .const import DATA_COORDINATOR, DATA_HUB, DOMAIN, MANUFACTURER, UPDATE_LISTENER
|
2021-02-22 06:12:50 +00:00
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
|
|
PLATFORMS = ["switch"]
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
|
|
"""Set up the kmtronic component."""
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Set up kmtronic from a config entry."""
|
|
|
|
session = aiohttp_client.async_get_clientsession(hass)
|
|
|
|
auth = Auth(
|
|
|
|
session,
|
2021-03-07 13:15:43 +00:00
|
|
|
f"http://{entry.data[CONF_HOST]}",
|
2021-02-22 06:12:50 +00:00
|
|
|
entry.data[CONF_USERNAME],
|
|
|
|
entry.data[CONF_PASSWORD],
|
|
|
|
)
|
|
|
|
hub = KMTronicHubAPI(auth)
|
|
|
|
|
|
|
|
async def async_update_data():
|
|
|
|
try:
|
|
|
|
async with async_timeout.timeout(10):
|
|
|
|
await hub.async_update_relays()
|
|
|
|
except aiohttp.client_exceptions.ClientResponseError as err:
|
|
|
|
raise UpdateFailed(f"Wrong credentials: {err}") from err
|
|
|
|
except (
|
|
|
|
asyncio.TimeoutError,
|
|
|
|
aiohttp.client_exceptions.ClientConnectorError,
|
|
|
|
) as err:
|
|
|
|
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
|
|
|
|
|
|
|
coordinator = DataUpdateCoordinator(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
|
|
|
name=f"{MANUFACTURER} {hub.name}",
|
|
|
|
update_method=async_update_data,
|
|
|
|
update_interval=timedelta(seconds=30),
|
|
|
|
)
|
2021-03-29 22:51:39 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2021-02-22 06:12:50 +00:00
|
|
|
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
|
|
DATA_HUB: hub,
|
|
|
|
DATA_COORDINATOR: coordinator,
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:43:59 +00:00
|
|
|
for platform in PLATFORMS:
|
2021-02-22 06:12:50 +00:00
|
|
|
hass.async_create_task(
|
2021-03-02 20:43:59 +00:00
|
|
|
hass.config_entries.async_forward_entry_setup(entry, platform)
|
2021-02-22 06:12:50 +00:00
|
|
|
)
|
|
|
|
|
2021-03-08 21:56:24 +00:00
|
|
|
update_listener = entry.add_update_listener(async_update_options)
|
|
|
|
hass.data[DOMAIN][entry.entry_id][UPDATE_LISTENER] = update_listener
|
|
|
|
|
2021-02-22 06:12:50 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-03-08 21:56:24 +00:00
|
|
|
async def async_update_options(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
|
|
|
"""Update options."""
|
|
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|
|
|
|
|
|
|
|
|
2021-02-22 06:12:50 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
unload_ok = all(
|
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
2021-03-02 20:43:59 +00:00
|
|
|
hass.config_entries.async_forward_entry_unload(entry, platform)
|
|
|
|
for platform in PLATFORMS
|
2021-02-22 06:12:50 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if unload_ok:
|
2021-03-08 21:56:24 +00:00
|
|
|
update_listener = hass.data[DOMAIN][entry.entry_id][UPDATE_LISTENER]
|
|
|
|
update_listener()
|
2021-02-22 06:12:50 +00:00
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|