Skip to content

Commit 1888f6f

Browse files
committed
Reduce complexity, improve
1 parent dddd7b9 commit 1888f6f

File tree

1 file changed

+60
-65
lines changed

1 file changed

+60
-65
lines changed

plugwise_usb/nodes/circle.py

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ async def _calibration_update_state(
255255
gain_b: float | None,
256256
off_noise: float | None,
257257
off_tot: float | None,
258-
load_from_cache=False,
258+
load_from_cache: bool = False,
259259
) -> bool:
260260
"""Process new energy calibration settings. Returns True if successful."""
261261
if gain_a is None or gain_b is None or off_noise is None or off_tot is None:
@@ -434,85 +434,78 @@ async def energy_update(self) -> EnergyStatistics | None:
434434
)
435435
return None
436436

437-
async def get_missing_energy_logs(self) -> None:
438-
"""Task to retrieve missing energy logs."""
439-
440-
self._energy_counters.update()
437+
async def _get_initial_energy_logs(self) -> None:
438+
"""Collect initial energy logs from the last 10 log addresses."""
441439
if self._current_log_address is None:
442-
return None
443-
444-
if (missing_addresses := self._energy_counters.log_addresses_missing) is None:
445-
_LOGGER.debug(
446-
"Start collecting initial energy logs from the last 10 log addresses for node %s.",
447-
self._mac_in_str,
448-
)
449-
total_addresses = 11
450-
log_address = self._current_log_address
451-
prev_address_timestamp: datetime | None = None
452-
while total_addresses > 0:
453-
if not await self.energy_log_update(log_address):
454-
# Handle case with None-data in all address slots
455-
_LOGGER.debug(
456-
"Energy None-data collected from log address %s, stopping collection",
457-
log_address,
458-
)
459-
break
440+
return
460441

461-
# Check if the most recent timestamp of an earlier address is recent
462-
# (within 2/4 * log_interval plus 5 mins margin)
463-
log_interval = self.energy_consumption_interval
464-
_LOGGER.debug("log_interval: %s", log_interval)
465-
_LOGGER.debug(
466-
"last_collected_energy_timestamp: %s",
467-
self._last_collected_energy_timestamp,
468-
)
442+
_LOGGER.debug(
443+
"Start collecting initial energy logs from the last 10 log addresses for node %s.",
444+
self._mac_in_str,
445+
)
446+
total_addresses = 11
447+
log_address = self._current_log_address
448+
prev_address_timestamp: datetime | None = None
449+
while total_addresses > 0:
450+
if not await self.energy_log_update(log_address):
451+
# Handle case with None-data in all address slots
469452
_LOGGER.debug(
470-
"energy_production_interval: %s",
471-
self.energy_production_interval,
453+
"Energy None-data collected from log address %s, stopping collection",
454+
log_address,
472455
)
473-
factor = 4
474-
if self.energy_production_interval is not None:
475-
factor = 2
456+
break
457+
458+
# Check if the most recent timestamp of an earlier address is recent
459+
# (within 2/4 * log_interval plus 5 mins margin)
460+
log_interval = self.energy_consumption_interval
461+
factor = 2 if self.energy_production_interval is not None else 4
476462

463+
if log_interval is not None:
464+
max_gap_minutes = (factor * log_interval) + 5
477465
if log_address == self._current_log_address:
478466
if (
479467
self._last_collected_energy_timestamp is not None
480-
and log_interval is not None
481468
and (
482469
datetime.now(tz=UTC) - self._last_collected_energy_timestamp
483-
).total_seconds() // 60 > (factor * log_interval) + 5 # minutes
484-
):
485-
_LOGGER.debug(
486-
"Energy data collected from the current log address is outdated, stopping collection"
487-
)
488-
break
489-
else:
490-
_LOGGER.debug("prev_address_timestamp: %s", prev_address_timestamp)
491-
if (
492-
self._last_collected_energy_timestamp is not None
493-
and log_interval is not None
494-
and prev_address_timestamp is not None
495-
and (
496-
prev_address_timestamp
497-
- self._last_collected_energy_timestamp
498470
).total_seconds()
499471
// 60
500-
> (factor * log_interval) + 5 # minutes
472+
> max_gap_minutes
501473
):
502474
_LOGGER.debug(
503-
"Collected energy data is outdated, stopping collection"
475+
"Energy data collected from the current log address is outdated, stopping collection"
504476
)
505477
break
478+
elif (
479+
prev_address_timestamp is not None
480+
and self._last_collected_energy_timestamp is not None
481+
and (
482+
prev_address_timestamp - self._last_collected_energy_timestamp
483+
).total_seconds()
484+
// 60
485+
> max_gap_minutes
486+
):
487+
_LOGGER.debug(
488+
"Collected energy data is outdated, stopping collection"
489+
)
490+
break
506491

507-
if self._last_collected_energy_timestamp is not None:
508-
prev_address_timestamp = self._last_collected_energy_timestamp
492+
if self._last_collected_energy_timestamp is not None:
493+
prev_address_timestamp = self._last_collected_energy_timestamp
509494

510-
log_address, _ = calc_log_address(log_address, 1, -4)
511-
total_addresses -= 1
495+
log_address, _ = calc_log_address(log_address, 1, -4)
496+
total_addresses -= 1
512497

513-
if self._cache_enabled:
514-
await self._energy_log_records_save_to_cache()
498+
if self._cache_enabled:
499+
await self._energy_log_records_save_to_cache()
500+
501+
async def get_missing_energy_logs(self) -> None:
502+
"""Task to retrieve missing energy logs."""
503+
self._energy_counters.update()
504+
if self._current_log_address is None:
505+
return
515506

507+
if (missing_addresses := self._energy_counters.log_addresses_missing) is None:
508+
await self._get_initial_energy_logs()
516509
return
517510

518511
_LOGGER.debug("Task created to get missing logs of %s", self._mac_in_str)
@@ -814,7 +807,9 @@ async def _relay_update_state(
814807
_LOGGER.debug("Saving relay state update to cache for %s", self._mac_in_str)
815808
await self.save_cache()
816809

817-
async def _relay_update_lock(self, state: bool, load_from_cache=False) -> None:
810+
async def _relay_update_lock(
811+
self, state: bool, load_from_cache: bool = False
812+
) -> None:
818813
"""Process relay lock update."""
819814
state_update = False
820815
if state:
@@ -1315,18 +1310,18 @@ async def energy_reset_request(self) -> None:
13151310
_LOGGER.warning("Energy reset for Node %s successful", self._mac_in_str)
13161311

13171312
# Follow up by an energy-intervals (re)set
1318-
request = CircleMeasureIntervalRequest(
1313+
interval_request = CircleMeasureIntervalRequest(
13191314
self._send,
13201315
self._mac_in_bytes,
13211316
DEFAULT_CONS_INTERVAL,
13221317
NO_PRODUCTION_INTERVAL,
13231318
)
1324-
if (response := await request.send()) is None:
1319+
if (interval_response := await interval_request.send()) is None:
13251320
raise NodeError("No response for CircleMeasureIntervalRequest")
13261321

1327-
if response.response_type != NodeResponseType.POWER_LOG_INTERVAL_ACCEPTED:
1322+
if interval_response.response_type != NodeResponseType.POWER_LOG_INTERVAL_ACCEPTED:
13281323
raise MessageError(
1329-
f"Unknown NodeResponseType '{response.response_type.name}' received"
1324+
f"Unknown NodeResponseType '{interval_response.response_type.name}' received"
13301325
)
13311326
_LOGGER.warning("Resetting energy intervals to default (= consumption only)")
13321327

0 commit comments

Comments
 (0)