From 30c3418f1b6b5ed4876f4588dd2308e5ea761d88 Mon Sep 17 00:00:00 2001 From: Jean-Marc Collin Date: Mon, 18 Dec 2023 20:54:39 +0000 Subject: [PATCH] Issue #281 - cannot use central config at first integration installation --- .../versatile_thermostat/config_flow.py | 5 ++++- .../versatile_thermostat/vtherm_api.py | 20 +++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/custom_components/versatile_thermostat/config_flow.py b/custom_components/versatile_thermostat/config_flow.py index 8b94796..f05cd32 100644 --- a/custom_components/versatile_thermostat/config_flow.py +++ b/custom_components/versatile_thermostat/config_flow.py @@ -86,7 +86,10 @@ class VersatileThermostatBaseConfigFlow(FlowHandler): # VTherm API should have been initialized before arriving here vtherm_api = VersatileThermostatAPI.get_vtherm_api() - self._central_config = vtherm_api.find_central_configuration() + if vtherm_api is not None: + self._central_config = vtherm_api.find_central_configuration() + else: + self._central_config = None self._init_feature_flags(infos) self._init_central_config_flags(infos) diff --git a/custom_components/versatile_thermostat/vtherm_api.py b/custom_components/versatile_thermostat/vtherm_api.py index 89e2572..9d93935 100644 --- a/custom_components/versatile_thermostat/vtherm_api.py +++ b/custom_components/versatile_thermostat/vtherm_api.py @@ -19,26 +19,27 @@ _LOGGER = logging.getLogger(__name__) class VersatileThermostatAPI(dict): """The VersatileThermostatAPI""" - _hass: HomeAssistant + _hass: HomeAssistant = None # _entries: Dict(str, ConfigEntry) @classmethod def get_vtherm_api(cls, hass=None): - """Get the eventual VTherm API class instance""" + """Get the eventual VTherm API class instance or + instantiate it if it doesn't exists""" if hass is not None: VersatileThermostatAPI._hass = hass - else: - if VersatileThermostatAPI._hass is None: - return None + + if VersatileThermostatAPI._hass is None: + return None domain = VersatileThermostatAPI._hass.data.get(DOMAIN) if not domain: - hass.data.setdefault(DOMAIN, {}) + VersatileThermostatAPI._hass.data.setdefault(DOMAIN, {}) ret = VersatileThermostatAPI._hass.data.get(DOMAIN).get(VTHERM_API_NAME) if ret is None: ret = VersatileThermostatAPI() - hass.data[DOMAIN][VTHERM_API_NAME] = ret + VersatileThermostatAPI._hass.data[DOMAIN][VTHERM_API_NAME] = ret return ret def __init__(self) -> None: @@ -46,7 +47,6 @@ class VersatileThermostatAPI(dict): 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""" @@ -57,8 +57,8 @@ class VersatileThermostatAPI(dict): config_entry.data.get(CONF_THERMOSTAT_TYPE) == CONF_THERMOSTAT_CENTRAL_CONFIG ): - self._central_config = config_entry - return self._central_config + central_config = config_entry + return central_config return None def add_entry(self, entry: ConfigEntry):