Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions custom_components/nordpool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
res = await _dry_setup(hass, entry.data)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

entry.add_update_listener(async_reload_entry)
# async_on_unload removes the listener on unload, so reloads don't stack
# up extra update listeners (each one triggers another reload).
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
return res


Expand All @@ -234,5 +236,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Reload config entry."""
await async_unload_entry(hass, entry)
await async_setup_entry(hass, entry)
# Go through HA's own reload so the entry's async_on_unload callbacks
# (the update listener above) actually run before setup adds new ones.
await hass.config_entries.async_reload(entry.entry_id)
22 changes: 18 additions & 4 deletions custom_components/nordpool/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,23 @@ async def async_added_to_hass(self):
await super().async_added_to_hass()
_LOGGER.debug("called async_added_to_hass %s", self.name)

async_dispatcher_connect(self._api._hass, EVENT_NEW_DAY, self.handle_new_day)
async_dispatcher_connect(
self._api._hass, EVENT_NEW_PRICE, self.handle_new_price
# async_on_remove disconnects these when the entity is removed (e.g. on
# a config entry reload). Without it the removed sensor objects stay
# subscribed and keep recalculating every 15 min, piling up on each
# reload until EVENT_NEW_HOUR blocks the event loop for minutes.
self.async_on_remove(
async_dispatcher_connect(
self._api._hass, EVENT_NEW_DAY, self.handle_new_day
)
)
self.async_on_remove(
async_dispatcher_connect(
self._api._hass, EVENT_NEW_PRICE, self.handle_new_price
)
)
self.async_on_remove(
async_dispatcher_connect(
self._api._hass, EVENT_NEW_HOUR, self.handle_new_hr
)
)
async_dispatcher_connect(self._api._hass, EVENT_NEW_HOUR, self.handle_new_hr)
await self.handle_new_hr()