diff --git a/plugwise_usb/nodes/helpers/pulses.py b/plugwise_usb/nodes/helpers/pulses.py index 81de2ca1a..0c0d4d94d 100644 --- a/plugwise_usb/nodes/helpers/pulses.py +++ b/plugwise_usb/nodes/helpers/pulses.py @@ -468,7 +468,7 @@ def _add_log_record( return False # Drop useless log records when we have at least 4 logs - if self.collected_logs > 4 and log_record.timestamp < ( + if self.collected_logs > 4 and log_record.timestamp <= ( datetime.now(tz=UTC) - timedelta(hours=MAX_LOG_HOURS) ): return False diff --git a/tests/test_usb.py b/tests/test_usb.py index d10ac122f..1a2b99bc3 100644 --- a/tests/test_usb.py +++ b/tests/test_usb.py @@ -939,15 +939,13 @@ async def fake_get_missing_energy_logs(address: int) -> None: ) await stick.disconnect() - @freeze_time(dt.now()) + @freeze_time("2025-04-03 22:00:00") def test_pulse_collection_consumption( self, monkeypatch: pytest.MonkeyPatch ) -> None: """Testing pulse collection class.""" monkeypatch.setattr(pw_energy_pulses, "MAX_LOG_HOURS", 24) - - fixed_timestamp_utc = dt.now(UTC) - fixed_this_hour = fixed_timestamp_utc.replace(minute=0, second=0, microsecond=0) + fixed_this_hour = dt.now(UTC) # Test consumption logs tst_consumption = pw_energy_pulses.PulseCollection(mac="0098765432101234") @@ -1085,7 +1083,6 @@ def test_pulse_collection_consumption( assert not tst_consumption.log_rollover # add missing logs - test_timestamp = fixed_this_hour - td(hours=3) tst_consumption.add_log(99, 2, (fixed_this_hour - td(hours=3)), 1000) tst_consumption.add_log(99, 1, (fixed_this_hour - td(hours=4)), 1000) tst_consumption.add_log(98, 4, (fixed_this_hour - td(hours=5)), 1000) @@ -1112,31 +1109,42 @@ def test_pulse_collection_consumption( # Test rollover by updating pulses before log record - pulse_update_3 = fixed_this_hour + td(hours=0, minutes=0, seconds=30) + pulse_update_3 = fixed_this_hour + td(hours=1, minutes=0, seconds=30) + test_timestamp = fixed_this_hour + td(hours=1) tst_consumption.update_pulse_counter(2500, 0, pulse_update_3) - assert not tst_consumption.log_rollover + assert tst_consumption.log_rollover + # Collected pulses last hour: assert tst_consumption.collected_pulses( test_timestamp, is_consumption=True - ) == (2500 + 1111 + 1000 + 750, pulse_update_3) - pulse_update_4 = fixed_this_hour + td(hours=1, minutes=1, seconds=3) + ) == (2500, pulse_update_3) + # Collected pulses last day: + assert tst_consumption.collected_pulses( + test_timestamp - td(hours=24), is_consumption=True + ) == (2500 + 22861, pulse_update_3) + pulse_update_4 = fixed_this_hour + td(hours=1, minutes=1) tst_consumption.update_pulse_counter(45, 0, pulse_update_4) assert tst_consumption.log_rollover - test_timestamp = fixed_this_hour + td(hours=1, minutes=1, seconds=4) + # Collected pulses last hour: assert tst_consumption.collected_pulses( test_timestamp, is_consumption=True ) == (45, pulse_update_4) - tst_consumption.add_log(100, 2, (fixed_this_hour + td(hours=1, minutes=1, seconds=5)), 2222) - assert tst_consumption.log_rollover + # Collected pulses last day: + assert tst_consumption.collected_pulses( + test_timestamp - td(hours=24), is_consumption=True + ) == (45 + 22861, pulse_update_4) + # pulse-count of 2500 is ignored, the code does not export this incorrect value + + tst_consumption.add_log(100, 2, (fixed_this_hour + td(hours=1)), 2222) + assert not tst_consumption.log_rollover # Test collection of the last full hour assert tst_consumption.collected_pulses( fixed_this_hour, is_consumption=True ) == (45 + 2222, pulse_update_4) pulse_update_5 = fixed_this_hour + td(hours=1, minutes=1, seconds=18) - test_timestamp_2 = fixed_this_hour + td(hours=1, minutes=1, seconds=20) tst_consumption.update_pulse_counter(145, 0, pulse_update_5) # Test collection of the last new hour assert tst_consumption.collected_pulses( - test_timestamp_2, is_consumption=True + test_timestamp, is_consumption=True ) == (145, pulse_update_5) # Test log rollover by updating log first before updating pulses