add cleanup

This commit is contained in:
Julian Pawlowski 2025-04-18 21:37:48 +00:00
parent f092ad2839
commit f06b9ff0cf
2 changed files with 19 additions and 8 deletions

View file

@ -12,11 +12,12 @@ from typing import TYPE_CHECKING
from homeassistant.const import CONF_ACCESS_TOKEN, Platform from homeassistant.const import CONF_ACCESS_TOKEN, Platform
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.storage import Store
from homeassistant.loader import async_get_loaded_integration from homeassistant.loader import async_get_loaded_integration
from .api import TibberPricesApiClient from .api import TibberPricesApiClient
from .const import DOMAIN, LOGGER from .const import DOMAIN, LOGGER
from .coordinator import TibberPricesDataUpdateCoordinator from .coordinator import STORAGE_VERSION, TibberPricesDataUpdateCoordinator
from .data import TibberPricesData from .data import TibberPricesData
if TYPE_CHECKING: if TYPE_CHECKING:
@ -39,6 +40,7 @@ async def async_setup_entry(
"""Set up this integration using UI.""" """Set up this integration using UI."""
coordinator = TibberPricesDataUpdateCoordinator( coordinator = TibberPricesDataUpdateCoordinator(
hass=hass, hass=hass,
entry=entry,
logger=LOGGER, logger=LOGGER,
name=DOMAIN, name=DOMAIN,
update_interval=timedelta(hours=1), update_interval=timedelta(hours=1),
@ -65,10 +67,19 @@ async def async_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: TibberPricesConfigEntry, entry: TibberPricesConfigEntry,
) -> bool: ) -> bool:
"""Handle removal of an entry.""" """Handle unload of an entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def async_remove_entry(
hass: HomeAssistant,
entry: TibberPricesConfigEntry,
) -> None:
"""Handle removal of an entry."""
if storage := Store(hass, STORAGE_VERSION, f"{DOMAIN}.{entry.entry_id}"):
await storage.async_remove()
async def async_reload_entry( async def async_reload_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: TibberPricesConfigEntry, entry: TibberPricesConfigEntry,

View file

@ -33,7 +33,6 @@ PRICE_UPDATE_INTERVAL = timedelta(days=1)
RATING_UPDATE_INTERVAL = timedelta(hours=1) RATING_UPDATE_INTERVAL = timedelta(hours=1)
NO_DATA_ERROR_MSG = "No data available" NO_DATA_ERROR_MSG = "No data available"
STORAGE_VERSION = 1 STORAGE_VERSION = 1
STORAGE_KEY = f"{DOMAIN}.coordinator"
def _raise_no_data() -> None: def _raise_no_data() -> None:
@ -45,17 +44,18 @@ def _raise_no_data() -> None:
class TibberPricesDataUpdateCoordinator(DataUpdateCoordinator): class TibberPricesDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to manage fetching data from the API.""" """Class to manage fetching data from the API."""
config_entry: TibberPricesConfigEntry
def __init__( def __init__(
self, self,
hass: HomeAssistant, hass: HomeAssistant,
entry: TibberPricesConfigEntry,
*args: Any, *args: Any,
**kwargs: Any, **kwargs: Any,
) -> None: ) -> None:
"""Initialize coordinator with cache.""" """Initialize coordinator with cache."""
super().__init__(hass, *args, **kwargs) super().__init__(hass, *args, **kwargs)
self._store = Store(hass, STORAGE_VERSION, STORAGE_KEY) self._entry = entry
storage_key = f"{DOMAIN}.{entry.entry_id}"
self._store = Store(hass, STORAGE_VERSION, storage_key)
self._cached_price_data: dict | None = None self._cached_price_data: dict | None = None
self._cached_rating_data: dict | None = None self._cached_rating_data: dict | None = None
self._last_price_update: datetime | None = None self._last_price_update: datetime | None = None
@ -189,7 +189,7 @@ class TibberPricesDataUpdateCoordinator(DataUpdateCoordinator):
async def _fetch_price_data(self) -> dict: async def _fetch_price_data(self) -> dict:
"""Fetch fresh price data from API.""" """Fetch fresh price data from API."""
client = self.config_entry.runtime_data.client client = self._entry.runtime_data.client
return await client.async_get_price_info() return await client.async_get_price_info()
def _extract_price_data(self, data: dict) -> dict: def _extract_price_data(self, data: dict) -> dict:
@ -239,7 +239,7 @@ class TibberPricesDataUpdateCoordinator(DataUpdateCoordinator):
async def _get_rating_data(self) -> dict: async def _get_rating_data(self) -> dict:
"""Get fresh rating data from API.""" """Get fresh rating data from API."""
client = self.config_entry.runtime_data.client client = self._entry.runtime_data.client
daily = await client.async_get_daily_price_rating() daily = await client.async_get_daily_price_rating()
hourly = await client.async_get_hourly_price_rating() hourly = await client.async_get_hourly_price_rating()
monthly = await client.async_get_monthly_price_rating() monthly = await client.async_get_monthly_price_rating()