From 412cecc126b305a4a4386bc9cdf166e066619310 Mon Sep 17 00:00:00 2001 From: Julian Pawlowski <75446+jpawlowski@users.noreply.github.com> Date: Sun, 30 Nov 2025 16:42:41 +0000 Subject: [PATCH] refactor(cache): enhance cache validation to support new structure and invalidate old format --- .../tibber_prices/coordinator/cache.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/custom_components/tibber_prices/coordinator/cache.py b/custom_components/tibber_prices/coordinator/cache.py index e1efcf2..7615bbb 100644 --- a/custom_components/tibber_prices/coordinator/cache.py +++ b/custom_components/tibber_prices/coordinator/cache.py @@ -106,11 +106,30 @@ def is_cache_valid( - No cached data exists - Cached data is from a different calendar day (in local timezone) - Midnight turnover has occurred since cache was saved + - Cache structure is outdated (pre-v0.15.0 multi-home format) """ if cache_data.price_data is None or cache_data.last_price_update is None: return False + # Check for old cache structure (multi-home format from v0.14.0) + # Old format: {"homes": {home_id: {...}}} + # New format: {"home_id": str, "price_info": [...]} + if "homes" in cache_data.price_data: + _LOGGER.info( + "%s Cache has old multi-home structure (v0.14.0), invalidating to fetch fresh data", + log_prefix, + ) + return False + + # Check for missing required keys in new structure + if "price_info" not in cache_data.price_data: + _LOGGER.info( + "%s Cache missing 'price_info' key, invalidating to fetch fresh data", + log_prefix, + ) + return False + current_local_date = time.as_local(time.now()).date() last_update_local_date = time.as_local(cache_data.last_price_update).date()