|
24 | 24 | ) |
25 | 25 | from ..connection import StickController |
26 | 26 | from ..constants import ( |
| 27 | + DAY_IN_HOURS, |
27 | 28 | DEFAULT_CONS_INTERVAL, |
28 | 29 | MAX_TIME_DRIFT, |
29 | 30 | MINIMAL_POWER_UPDATE, |
@@ -582,7 +583,14 @@ async def energy_log_update(self, address: int | None) -> tuple[bool, bool]: |
582 | 583 | if log_timestamp is None or log_pulses is None: |
583 | 584 | self._energy_counters.add_empty_log(response.log_address, _slot) |
584 | 585 | empty_log = True |
585 | | - elif await self._energy_log_record_update_state( |
| 586 | + continue |
| 587 | + elif not self._check_timestamp_is_recent(address, _slot, log_timestamp): |
| 588 | + # Don't store an old log-record, store am empty record instead |
| 589 | + self._energy_counters.add_empty_log(response.log_address, _slot) |
| 590 | + empty_log = True |
| 591 | + continue |
| 592 | + |
| 593 | + if await self._energy_log_record_update_state( |
586 | 594 | response.log_address, |
587 | 595 | _slot, |
588 | 596 | log_timestamp.replace(tzinfo=UTC), |
@@ -611,6 +619,25 @@ async def energy_log_update(self, address: int | None) -> tuple[bool, bool]: |
611 | 619 |
|
612 | 620 | return result, empty_log |
613 | 621 |
|
| 622 | + def _check_timestamp_is_recent(self, address, slot, timestamp) -> bool: |
| 623 | + """Check if the timestamp of the received log-record is recent. |
| 624 | +
|
| 625 | + A timestamp from within the last 24 hours is considered recent. |
| 626 | + """ |
| 627 | + if ( |
| 628 | + (datetime.now(tz=UTC) - timestamp.replace(tzinfo=UTC)).total_seconds() |
| 629 | + // 3600 |
| 630 | + ) > DAY_IN_HOURS: |
| 631 | + _LOGGER.warning( |
| 632 | + "EnergyLog from Node %s | address %s | slot %s | timestamp %s is outdated, ignoring...", |
| 633 | + self._mac_in_str, |
| 634 | + address, |
| 635 | + slot, |
| 636 | + timestamp, |
| 637 | + ) |
| 638 | + return False |
| 639 | + return True |
| 640 | + |
614 | 641 | async def _energy_log_records_load_from_cache(self) -> bool: |
615 | 642 | """Load energy_log_record from cache.""" |
616 | 643 | if (cache_data := self._get_cache(CACHE_ENERGY_COLLECTION)) is None: |
|
0 commit comments