Skip to content

Commit b68c766

Browse files
authored
daily log: fix percentage calculation for not available counters (#2482)
* draft * improve
1 parent 33c634a commit b68c766

3 files changed

Lines changed: 453 additions & 0 deletions

File tree

packages/helpermodules/measurement_logging/process_log.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,19 @@ def calc_energy_imported_by_source(energy_imported, energy_source):
408408
pv = entry["pv"]["all"]["energy_exported"] if "all" in entry["pv"].keys() else 0
409409
grid_imported, grid_exported = get_grid_from(entry)
410410
consumption = grid_imported - grid_exported + pv + bat_exported - bat_imported + cp_exported
411+
for type in ("bat", "cp"):
412+
if entry[type]["all"]["energy_imported"] > consumption:
413+
consumption += entry[type]["all"]["energy_imported"] - consumption
414+
grid_imported += entry[type]["all"]["energy_imported"] - grid_imported
415+
log.debug(f"Angepasste Verbrauchswerte für {type} um "
416+
f"{entry[type]['all']['energy_imported'] - consumption} kWh")
417+
for counter in entry["counter"].values():
418+
if counter["grid"] is False:
419+
if counter["energy_imported"] > consumption:
420+
consumption += counter["energy_imported"] - consumption
421+
grid_imported += counter["energy_imported"] - grid_imported
422+
log.debug(f"Angepasste Verbrauchswerte für {type} um "
423+
f"{entry[type]['all']['energy_imported'] - consumption} kWh")
411424
try:
412425
if grid_exported > pv:
413426
# Ins Netz eingespeiste Leistung kam nicht von der PV-Anlage sondern aus dem Speicher

packages/helpermodules/measurement_logging/process_log_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
from copy import deepcopy
2+
from unittest.mock import Mock
3+
import pytest
4+
5+
from helpermodules.measurement_logging import process_log
26
from helpermodules.measurement_logging.process_log import (
37
analyse_percentage,
48
_calculate_average_power,
59
process_entry,
610
get_totals,
711
CalculationType)
812

13+
from helpermodules.measurement_logging.process_log_testdata import (counter_jumps_forward,
14+
counter_jumps_forward_processed,
15+
regular_daily_log_entry,
16+
regular_daily_log_entry_processed)
17+
918

1019
def test_get_totals(daily_log_sample, daily_log_totals):
1120
# setup and execution
@@ -52,3 +61,19 @@ def test_convert(daily_log_entry_kw, daily_log_sample):
5261

5362
# evaluation
5463
assert entry == daily_log_entry_kw
64+
65+
66+
@pytest.mark.parametrize("data, expected", [
67+
pytest.param(counter_jumps_forward, counter_jumps_forward_processed, id="counter jumps forward"),
68+
pytest.param(regular_daily_log_entry, regular_daily_log_entry_processed, id="regular daily log entry")
69+
])
70+
def test_get_daily_log(data, expected, monkeypatch):
71+
# setup
72+
collect_daily_log_data_mock = Mock(return_value=data)
73+
monkeypatch.setattr(process_log, "_collect_daily_log_data", collect_daily_log_data_mock)
74+
75+
# execution
76+
daily_log_processed = process_log.get_daily_log("20250616")
77+
78+
# evaluation
79+
assert daily_log_processed == expected

0 commit comments

Comments
 (0)