From 6457df2dfe52fb976903aff0f94c08015218c2ff Mon Sep 17 00:00:00 2001 From: balgerion <133121849+balgerion@users.noreply.github.com> Date: Tue, 29 Apr 2025 16:36:29 +0200 Subject: [PATCH] Update __init__.py --- custom_components/pstryk/__init__.py | 33 +++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/custom_components/pstryk/__init__.py b/custom_components/pstryk/__init__.py index 8360a8f..9e52eb1 100644 --- a/custom_components/pstryk/__init__.py +++ b/custom_components/pstryk/__init__.py @@ -1,3 +1,4 @@ +"""Pstryk Energy integration.""" import logging from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant @@ -13,13 +14,43 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Store API key and forward to sensor platform.""" hass.data[DOMAIN].setdefault(entry.entry_id, {})["api_key"] = entry.data.get("api_key") + + # Register update listener for option changes - only if not already registered + if not entry.update_listeners: + entry.async_on_unload(entry.add_update_listener(async_reload_entry)) + await hass.config_entries.async_forward_entry_setup(entry, "sensor") _LOGGER.debug("Pstryk entry setup: %s", entry.entry_id) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload sensor platform and clear data.""" + # First cancel coordinators' scheduled updates + for price_type in ("buy", "sell"): + key = f"{entry.entry_id}_{price_type}" + coordinator = hass.data[DOMAIN].get(key) + if coordinator: + if hasattr(coordinator, '_unsub_hourly') and coordinator._unsub_hourly: + coordinator._unsub_hourly() + if hasattr(coordinator, '_unsub_midnight') and coordinator._unsub_midnight: + coordinator._unsub_midnight() + + # Then unload the platform unload_ok = await hass.config_entries.async_forward_entry_unload(entry, "sensor") + + # Finally clean up data if unload_ok: - hass.data[DOMAIN].pop(entry.entry_id) + if entry.entry_id in hass.data[DOMAIN]: + hass.data[DOMAIN].pop(entry.entry_id) + + # Clean up any coordinators + for key in list(hass.data[DOMAIN].keys()): + if key.startswith(f"{entry.entry_id}_"): + hass.data[DOMAIN].pop(key, None) + return unload_ok + +async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: + """Reload the config entry when options change.""" + await async_unload_entry(hass, entry) + await async_setup_entry(hass, entry)