Add sensor freshness checks

This commit is contained in:
buzera committed 2026-10-26 18:13:00 +02:00
1 parent c434f3995d
commit cea79b6b48
12 files changed
+213 -25

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
"""Timestamp boundaries and UI translation coverage without HA dependencies."""
import importlib.util
import json
import unittest
from datetime import datetime, timedelta, timezone
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1] / 'custom_components' / 'heating_mpc'
spec = importlib.util.spec_from_file_location('quality', ROOT / 'data_quality.py')
quality = importlib.util.module_from_spec(spec)
spec.loader.exec_module(quality)
class FreshnessTests(unittest.TestCase):
def test_age_boundaries(self):
now = datetime(2026, 10, 26, tzinfo=timezone.utc)
for seconds, expected in ((0, 'valid'), (1800, 'valid'), (1801, 'stale'), (-1, 'future_timestamp')):
with self.subTest(seconds=seconds):
result = quality.freshness(now - timedelta(seconds=seconds), now, 1800)
self.assertEqual(result['status'], expected)
self.assertEqual(result['age_seconds'], seconds)
def test_missing_naive_and_malformed_times(self):
now = datetime.now(timezone.utc)
for timestamp in (None, 'bad', datetime.now()):
self.assertEqual(quality.freshness(timestamp, now, 1800)['status'], 'invalid_timestamp')
def test_invalid_limits_rejected(self):
now = datetime.now(timezone.utc)
for limit in (0, -1, float('inf'), float('nan')):
with self.assertRaises(ValueError):
quality.freshness(now, now, limit)
def test_freshness_settings_translated(self):
for name in ('strings.json', 'translations/en.json', 'translations/ro.json'):
data = json.loads((ROOT / name).read_text())
for section, step in (('config', 'user'), ('options', 'init')):
labels = data[section]['step'][step]['data']
for key in ('room_max_age_minutes', 'weather_max_age_minutes'):
self.assertTrue(labels[key])