Skip to content

Commit

Permalink
Merge pull request #1231 from schummar/fix/duplicateInstanceOnUpdate
Browse files Browse the repository at this point in the history
potential fix for duplicated instances on update
  • Loading branch information
KartoffelToby authored Jan 7, 2024
2 parents 8333bdb + fd3f300 commit c1ec7f8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 5 additions & 3 deletions custom_components/better_thermostat/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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."""
Expand All @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions custom_components/better_thermostat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit c1ec7f8

Please sign in to comment.