103 lines
3.3 KiB
Python
103 lines
3.3 KiB
Python
""" The API of Versatile Thermostat"""
|
|
import logging
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from .const import (
|
|
DOMAIN,
|
|
CONF_AUTO_REGULATION_EXPERT,
|
|
CONF_SHORT_EMA_PARAMS,
|
|
CONF_THERMOSTAT_TYPE,
|
|
CONF_THERMOSTAT_CENTRAL_CONFIG,
|
|
)
|
|
|
|
VTHERM_API_NAME = "vtherm_api"
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
class VersatileThermostatAPI(dict):
|
|
"""The VersatileThermostatAPI"""
|
|
|
|
_hass: HomeAssistant
|
|
# _entries: Dict(str, ConfigEntry)
|
|
|
|
@classmethod
|
|
def get_vtherm_api(cls, hass=None):
|
|
"""Get the eventual VTherm API class instance"""
|
|
if hass is not None:
|
|
VersatileThermostatAPI._hass = hass
|
|
else:
|
|
if VersatileThermostatAPI._hass is None:
|
|
return None
|
|
|
|
ret = VersatileThermostatAPI._hass.data.get(DOMAIN).get(VTHERM_API_NAME)
|
|
if ret is None:
|
|
ret = VersatileThermostatAPI()
|
|
hass.data[DOMAIN][VTHERM_API_NAME] = ret
|
|
return ret
|
|
|
|
def __init__(self) -> None:
|
|
_LOGGER.debug("building a VersatileThermostatAPI")
|
|
super().__init__()
|
|
self._expert_params = None
|
|
self._short_ema_params = None
|
|
self._central_config = None
|
|
|
|
def find_central_configuration(self):
|
|
"""Search for a central configuration"""
|
|
for config_entry in VersatileThermostatAPI._hass.config_entries.async_entries(
|
|
DOMAIN
|
|
):
|
|
if (
|
|
config_entry.data.get(CONF_THERMOSTAT_TYPE)
|
|
== CONF_THERMOSTAT_CENTRAL_CONFIG
|
|
):
|
|
self._central_config = config_entry
|
|
return self._central_config
|
|
return None
|
|
|
|
def add_entry(self, entry: ConfigEntry):
|
|
"""Add a new entry"""
|
|
_LOGGER.debug("Add the entry %s", entry.entry_id)
|
|
# self._entries[entry.entry_id] = entry
|
|
# Add the entry in hass.data
|
|
VersatileThermostatAPI._hass.data[DOMAIN][entry.entry_id] = entry
|
|
|
|
def remove_entry(self, entry: ConfigEntry):
|
|
"""Remove an entry"""
|
|
_LOGGER.debug("Remove the entry %s", entry.entry_id)
|
|
# self._entries.pop(entry.entry_id)
|
|
VersatileThermostatAPI._hass.data[DOMAIN].pop(entry.entry_id)
|
|
# If not more entries are preset, remove the API
|
|
if len(self) == 0:
|
|
_LOGGER.debug("No more entries-> Remove the API from DOMAIN")
|
|
VersatileThermostatAPI._hass.data.pop(DOMAIN)
|
|
|
|
def set_global_config(self, config):
|
|
"""Read the global configuration from configuration.yaml file"""
|
|
_LOGGER.info("Read global config from configuration.yaml")
|
|
|
|
self._expert_params = config.get(CONF_AUTO_REGULATION_EXPERT)
|
|
if self._expert_params:
|
|
_LOGGER.debug("We have found expert params %s", self._expert_params)
|
|
|
|
self._short_ema_params = config.get(CONF_SHORT_EMA_PARAMS)
|
|
if self._short_ema_params:
|
|
_LOGGER.debug("We have found short ema params %s", self._short_ema_params)
|
|
|
|
@property
|
|
def self_regulation_expert(self):
|
|
"""Get the self regulation params"""
|
|
return self._expert_params
|
|
|
|
@property
|
|
def short_ema_params(self):
|
|
"""Get the self regulation params"""
|
|
return self._short_ema_params
|
|
|
|
@property
|
|
def hass(self):
|
|
"""Get the HomeAssistant object"""
|
|
return VersatileThermostatAPI._hass
|