Skip to content

Commit 0e2252a

Browse files
committed
Correct total_addresses calculation
1 parent b10698c commit 0e2252a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

plugwise_usb/nodes/circle.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from datetime import UTC, datetime
99
from functools import wraps
1010
import logging
11+
from match import ceil
1112
from typing import Any, Final, TypeVar, cast
1213

1314
from ..api import (
@@ -454,15 +455,23 @@ async def energy_update(self) -> EnergyStatistics | None: # noqa: PLR0911 PLR09
454455
return None
455456

456457
async def _get_initial_energy_logs(self) -> None:
457-
"""Collect initial energy logs for up to 10 last log addresses or from the hours elapsed today."""
458+
"""Collect initial energy logs for the hours elapsed today up to MAX_LOG_HOURS ."""
458459
if self._current_log_address is None:
459460
return
460461

462+
if self.energy_consumption_interval is None:
463+
return
464+
461465
_LOGGER.debug(
462466
"Start collecting today's energy logs for node %s.",
463467
self._mac_in_str,
464468
)
465-
total_addresses = min(MAX_ADDRESSES_COLLECTED, datetime.now(tz=UTC).hour + 1)
469+
470+
# When only consumption is measured, 1 address contains data from 4 hours
471+
# When both consumption and production are measured, 1 address contains data from 2 hours
472+
factor = 4 if self.energy_production_interval is not None else 2
473+
max_addresses_to_collect = int(MAX_LOG_HOURS / factor)
474+
total_addresses = min(max_addresses_to_collect, ceil(datetime.now(tz=UTC).hour / factor) + 1)
466475
log_address = self._current_log_address
467476
while total_addresses > 0:
468477
result = await self.energy_log_update(log_address)

0 commit comments

Comments
 (0)