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
|
2019-12-09 12:13:33 +00:00
|
|
|
import voluptuous as vol
|
2019-09-06 20:09:03 +00:00
|
|
|
|
2021-11-22 14:06:42 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2020-03-28 11:12:27 +00:00
|
|
|
from homeassistant.const import (
|
2021-07-22 19:56:38 +00:00
|
|
|
CONF_CLIENT_ID,
|
2020-03-28 11:12:27 +00:00
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2021-11-22 14:06:42 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-12-09 12:13:33 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2020-01-20 16:44:55 +00:00
|
|
|
from homeassistant.helpers.storage import STORAGE_DIR
|
2022-01-02 15:27:12 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2019-09-06 20:09:03 +00:00
|
|
|
|
2021-10-25 11:43:43 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_CIRCUIT,
|
|
|
|
CONF_HEATING_TYPE,
|
|
|
|
DEFAULT_HEATING_TYPE,
|
|
|
|
DOMAIN,
|
|
|
|
HEATING_TYPE_TO_CREATOR_METHOD,
|
|
|
|
PLATFORMS,
|
|
|
|
VICARE_API,
|
|
|
|
VICARE_CIRCUITS,
|
|
|
|
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
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
2021-10-25 11:43:43 +00:00
|
|
|
DOMAIN: vol.All(
|
|
|
|
cv.deprecated(CONF_CIRCUIT),
|
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Required(CONF_CLIENT_ID): cv.string,
|
|
|
|
vol.Optional(CONF_SCAN_INTERVAL, default=60): vol.All(
|
|
|
|
cv.time_period, lambda value: value.total_seconds()
|
|
|
|
),
|
|
|
|
vol.Optional(
|
|
|
|
CONF_CIRCUIT
|
|
|
|
): int, # Ignored: All circuits are now supported. Will be removed when switching to Setup via UI.
|
|
|
|
vol.Optional(CONF_NAME, default="ViCare"): cv.string,
|
|
|
|
vol.Optional(
|
2021-11-22 14:06:42 +00:00
|
|
|
CONF_HEATING_TYPE, default=DEFAULT_HEATING_TYPE.value
|
|
|
|
): vol.In([e.value for e in HeatingType]),
|
2021-10-25 11:43:43 +00:00
|
|
|
}
|
|
|
|
),
|
2019-09-06 20:09:03 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-02 15:27:12 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2021-11-22 14:06:42 +00:00
|
|
|
"""Set up the ViCare component from yaml."""
|
|
|
|
if DOMAIN not in config:
|
|
|
|
# Setup via UI. No need to continue yaml-based setup
|
|
|
|
return True
|
2019-09-06 20:09:03 +00:00
|
|
|
|
2021-11-22 14:06:42 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data=config[DOMAIN],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-11-22 14:06:42 +00:00
|
|
|
hass.config_entries.async_setup_platforms(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()
|
2021-11-22 14:06:42 +00:00
|
|
|
vicare_api.setCacheDuration(entry_data[CONF_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
|
|
|
hass.data[DOMAIN][entry.entry_id][VICARE_CIRCUITS] = hass.data[DOMAIN][
|
|
|
|
entry.entry_id
|
|
|
|
][VICARE_API].circuits
|
|
|
|
|
|
|
|
|
|
|
|
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
|