feat(measurement): treat long *_emr_keys meter gaps as unknown

Additional fixes:
Add timezone to loadakkudoktoradapted tests datetime generation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: Claude Code (claude-opus-5-5)
This commit is contained in:
Bobby Noelte
2026-11-15 18:30:00 +01:00
co-authored by Claude Opus 5.5
parent f441759dfa
commit cc875f798c
13 changed files with 179 additions and 25 deletions
+13 -10
View File
@@ -298,17 +298,18 @@ async def _adjustment_window(adjusted, measurement):
async def test_adjustment_buckets_stay_on_full_hours(loadakkudoktoradjusted, empty_measurement):
"""A newer off-hour record (e.g. a device SoC) must not shift the hourly buckets (#1447)."""
loadakkudoktoradjusted.config.measurement.load_emr_keys = ["load0_mr"]
dt = to_datetime("2024-01-01T00:00:00")
start_dt = to_datetime("2024-01-01T00:00:00", in_timezone=loadakkudoktoradjusted.config.general.timezone)
end_dt = to_datetime("2024-01-02T00:00:00", in_timezone=loadakkudoktoradjusted.config.general.timezone)
for hour in range(25):
await empty_measurement.update_value(dt.add(hours=hour), "load0_mr", 500.0 + hour)
await empty_measurement.update_value(start_dt.add(hours=hour), "load0_mr", 500.0 + hour)
await empty_measurement.update_value(
to_datetime("2024-01-02T00:11:00"), "storage-soc-factor", 0.42
to_datetime("2024-01-02T00:11:00", in_timezone=loadakkudoktoradjusted.config.general.timezone),
"storage-soc-factor", 0.42
)
_, calls = await _adjustment_window(loadakkudoktoradjusted, empty_measurement)
assert calls
assert calls[0]["start_datetime"] == to_datetime("2024-01-01T00:00:00")
assert calls[0]["end_datetime"] == to_datetime("2024-01-02T00:00:00")
assert calls[0]["start_datetime"] == start_dt
assert calls[0]["end_datetime"] == end_dt
@pytest.mark.asyncio
@pytest.mark.parametrize(
@@ -322,8 +323,10 @@ async def test_adjustment_within_first_hour_returns_zero(
loadakkudoktoradjusted, empty_measurement, records
):
"""Less than one full hour of data yields no adjustment instead of an error."""
timezone = loadakkudoktoradjusted.config.general.timezone
for time, key, value in records:
await empty_measurement.update_value(to_datetime(f"2026-08-01T{time}:00"), key, value)
time_dt = to_datetime(f"2026-08-01T{time}:00", in_timezone=timezone)
await empty_measurement.update_value(time_dt, key, value)
(weekday, weekend), calls = await _adjustment_window(loadakkudoktoradjusted, empty_measurement)
assert calls == []
assert not weekday.any() and not weekend.any()
@@ -360,7 +363,7 @@ async def test_adjustment_falls_back_when_the_meter_conversion_fails(
The failure is an error once per streak, not on every prediction run.
"""
loadakkudoktoradjusted.config.measurement.load_emr_keys = ["load0_mr"]
dt = to_datetime("2024-01-01T00:00:00")
dt = to_datetime("2024-01-01T00:00:00", in_timezone=loadakkudoktoradjusted.config.general.timezone)
for hour in range(25):
await empty_measurement.update_value(dt.add(hours=hour), "load0_mr", 500.0 + hour)
@@ -415,7 +418,7 @@ async def test_adjustment_window_follows_the_load_meters(
):
"""A load meter lagging behind other channels still gives its last 7 days."""
loadakkudoktoradjusted.config.measurement.load_emr_keys = ["load0_mr"]
dt = to_datetime("2024-01-01T00:00:00")
dt = to_datetime("2024-01-01T00:00:00", in_timezone=loadakkudoktoradjusted.config.general.timezone)
for hour in range(10 * 24 + 1):
await empty_measurement.update_value(dt.add(hours=hour), "load0_mr", 500.0 + hour)
# SoC keeps reporting three days after the load meter stopped.
@@ -434,7 +437,7 @@ async def test_adjustment_ignores_a_stale_load_meter_period(
):
"""A load meter that stopped long ago must not adjust today's profile with old weeks."""
loadakkudoktoradjusted.config.measurement.load_emr_keys = ["load0_mr"]
dt = to_datetime("2024-01-01T00:00:00")
dt = to_datetime("2024-01-01T00:00:00", in_timezone=loadakkudoktoradjusted.config.general.timezone)
for hour in range(3 * 24 + 1):
await empty_measurement.update_value(dt.add(hours=hour), "load0_mr", 500.0 + hour)
get_ems().set_start_datetime(dt.add(days=30))
+44
View File
@@ -356,6 +356,43 @@ class TestMeasurement:
timezone = measurement_eos.config.general.timezone
assert all(stamp.timezone_name == timezone for stamp in [*firsts.values(), *lasts.values()])
@pytest.mark.parametrize(
("emr_max_gap_seconds", "channel_max_gap_seconds", "expected_kwh"),
[
# Default limit (3 h): a 6 h gap has no energy value.
("default", None, [np.nan] * 6),
# No limit: the gap's energy is spread evenly, as before the limit.
(None, None, [1.0] * 6),
# An own channels entry overrides the limit for its meter.
("default", 86400, [1.0] * 6),
],
)
async def test_emr_meter_gap_limit(
self, measurement_eos, emr_max_gap_seconds, channel_max_gap_seconds, expected_kwh
):
"""A *_emr_keys meter gap longer than emr_max_gap_seconds is unknown energy."""
settings = measurement_eos.config.measurement
settings.load_emr_keys = ["load_gap_mr"]
if emr_max_gap_seconds != "default":
settings.emr_max_gap_seconds = emr_max_gap_seconds
if channel_max_gap_seconds is not None:
settings.channels = {
"load_gap_mr": MeasurementChannelSettings(
quantity="cumulative_energy",
unit="kWh",
max_gap_seconds=channel_max_gap_seconds,
)
}
start = to_datetime("2023-01-02T00:00:00")
await measurement_eos.update_value(start, "load_gap_mr", 10.0)
await measurement_eos.update_value(start.add(hours=6), "load_gap_mr", 16.0)
energy = await measurement_eos.meter_energy_kwh(
["load_gap_mr"], start, start.add(hours=6), to_duration("1 hour")
)
np.testing.assert_allclose(energy["load_gap_mr"], expected_kwh)
async def test_conversion_failure_is_logged_once_per_streak(self, measurement_eos, caplog):
"""Error on the first failure, debug while it persists, error again after a success."""
@@ -633,6 +670,13 @@ class TestMeasurementChannels:
**{legacy_field: ["legacy"]}, channels={"legacy": CHANNELS["house_power"]}
))
def test_emr_max_gap_seconds_default(self):
"""Three hours tolerate hourly meters with jitter; None turns the limit off."""
assert MeasurementCommonSettings().emr_max_gap_seconds == 10800
assert MeasurementCommonSettings(emr_max_gap_seconds=None).emr_max_gap_seconds is None
with pytest.raises(ValidationError):
MeasurementCommonSettings(emr_max_gap_seconds=0)
@pytest.mark.parametrize("key", ["", " x", "date_time", "configured_data", "keys", "_private"])
def test_reject_reserved_key(self, key):
with pytest.raises(ValidationError):