2019-09-06 20:09:03 +00:00
|
|
|
"""The ViCare integration."""
|
2021-09-14 20:06:06 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-29 14:19:06 +00:00
|
|
|
from collections.abc import Callable
|
2021-09-14 20:06:06 +00:00
|
|
|
from dataclasses import dataclass
|
2019-11-04 19:12:43 +00:00
|
|
|
import enum
|
2019-09-06 20:09:03 +00:00
|
|
|
import logging
|
2021-09-29 14:19:06 +00:00
|
|
|
from typing import Generic, TypeVar
|
2019-09-06 20:09:03 +00:00
|
|
|
|
|
|
|
from PyViCare.PyViCareDevice import Device
|
2021-03-31 09:29:24 +00:00
|
|
|
from PyViCare.PyViCareFuelCell import FuelCell
|
2019-11-04 19:12:43 +00:00
|
|
|
from PyViCare.PyViCareGazBoiler import GazBoiler
|
|
|
|
from PyViCare.PyViCareHeatPump import HeatPump
|
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
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-03-02 20:43:59 +00:00
|
|
|
PLATFORMS = ["climate", "sensor", "binary_sensor", "water_heater"]
|
2019-09-06 20:09:03 +00:00
|
|
|
|
|
|
|
DOMAIN = "vicare"
|
|
|
|
VICARE_API = "api"
|
|
|
|
VICARE_NAME = "name"
|
2019-11-04 19:12:43 +00:00
|
|
|
VICARE_HEATING_TYPE = "heating_type"
|
2019-09-06 20:09:03 +00:00
|
|
|
|
|
|
|
CONF_CIRCUIT = "circuit"
|
2019-11-04 19:12:43 +00:00
|
|
|
CONF_HEATING_TYPE = "heating_type"
|
|
|
|
DEFAULT_HEATING_TYPE = "generic"
|
|
|
|
|
|
|
|
|
2021-09-14 20:06:06 +00:00
|
|
|
ApiT = TypeVar("ApiT", bound=Device)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass()
|
|
|
|
class ViCareRequiredKeysMixin(Generic[ApiT]):
|
|
|
|
"""Mixin for required keys."""
|
|
|
|
|
|
|
|
value_getter: Callable[[ApiT], bool]
|
|
|
|
|
|
|
|
|
2019-11-04 19:12:43 +00:00
|
|
|
class HeatingType(enum.Enum):
|
|
|
|
"""Possible options for heating type."""
|
|
|
|
|
|
|
|
generic = "generic"
|
|
|
|
gas = "gas"
|
|
|
|
heatpump = "heatpump"
|
2021-03-31 09:29:24 +00:00
|
|
|
fuelcell = "fuelcell"
|
2019-11-04 19:12:43 +00:00
|
|
|
|
2019-09-06 20:09:03 +00:00
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
2021-07-22 19:56:38 +00:00
|
|
|
vol.Required(CONF_CLIENT_ID): cv.string,
|
2020-03-28 11:12:27 +00:00
|
|
|
vol.Optional(CONF_SCAN_INTERVAL, default=60): vol.All(
|
|
|
|
cv.time_period, lambda value: value.total_seconds()
|
|
|
|
),
|
2019-09-06 20:09:03 +00:00
|
|
|
vol.Optional(CONF_CIRCUIT): int,
|
|
|
|
vol.Optional(CONF_NAME, default="ViCare"): cv.string,
|
2019-11-04 19:12:43 +00:00
|
|
|
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
|
|
|
if conf.get(CONF_CIRCUIT) is not None:
|
|
|
|
params["circuit"] = conf[CONF_CIRCUIT]
|
|
|
|
|
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-11-04 19:12:43 +00:00
|
|
|
heating_type = conf[CONF_HEATING_TYPE]
|
|
|
|
|
2019-09-06 20:09:03 +00:00
|
|
|
try:
|
2019-11-04 19:12:43 +00:00
|
|
|
if heating_type == HeatingType.gas:
|
|
|
|
vicare_api = GazBoiler(conf[CONF_USERNAME], conf[CONF_PASSWORD], **params)
|
|
|
|
elif heating_type == HeatingType.heatpump:
|
|
|
|
vicare_api = HeatPump(conf[CONF_USERNAME], conf[CONF_PASSWORD], **params)
|
2021-03-31 09:29:24 +00:00
|
|
|
elif heating_type == HeatingType.fuelcell:
|
|
|
|
vicare_api = FuelCell(conf[CONF_USERNAME], conf[CONF_PASSWORD], **params)
|
2019-11-04 19:12:43 +00:00
|
|
|
else:
|
|
|
|
vicare_api = Device(conf[CONF_USERNAME], conf[CONF_PASSWORD], **params)
|
2019-09-06 20:09:03 +00:00
|
|
|
except AttributeError:
|
|
|
|
_LOGGER.error(
|
2020-07-05 21:04:19 +00:00
|
|
|
"Failed to create PyViCare API client. Please check your credentials"
|
2019-09-06 20:09:03 +00:00
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
hass.data[DOMAIN][VICARE_API] = vicare_api
|
|
|
|
hass.data[DOMAIN][VICARE_NAME] = conf[CONF_NAME]
|
2019-11-04 19:12:43 +00:00
|
|
|
hass.data[DOMAIN][VICARE_HEATING_TYPE] = 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
|