Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/control/chargelog/chargelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,13 @@ def calc_energy_costs(cp, create_log_entry: bool = False):
_add_charged_energy_by_source(cp, charged_energy_by_source)
log.debug(f"charged_energy_by_source {charged_energy_by_source} "
f"total charged_energy_by_source {cp.data.set.log.charged_energy_by_source}")
costs = _calc_costs(charged_energy_by_source, reference_entries[-1]["prices"])
cp.data.set.log.costs += costs
prices = data.data.general_data.data.prices
if (prices.bat == prices.grid == prices.pv and
data.data.optional_data.data.electricity_pricing.configured is False):
cp.data.set.log.costs = cp.data.set.log.imported_since_mode_switch * prices.bat
else:
costs = _calc_costs(charged_energy_by_source, reference_entries[-1]["prices"])
cp.data.set.log.costs += costs
except Exception:
log.exception(f"Fehler beim Berechnen der Ladekosten für Ladepunkt {cp.num}")

Expand Down
17 changes: 17 additions & 0 deletions packages/control/chargelog/chargelog_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from control.chargelog import chargelog
from control.chargelog.chargelog import calc_energy_costs
from control.chargepoint.chargepoint import Chargepoint
from control.general import Prices


@pytest.fixture()
Expand Down Expand Up @@ -97,6 +98,22 @@ def test_calc_charge_cost_reference_end(mock_data, monkeypatch):
assert round(cp.data.set.log.costs, 5) == 0.025


def test_calc_charge_cost_reference_end_unique_price(mock_data, monkeypatch):
cp = Chargepoint(4, None)
cp.data.set.log.imported_since_plugged = cp.data.set.log.imported_since_mode_switch = 3950
cp.data.set.log.timestamp_mode_switch = 1652682600 # 8:30
cp.data.get.imported = 4100
cp.data.set.log.charged_energy_by_source = {'grid': 1243, 'pv': 386, 'bat': 671, 'cp': 0.0}
daily_log = mock_daily_log(monkeypatch)
data.data.general_data.data.prices = Prices(bat=0.0002, cp=0, grid=0.0002, pv=0.0002)

with patch("builtins.open", mock_open(read_data=json.dumps(daily_log))):
calc_energy_costs(cp, True)

assert cp.data.set.log.charged_energy_by_source == {'bat': 699.57, 'cp': 0.0, 'grid': 1300.14, 'pv': 400.29}
assert round(cp.data.set.log.costs, 5) == 0.79


def test_calc_charge_cost_reference_middle_day_change(mock_data, monkeypatch):
cp = Chargepoint(4, None)
cp.data.set.log.imported_since_plugged = cp.data.set.log.imported_since_mode_switch = 3950
Expand Down