2019-09-06 20:09:03 +00:00
|
|
|
"""The ViCare integration."""
|
2021-09-14 20:06:06 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
2019-09-06 20:09:03 +00:00
|
|
|
import logging
|
2021-10-25 11:43:43 +00:00
|
|
|
from typing import Callable
|
2019-09-06 20:09:03 +00:00
|
|
|
|
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
|
|
|
|
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,
|
|
|
|
)
|
2019-09-06 20:09:03 +00:00
|
|
|
from homeassistant.helpers import discovery
|
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
|
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,
|
|
|
|
VICARE_NAME,
|
|
|
|
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(
|
|
|
|
CONF_HEATING_TYPE, default=DEFAULT_HEATING_TYPE
|
|
|
|
): cv.enum(HeatingType),
|
|
|
|
}
|
|
|
|
),
|
2019-09-06 20:09:03 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
"""Create the ViCare component."""
|
|
|
|
conf = config[DOMAIN]
|
2020-01-20 16:44:55 +00:00
|
|
|
params = {"token_file": hass.config.path(STORAGE_DIR, "vicare_token.save")}
|
2019-09-06 20:09:03 +00:00
|
|
|
|
2020-03-28 11:12:27 +00:00
|
|
|
params["cacheDuration"] = conf.get(CONF_SCAN_INTERVAL)
|
2021-07-22 19:56:38 +00:00
|
|
|
params["client_id"] = conf.get(CONF_CLIENT_ID)
|
2019-09-06 20:09:03 +00:00
|
|
|
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
hass.data[DOMAIN][VICARE_NAME] = conf[CONF_NAME]
|
2021-10-25 11:43:43 +00:00
|
|
|
setup_vicare_api(hass, conf, hass.data[DOMAIN])
|
|
|
|
|
|
|
|
hass.data[DOMAIN][CONF_HEATING_TYPE] = conf[CONF_HEATING_TYPE]
|
2019-09-06 20:09:03 +00:00
|
|
|
|
2021-03-02 20:43:59 +00:00
|
|
|
for platform in PLATFORMS:
|
2019-09-06 20:09:03 +00:00
|
|
|
discovery.load_platform(hass, platform, DOMAIN, {}, config)
|
|
|
|
|
|
|
|
return True
|
2021-10-25 11:43:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup_vicare_api(hass, conf, entity_data):
|
|
|
|
"""Set up PyVicare API."""
|
|
|
|
vicare_api = PyViCare()
|
|
|
|
vicare_api.setCacheDuration(conf[CONF_SCAN_INTERVAL])
|
|
|
|
vicare_api.initWithCredentials(
|
|
|
|
conf[CONF_USERNAME],
|
|
|
|
conf[CONF_PASSWORD],
|
|
|
|
conf[CONF_CLIENT_ID],
|
|
|
|
hass.config.path(STORAGE_DIR, "vicare_token.save"),
|
|
|
|
)
|
|
|
|
|
|
|
|
device = vicare_api.devices[0]
|
|
|
|
for device in vicare_api.devices:
|
|
|
|
_LOGGER.info(
|
|
|
|
"Found device: %s (online: %s)", device.getModel(), str(device.isOnline())
|
|
|
|
)
|
|
|
|
entity_data[VICARE_DEVICE_CONFIG] = device
|
|
|
|
entity_data[VICARE_API] = getattr(
|
|
|
|
device, HEATING_TYPE_TO_CREATOR_METHOD[conf[CONF_HEATING_TYPE]]
|
|
|
|
)()
|
|
|
|
entity_data[VICARE_CIRCUITS] = entity_data[VICARE_API].circuits
|