From 6fa616775e26c9db359514270a8a289a5c8ce14c Mon Sep 17 00:00:00 2001 From: Jean-Marc Collin Date: Tue, 5 Mar 2024 18:19:52 +0000 Subject: [PATCH] Update central config Number temp entity --- .devcontainer/devcontainer.json | 2 +- .../versatile_thermostat/__init__.py | 3 ++ .../versatile_thermostat/base_thermostat.py | 4 ++ .../versatile_thermostat/number.py | 42 +++++++------------ .../versatile_thermostat/vtherm_api.py | 4 +- 5 files changed, 25 insertions(+), 30 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 3d45d97..34d85bb 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -22,7 +22,7 @@ "extensions": [ "ms-python.python", "ms-python.pylint", - // already included into ms-python.python + // Doesn't work (crash). Default in python is to use Jedi see Settings / Python / Default Language // "ms-python.vscode-pylance", "ms-python.isort", "ms-python.black-formatter", diff --git a/custom_components/versatile_thermostat/__init__.py b/custom_components/versatile_thermostat/__init__.py index 6d1de7e..ff07576 100644 --- a/custom_components/versatile_thermostat/__init__.py +++ b/custom_components/versatile_thermostat/__init__.py @@ -126,6 +126,7 @@ async def reload_all_vtherm(hass): api: VersatileThermostatAPI = VersatileThermostatAPI.get_vtherm_api(hass) if api: await api.reload_central_boiler_entities_list() + await api.init_vtherm_links() async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: @@ -146,6 +147,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await api.reload_central_boiler_entities_list() + await api.init_vtherm_links() return True @@ -160,6 +162,7 @@ async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: api: VersatileThermostatAPI = VersatileThermostatAPI.get_vtherm_api(hass) if api is not None: await api.reload_central_boiler_entities_list() + await api.init_vtherm_links() async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: diff --git a/custom_components/versatile_thermostat/base_thermostat.py b/custom_components/versatile_thermostat/base_thermostat.py index 225d6e2..9c1d62b 100644 --- a/custom_components/versatile_thermostat/base_thermostat.py +++ b/custom_components/versatile_thermostat/base_thermostat.py @@ -2722,3 +2722,7 @@ class BaseThermostat(ClimateEntity, RestoreEntity): if self._motion_on: self._attr_preset_modes.append(PRESET_ACTIVITY) + + # Re-applicate the last preset if any to take change into account + if self._attr_preset_mode: + await self._async_set_preset_mode_internal(self._attr_preset_mode, True) diff --git a/custom_components/versatile_thermostat/number.py b/custom_components/versatile_thermostat/number.py index e413522..fa30003 100644 --- a/custom_components/versatile_thermostat/number.py +++ b/custom_components/versatile_thermostat/number.py @@ -227,7 +227,9 @@ class ActivateBoilerThresholdNumber( return f"VersatileThermostat-{self.name}" -class CentralConfigTemperatureNumber(NumberEntity, RestoreEntity): +class CentralConfigTemperatureNumber( + NumberEntity, RestoreEntity +): # pylint: disable=abstract-method """Representation of one temperature number""" _attr_has_entity_name = True @@ -318,35 +320,19 @@ class CentralConfigTemperatureNumber(NumberEntity, RestoreEntity): pass @overrides - async def async_set_native_value(self, value: float) -> None: - """Change the value""" + def set_native_value(self, value: float) -> None: + """The value have change from the Number Entity in UI""" + float_value = float(value) + old_value = float(self._attr_native_value) + if float_value == old_value: + return - # TODO implements the native value change -> reload values for all central config - # based VTherm - # if self.my_climate is None: - # _LOGGER.warning( - # "%s - cannot change temperature because VTherm is not initialized", self - # ) - # return + self._attr_value = self._attr_native_value = float_value - # - # float_value = float(value) - # old_value = float(self._attr_native_value) - # - # if float_value == old_value: - # return - # - # self._attr_value = self._attr_native_value = float_value - # - # self.async_write_ha_state() - # - # # Update the VTherm - # self.hass.create_task( - # self.my_climate.service_set_preset_temperature( - # self._preset_name.replace("_temp", ""), self._attr_native_value, None - # ) - - # ) + # We have to reload all VTherm for which uses the central configuration + api: VersatileThermostatAPI = VersatileThermostatAPI.get_vtherm_api(self.hass) + # Update the VTherms + self.hass.create_task(api.init_vtherm_links()) def __str__(self): return f"VersatileThermostat-{self.name}" diff --git a/custom_components/versatile_thermostat/vtherm_api.py b/custom_components/versatile_thermostat/vtherm_api.py index 19ff86c..1401484 100644 --- a/custom_components/versatile_thermostat/vtherm_api.py +++ b/custom_components/versatile_thermostat/vtherm_api.py @@ -152,6 +152,7 @@ class VersatileThermostatAPI(dict): async def init_vtherm_links(self): """INitialize all VTherms entities links This method is called when HA is fully started (and all entities should be initialized) + Or when we need to reload all VTherm links (with Number temp entities, central boiler, ...) """ await self.reload_central_boiler_binary_listener() await self.reload_central_boiler_entities_list() @@ -161,7 +162,8 @@ class VersatileThermostatAPI(dict): ) if component: for entity in component.entities: - await entity.init_presets(self.find_central_configuration()) + if hasattr(entity, "init_presets"): + await entity.init_presets(self.find_central_configuration()) async def reload_central_boiler_binary_listener(self): """Reloads the BinarySensor entity which listen to the number of