From 4a97a2faf2e0f24c956edd343c73ec51d968945d Mon Sep 17 00:00:00 2001 From: Jean-Marc Collin Date: Wed, 6 Nov 2024 19:05:38 +0000 Subject: [PATCH] - Force writing state when entity is removed - Fix bug with issue #552 on CONF_USE_CENTRAL_BOILER_FEATURE which should be proposed on a central configuration - Improve reload of entity to avoid reloading all VTherm. Only the reconfigured one will be reloaded --- .../versatile_thermostat/__init__.py | 11 +++++++++-- .../versatile_thermostat/base_thermostat.py | 17 +++++++++++++++-- .../versatile_thermostat/config_flow.py | 2 +- .../versatile_thermostat/vtherm_api.py | 6 ++++-- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/custom_components/versatile_thermostat/__init__.py b/custom_components/versatile_thermostat/__init__.py index 5c7c366..c95209d 100644 --- a/custom_components/versatile_thermostat/__init__.py +++ b/custom_components/versatile_thermostat/__init__.py @@ -178,13 +178,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: if hass.state == CoreState.running: await api.reload_central_boiler_entities_list() - await api.init_vtherm_links() + await api.init_vtherm_links(entry.entry_id) return True async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: """Update listener.""" + + _LOGGER.debug( + "Calling update_listener entry: entry_id='%s', value='%s'", + entry.entry_id, + entry.data, + ) + if entry.data.get(CONF_THERMOSTAT_TYPE) == CONF_THERMOSTAT_CENTRAL_CONFIG: await reload_all_vtherm(hass) else: @@ -193,7 +200,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() + await api.init_vtherm_links(entry.entry_id) 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 04527c5..0b8cc9c 100644 --- a/custom_components/versatile_thermostat/base_thermostat.py +++ b/custom_components/versatile_thermostat/base_thermostat.py @@ -19,7 +19,10 @@ from homeassistant.core import ( ) from homeassistant.components.climate import ClimateEntity -from homeassistant.helpers.restore_state import RestoreEntity +from homeassistant.helpers.restore_state import ( + RestoreEntity, + async_get as restore_async_get, +) from homeassistant.helpers.entity import Entity from homeassistant.config_entries import ConfigEntry from homeassistant.helpers.device_registry import DeviceInfo, DeviceEntryType @@ -591,14 +594,24 @@ class BaseThermostat(ClimateEntity, RestoreEntity, Generic[T]): # issue 428. Link to others entities will start at link # await self.async_startup() + async def async_will_remove_from_hass(self): + """Try to force backup of entity""" + _LOGGER_ENERGY.debug( + "%s - force write before remove. Energy is %s", self, self.total_energy + ) + # Force dump in background + await restore_async_get(self.hass).async_dump_states() + def remove_thermostat(self): """Called when the thermostat will be removed""" _LOGGER.info("%s - Removing thermostat", self) + for under in self._underlyings: under.remove_entity() async def async_startup(self, central_configuration): - """Triggered on startup, used to get old state and set internal states accordingly""" + """Triggered on startup, used to get old state and set internal states accordingly. This is triggered by + VTherm API""" _LOGGER.debug("%s - Calling async_startup", self) _LOGGER.debug("%s - Calling async_startup_internal", self) diff --git a/custom_components/versatile_thermostat/config_flow.py b/custom_components/versatile_thermostat/config_flow.py index de67f6b..5c0a82a 100644 --- a/custom_components/versatile_thermostat/config_flow.py +++ b/custom_components/versatile_thermostat/config_flow.py @@ -215,7 +215,7 @@ class VersatileThermostatBaseConfigFlow(FlowHandler): CONF_USE_PRESETS_CENTRAL_CONFIG, CONF_USE_ADVANCED_CENTRAL_CONFIG, CONF_USE_CENTRAL_MODE, - CONF_USE_CENTRAL_BOILER_FEATURE, + # CONF_USE_CENTRAL_BOILER_FEATURE, this is for Central Config CONF_USED_BY_CENTRAL_BOILER, ]: if data.get(conf) is True: diff --git a/custom_components/versatile_thermostat/vtherm_api.py b/custom_components/versatile_thermostat/vtherm_api.py index 52a0609..98cef53 100644 --- a/custom_components/versatile_thermostat/vtherm_api.py +++ b/custom_components/versatile_thermostat/vtherm_api.py @@ -150,10 +150,11 @@ class VersatileThermostatAPI(dict): return entity.state return None - async def init_vtherm_links(self): + async def init_vtherm_links(self, entry_id=None): """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, ...) + If entry_id is set, only the VTherm of this entry will be reloaded """ await self.reload_central_boiler_binary_listener() await self.reload_central_boiler_entities_list() @@ -175,7 +176,8 @@ class VersatileThermostatAPI(dict): entity.device_info and entity.device_info.get("model", None) == DOMAIN ): - await entity.async_startup(self.find_central_configuration()) + if entry_id is None or entry_id == entity.unique_id: + await entity.async_startup(self.find_central_configuration()) async def init_vtherm_preset_with_central(self): """Init all VTherm presets when the VTherm uses central temperature"""