2019-09-06 20:09:03 +00:00
|
|
|
"""The ViCare integration."""
|
2021-09-14 20:06:06 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-11-25 21:23:48 +00:00
|
|
|
from collections.abc import Callable
|
2021-09-14 20:06:06 +00:00
|
|
|
from dataclasses import dataclass
|
2019-09-06 20:09:03 +00:00
|
|
|
import logging
|
|
|
|
|
2021-10-25 11:43:43 +00:00
|
|
|
from PyViCare.PyViCare import PyViCare
|
2019-09-06 20:09:03 +00:00
|
|
|
from PyViCare.PyViCareDevice import Device
|
|
|
|
|
2022-05-24 18:28:09 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-01-05 21:49:42 +00:00
|
|
|
from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME
|
2021-11-22 14:06:42 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-01-20 16:44:55 +00:00
|
|
|
from homeassistant.helpers.storage import STORAGE_DIR
|
2019-09-06 20:09:03 +00:00
|
|
|
|
2021-10-25 11:43:43 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_HEATING_TYPE,
|
2022-01-05 21:49:42 +00:00
|
|
|
DEFAULT_SCAN_INTERVAL,
|
2021-10-25 11:43:43 +00:00
|
|
|
DOMAIN,
|
|
|
|
HEATING_TYPE_TO_CREATOR_METHOD,
|
|
|
|
PLATFORMS,
|
|
|
|
VICARE_API,
|
|
|
|
VICARE_DEVICE_CONFIG,
|
|
|
|
HeatingType,
|
|
|
|
)
|
2019-11-04 19:12:43 +00:00
|
|
|
|
2021-10-25 11:43:43 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2021-09-14 20:06:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass()
|
2021-10-25 11:43:43 +00:00
|
|
|
class ViCareRequiredKeysMixin:
|
2021-09-14 20:06:06 +00:00
|
|
|
"""Mixin for required keys."""
|
|
|
|
|
2021-10-25 11:43:43 +00:00
|
|
|
value_getter: Callable[[Device], bool]
|
2019-11-04 19:12:43 +00:00
|
|
|
|
2019-09-06 20:09:03 +00:00
|
|
|
|
2021-11-22 14:06:42 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up from config entry."""
|
|
|
|
_LOGGER.debug("Setting up ViCare component")
|
2019-09-06 20:09:03 +00:00
|
|
|
|
|
|
|
hass.data[DOMAIN] = {}
|
2021-11-22 14:06:42 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = {}
|
2021-10-25 11:43:43 +00:00
|
|
|
|
2021-11-22 14:06:42 +00:00
|
|
|
await hass.async_add_executor_job(setup_vicare_api, hass, entry)
|
2019-09-06 20:09:03 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2019-09-06 20:09:03 +00:00
|
|
|
|
|
|
|
return True
|
2021-10-25 11:43:43 +00:00
|
|
|
|
|
|
|
|
2021-11-22 14:06:42 +00:00
|
|
|
def vicare_login(hass, entry_data):
|
|
|
|
"""Login via PyVicare API."""
|
2021-10-25 11:43:43 +00:00
|
|
|
vicare_api = PyViCare()
|
2022-01-05 21:49:42 +00:00
|
|
|
vicare_api.setCacheDuration(DEFAULT_SCAN_INTERVAL)
|
2021-10-25 11:43:43 +00:00
|
|
|
vicare_api.initWithCredentials(
|
2021-11-22 14:06:42 +00:00
|
|
|
entry_data[CONF_USERNAME],
|
|
|
|
entry_data[CONF_PASSWORD],
|
|
|
|
entry_data[CONF_CLIENT_ID],
|
2021-10-25 11:43:43 +00:00
|
|
|
hass.config.path(STORAGE_DIR, "vicare_token.save"),
|
|
|
|
)
|
2021-11-22 14:06:42 +00:00
|
|
|
return vicare_api
|
|
|
|
|
|
|
|
|
|
|
|
def setup_vicare_api(hass, entry):
|
|
|
|
"""Set up PyVicare API."""
|
|
|
|
vicare_api = vicare_login(hass, entry.data)
|
2021-10-25 11:43:43 +00:00
|
|
|
|
|
|
|
for device in vicare_api.devices:
|
|
|
|
_LOGGER.info(
|
|
|
|
"Found device: %s (online: %s)", device.getModel(), str(device.isOnline())
|
|
|
|
)
|
2021-11-22 14:06:42 +00:00
|
|
|
|
|
|
|
# Currently we only support a single device
|
|
|
|
device = vicare_api.devices[0]
|
|
|
|
hass.data[DOMAIN][entry.entry_id][VICARE_DEVICE_CONFIG] = device
|
|
|
|
hass.data[DOMAIN][entry.entry_id][VICARE_API] = getattr(
|
|
|
|
device,
|
|
|
|
HEATING_TYPE_TO_CREATOR_METHOD[HeatingType(entry.data[CONF_HEATING_TYPE])],
|
2021-10-25 11:43:43 +00:00
|
|
|
)()
|
2021-11-22 14:06:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload ViCare config entry."""
|
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|