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
16 changes: 11 additions & 5 deletions packages/modules/common/configurable_tariff.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from modules.common import store
from modules.common.component_context import SingleComponentUpdateContext
from modules.common.component_state import TariffState
from modules.common.component_type import ComponentType
from modules.common.fault_state import ComponentInfo, FaultState

Expand All @@ -24,14 +25,19 @@ def update(self):
if hasattr(self, "_component_updater"):
# Wenn beim Initialisieren etwas schief gelaufen ist, ursprüngliche Fehlermeldung beibehalten
with SingleComponentUpdateContext(self.fault_state):
tariff_state = self._component_updater()
current_hour = str(int(create_unix_timestamp_current_full_hour()))
tariff_state = self._remove_outdated_prices(self._component_updater())
self.store.set(tariff_state)
self.store.update()
for timestamp in tariff_state.prices.keys():
if timestamp < current_hour:
self.fault_state.warning('Die Preisliste startet nicht mit der aktuellen Stunde.')
if len(tariff_state.prices) < 24:
self.fault_state.no_error(
f'Die Preisliste hat nicht 24, sondern {len(tariff_state.prices)} Einträge. '
'Die Strompreise werden vom Anbieter erst um 14:00 für den Folgetag aktualisiert.')

def _remove_outdated_prices(self, tariff_state: TariffState) -> TariffState:
current_hour = str(int(create_unix_timestamp_current_full_hour()))
for timestamp in list(tariff_state.prices.keys()):
if timestamp < current_hour:
self.fault_state.warning(
'Die Preisliste startet nicht mit der aktuellen Stunde. Abgelaufene Einträge wurden entfernt.')
tariff_state.prices.pop(timestamp)
return tariff_state
34 changes: 34 additions & 0 deletions packages/modules/common/configurable_tariff_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

from unittest.mock import Mock
import pytest

from modules.common.component_state import TariffState
from modules.common.configurable_tariff import ConfigurableElectricityTariff
from modules.electricity_tariffs.awattar.config import AwattarTariff


@pytest.mark.parametrize(
"tariff_state, expected",
[
pytest.param(TariffState(prices={"1652680800": -5.87e-06,
"1652684400": 5.467e-05,
"1652688000": 10.72e-05}),
TariffState(prices={"1652680800": -5.87e-06,
"1652684400": 5.467e-05,
"1652688000": 10.72e-05}), id="keine veralteten Einträge"),
pytest.param(TariffState(prices={"1652677200": -5.87e-06,
"1652680800": 5.467e-05,
"1652684400": 10.72e-05}),
TariffState(prices={"1652680800": 5.467e-05,
"1652684400": 10.72e-05}), id="Lösche ersten Eintrag"),
],
)
def test_remove_outdated_prices(tariff_state: TariffState, expected: TariffState, monkeypatch):
# setup
tariff = ConfigurableElectricityTariff(AwattarTariff(), Mock())

# test
result = tariff._remove_outdated_prices(tariff_state)

# assert
assert result.prices == expected.prices