From c060cb71aeb14b685db4af0dfc4b21a743ec1973 Mon Sep 17 00:00:00 2001 From: beardhatcode Date: Fri, 28 Nov 2025 16:23:36 +0100 Subject: [PATCH 1/2] fix: multiply staleness threshold by category-specific factors --- custom_components/marstek_local_api/coordinator.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/custom_components/marstek_local_api/coordinator.py b/custom_components/marstek_local_api/coordinator.py index 67a6ad1..7c056eb 100644 --- a/custom_components/marstek_local_api/coordinator.py +++ b/custom_components/marstek_local_api/coordinator.py @@ -393,8 +393,20 @@ def is_category_fresh(self, category: str) -> bool: last_update = self.category_last_updated[category] elapsed = time.time() - last_update + stale_factor = { + "battery": UPDATE_INTERVAL_FAST, + "es": UPDATE_INTERVAL_FAST, + "em": UPDATE_INTERVAL_MEDIUM, + "pv": UPDATE_INTERVAL_MEDIUM, + "mode": UPDATE_INTERVAL_MEDIUM, + "ble": UPDATE_INTERVAL_SLOW, + "wifi": UPDATE_INTERVAL_SLOW, + } + + category_stale_factor = stale_factor.get(category, 1) + # Calculate max age (update interval * threshold) - max_age = self.update_interval.total_seconds() * self.STALENESS_THRESHOLD + max_age = self.update_interval.total_seconds() * self.STALENESS_THRESHOLD * category_stale_factor return elapsed < max_age From 961f3f3472ca2941af62dce1b8bac36b1036470b Mon Sep 17 00:00:00 2001 From: beardhatcode Date: Fri, 28 Nov 2025 17:00:07 +0100 Subject: [PATCH 2/2] clean: move staleness factor to init --- .../marstek_local_api/coordinator.py | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/custom_components/marstek_local_api/coordinator.py b/custom_components/marstek_local_api/coordinator.py index 7c056eb..c34d81f 100644 --- a/custom_components/marstek_local_api/coordinator.py +++ b/custom_components/marstek_local_api/coordinator.py @@ -277,6 +277,15 @@ def __init__( # Staleness tracking - track last successful update per category self.category_last_updated: dict[str, float] = {} self.STALENESS_THRESHOLD = 3 # missed updates before invalidation + self.STALENESS_FACTOR = { + "battery": UPDATE_INTERVAL_FAST, + "es": UPDATE_INTERVAL_FAST, + "em": UPDATE_INTERVAL_MEDIUM, + "pv": UPDATE_INTERVAL_MEDIUM, + "mode": UPDATE_INTERVAL_MEDIUM, + "ble": UPDATE_INTERVAL_SLOW, + "wifi": UPDATE_INTERVAL_SLOW, + } self.STATIC_CATEGORIES = {"device", "wifi", "ble", "_diagnostic", "aggregates"} # Initialize compatibility matrix for version-specific scaling @@ -393,17 +402,7 @@ def is_category_fresh(self, category: str) -> bool: last_update = self.category_last_updated[category] elapsed = time.time() - last_update - stale_factor = { - "battery": UPDATE_INTERVAL_FAST, - "es": UPDATE_INTERVAL_FAST, - "em": UPDATE_INTERVAL_MEDIUM, - "pv": UPDATE_INTERVAL_MEDIUM, - "mode": UPDATE_INTERVAL_MEDIUM, - "ble": UPDATE_INTERVAL_SLOW, - "wifi": UPDATE_INTERVAL_SLOW, - } - - category_stale_factor = stale_factor.get(category, 1) + category_stale_factor = self.STALENESS_FACTOR.get(category, 1) # Calculate max age (update interval * threshold) max_age = self.update_interval.total_seconds() * self.STALENESS_THRESHOLD * category_stale_factor