Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

potential fix for duplicated instances on update #1231

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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