@@ -189,6 +189,7 @@ def __init__(
189189 ) -> None :
190190 """Initialize energy counter based on energy id."""
191191 self ._mac = mac
192+ self ._midnight_reset_passed = False
192193 if energy_id not in ENERGY_COUNTERS :
193194 raise EnergyError (f"Invalid energy id '{ energy_id } ' for Energy counter" )
194195 self ._calibration : EnergyCalibration | None = None
@@ -279,15 +280,22 @@ def update(
279280 ) -> tuple [float | None , datetime | None ]:
280281 """Get pulse update."""
281282 last_reset = datetime .now (tz = LOCAL_TIMEZONE )
283+ if self ._midnight_reset_passed and last_reset .hour == 1 :
284+ self ._midnight_reset_passed = False
285+
282286 if self ._energy_id in ENERGY_HOUR_COUNTERS :
283287 last_reset = last_reset .replace (minute = 0 , second = 0 , microsecond = 0 )
284288 if self ._energy_id in ENERGY_DAY_COUNTERS :
285289 last_reset = last_reset .replace (hour = 0 , minute = 0 , second = 0 , microsecond = 0 )
286290 # Postpone the last_reset time-changes at day-end until a device pulsecounter resets
287- if last_reset .hour == 0 and not pulse_collection .pulse_counter_reset :
291+ if last_reset .hour == 0 and (
292+ not pulse_collection .pulse_counter_reset
293+ and not self ._midnight_reset_passed
294+ ):
288295 last_reset = (last_reset - timedelta (days = 1 )).replace (
289296 hour = 0 , minute = 0 , second = 0 , microsecond = 0
290297 )
298+ self ._midnight_reset_passed = True
291299
292300 pulses , last_update = pulse_collection .collected_pulses (
293301 last_reset , self ._is_consumption
@@ -307,3 +315,4 @@ def update(
307315 energy = self .energy
308316 _LOGGER .debug ("energy=%s on last_update=%s" , energy , last_update )
309317 return (energy , last_reset )
318+
0 commit comments