@@ -76,6 +76,7 @@ def __init__(self, mac, address, message_sender):
7676 self ._energy_last_collected_count = 0
7777 self ._energy_ratelimit_collection_timestamp = datetime .utcnow ()
7878 self ._energy_last_rollover_timestamp = datetime .utcnow ()
79+ self ._energy_pulses_midnight_rollover = datetime .utcnow ()
7980 self ._energy_last_local_hour = datetime .now ().hour
8081 self ._energy_last_populated_slot = 0
8182 self ._energy_pulses_current_hour = None
@@ -483,33 +484,49 @@ def _update_energy_today_now(self):
483484 + self ._energy_pulses_current_hour
484485 )
485486
487+ _utc_hour_timestamp = datetime .utcnow ().replace (
488+ minute = 0 , second = 0 , microsecond = 0
489+ )
490+ _local_hour = datetime .now ().hour
491+ _utc_midnight_timestamp = _utc_hour_timestamp - timedelta (hours = _local_hour )
492+ _local_midnight_timestamp = datetime .now ().replace (
493+ hour = 0 , minute = 0 , second = 0 , microsecond = 0
494+ )
495+
486496 if _pulses_today_now is None :
487497 if self ._energy_pulses_today_hourly is None :
488- _utc_hour_timestamp = datetime .utcnow ().replace (
489- minute = 0 , second = 0 , microsecond = 0
490- )
491- _local_hour = datetime .now ().hour
492- _utc_midnight_timestamp = _utc_hour_timestamp - timedelta (hours = _local_hour )
493498 self ._update_energy_today_hourly (
494499 _utc_midnight_timestamp + timedelta (hours = 1 ),
495500 _utc_hour_timestamp ,
496501 )
497502 elif (
498503 self ._energy_pulses_today_now is not None
499504 and self ._energy_pulses_today_now > _pulses_today_now
500- and int (( self ._energy_pulses_today_now - _pulses_today_now ) / self . _energy_pulses_today_now * 100 ) > 1
505+ and self ._energy_pulses_midnight_rollover < _local_midnight_timestamp
501506 ):
502507 _LOGGER .info (
503- "_update_energy_today_now for %s hour rollover started old=%s, new=%s" ,
508+ "_update_energy_today_now for %s midnight rollover started old=%s, new=%s" ,
504509 str (self .mac ),
505510 str (self ._energy_pulses_today_now ),
506511 str (_pulses_today_now ),
507512 )
508- _utc_hour_timestamp = datetime .utcnow ().replace (
509- minute = 0 , second = 0 , microsecond = 0
513+ self ._energy_pulses_today_now = 0
514+ self ._energy_pulses_midnight_rollover = _local_midnight_timestamp
515+ self ._update_energy_today_hourly (
516+ _utc_midnight_timestamp + timedelta (hours = 1 ),
517+ _utc_hour_timestamp ,
518+ )
519+ elif (
520+ self ._energy_pulses_today_now is not None
521+ and self ._energy_pulses_today_now > _pulses_today_now
522+ and int ((self ._energy_pulses_today_now - _pulses_today_now )/ (self ._energy_pulses_today_now + 1 )* 100 ) > 1
523+ ):
524+ _LOGGER .info (
525+ "_update_energy_today_now for %s hour rollover started old=%s, new=%s" ,
526+ str (self .mac ),
527+ str (self ._energy_pulses_today_now ),
528+ str (_pulses_today_now ),
510529 )
511- _local_hour = datetime .now ().hour
512- _utc_midnight_timestamp = _utc_hour_timestamp - timedelta (hours = _local_hour )
513530 self ._update_energy_today_hourly (
514531 _utc_midnight_timestamp + timedelta (hours = 1 ),
515532 _utc_hour_timestamp ,
0 commit comments