From f3c16fd10b8bb7a8eca2564f24632f9192854373 Mon Sep 17 00:00:00 2001 From: Marco Schumacher Date: Fri, 5 Jan 2024 22:15:59 +0100 Subject: [PATCH] potential fix for duplicated instances on update --- custom_components/better_thermostat/__init__.py | 8 +++++--- custom_components/better_thermostat/climate.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/custom_components/better_thermostat/__init__.py b/custom_components/better_thermostat/__init__.py index cdfabb99..ac723537 100644 --- a/custom_components/better_thermostat/__init__.py +++ b/custom_components/better_thermostat/__init__.py @@ -1,5 +1,6 @@ """The better_thermostat component.""" import logging +from asyncio import Lock from homeassistant.const import Platform from homeassistant.core import HomeAssistant, Config from homeassistant.config_entries import ConfigEntry @@ -19,6 +20,8 @@ PLATFORMS = [Platform.CLIMATE] CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA) +config_entry_update_listener_lock = Lock() + async def async_setup(hass: HomeAssistant, config: Config): """Set up this integration using YAML is not supported.""" @@ -35,9 +38,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: """Handle options update.""" - await hass.config_entries.async_reload(entry.entry_id) - await async_unload_entry(hass, entry) - await async_setup_entry(hass, entry) + async with config_entry_update_listener_lock: + await hass.config_entries.async_reload(entry.entry_id) async def async_unload_entry(hass, entry): diff --git a/custom_components/better_thermostat/climate.py b/custom_components/better_thermostat/climate.py index d4fd69ae..07a0527e 100644 --- a/custom_components/better_thermostat/climate.py +++ b/custom_components/better_thermostat/climate.py @@ -298,6 +298,7 @@ def __init__( asyncio.create_task(window_queue(self)) self.heating_power = 0.01 self.last_heating_power_stats = [] + self.is_removed = False async def async_added_to_hass(self): """Run when entity about to be added. @@ -357,6 +358,10 @@ async def async_added_to_hass(self): "last_calibration": None, } + def on_remove(): + self.is_removed = True + self.async_on_remove(on_remove) + await super().async_added_to_hass() _LOGGER.info( @@ -859,6 +864,9 @@ async def startup(self): "battery": None, } + if self.is_removed: + return + # update_hvac_action(self) # Add listener if self.outdoor_sensor is not None: @@ -869,6 +877,9 @@ async def startup(self): await check_all_entities(self) + if self.is_removed: + return + self.async_on_remove( async_track_time_interval( self.hass, self._trigger_check_weather, timedelta(hours=1)