Skip to content
Merged
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
17 changes: 8 additions & 9 deletions custom_components/suez_enhanced/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .year_merge import merge_year_readings
from .tariffs import TariffSchedule
from .daily_cache import DailyTelemetryCache
from .recent_reading import confirmed_yesterday, without_provisional_yesterday
from .backfill import async_forward_backfill
from .older_history import async_discover_older_history
from .historical_archive import HistoricalArchive
Expand Down Expand Up @@ -75,10 +76,7 @@ async def _async_update_data(self):
if (
self._recent_cache_day == today
and self._recent_cache is not None
and any(
m.date == target_day and m.volume is not None
for m in self._recent_cache
)
and confirmed_yesterday(self._recent_cache, target_day) is not None
):
recent = self._recent_cache
else:
Expand All @@ -98,9 +96,10 @@ async def _async_update_data(self):
await self._persistent_cache.async_save(today, recent)
except Exception as exc:
_LOGGER.warning("SUEZ daily cache save failed: %s: %s", type(exc).__name__, exc)
yesterday = next(
(m for m in recent if m.date == today - timedelta(days=1)), None
)
yesterday = confirmed_yesterday(recent, target_day)
# An unindexed zero for yesterday is provisional, not 0 L usage.
# Do not pass it to costs or consumption summaries either.
confirmed_recent = without_provisional_yesterday(recent, target_day)
diagnostics["last_reading_day"] = latest.date.isoformat()
diagnostics["last_sync"] = dt_util.utcnow().isoformat()

Expand Down Expand Up @@ -146,7 +145,7 @@ async def _async_update_data(self):
)
try:
summary = consumption_summary(
recent, today, tariff
confirmed_recent, today, tariff
)
except (ValueError, TypeError) as exc:
_LOGGER.warning("SUEZ consumption summary unavailable: %s", exc)
Expand All @@ -171,7 +170,7 @@ async def _async_update_data(self):
# Recorder is canonical for days already imported.
# Recent API telemetry only fills missing Recorder dates;
# never silently replace historical daily values.
year_measures = merge_year_readings(recorded, daily_readings(recent))
year_measures = merge_year_readings(recorded, daily_readings(confirmed_recent))
year_summary = consumption_summary(
year_measures, today,
tariff,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/suez_enhanced/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"requirements": [
"pysuezV2>=2.0.7"
],
"version": "0.8.1"
"version": "0.8.2"
}
33 changes: 33 additions & 0 deletions custom_components/suez_enhanced/recent_reading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Identify a provisional zero in the most recent SUEZ daily telemetry."""
from datetime import date, datetime
from math import isfinite


def confirmed_yesterday(measures, target_day: date):
"""Return yesterday's reading only if it is not an unindexed zero.

SUEZ sometimes sends volume=0, index=None before publishing the actual
daily usage. A true zero with a valid meter index remains legitimate.
"""
for measure in measures:
day = measure.date
if isinstance(day, datetime):
day = day.date()
if day != target_day:
continue
volume = measure.volume
if not isinstance(volume, (int, float)) or not isfinite(volume) or volume < 0:
return None
if volume == 0 and measure.index is None:
return None
return measure
return None


def without_provisional_yesterday(measures, target_day: date):
"""Exclude only the unconfirmed current-yesterday zero from summaries."""
if confirmed_yesterday(measures, target_day) is not None:
return measures
return [m for m in measures if (
(m.date.date() if isinstance(m.date, datetime) else m.date) != target_day
)]
50 changes: 50 additions & 0 deletions tests/test_recent_reading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Regression tests for provisional SUEZ zero values."""
from datetime import date, datetime
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path
from types import SimpleNamespace
import unittest

SOURCE = Path(__file__).resolve().parents[1] / "custom_components/suez_enhanced/recent_reading.py"
spec = spec_from_file_location("recent_reading_test", SOURCE)
module = module_from_spec(spec)
spec.loader.exec_module(module)


def measure(day, volume, index):
return SimpleNamespace(date=date.fromisoformat(day), volume=volume, index=index)


class RecentReadingTests(unittest.TestCase):
def setUp(self):
self.day = date(2026, 11, 11)

def test_provisional_zero_is_not_confirmed(self):
rows = [measure("2026-11-10", 150, 482905),
measure("2026-11-11", 0, None)]
self.assertIsNone(module.confirmed_yesterday(rows, self.day))
self.assertEqual(len(module.without_provisional_yesterday(rows, self.day)), 1)

def test_real_zero_with_index_is_confirmed(self):
rows = [measure("2026-11-11", 0, 482905)]
self.assertIs(module.confirmed_yesterday(rows, self.day), rows[0])
self.assertEqual(module.without_provisional_yesterday(rows, self.day), rows)

def test_published_positive_volume_is_confirmed_without_index(self):
rows = [measure("2026-11-11", 146, None)]
self.assertIs(module.confirmed_yesterday(rows, self.day), rows[0])

def test_missing_yesterday_does_not_change_other_days(self):
rows = [measure("2026-11-10", 120, None)]
self.assertIsNone(module.confirmed_yesterday(rows, self.day))
self.assertEqual(module.without_provisional_yesterday(rows, self.day), rows)

def test_provisional_zero_not_reused_when_api_updates(self):
cached = [measure("2026-11-11", 0, None)]
refreshed = [measure("2026-11-11", 146, None)]
self.assertIsNone(module.confirmed_yesterday(cached, self.day))
self.assertEqual(module.confirmed_yesterday(refreshed, self.day).volume, 146)


if __name__ == "__main__":
unittest.main()
Loading