From 72755ddd24cd59168af79f93c149a1e2180b1c17 Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Mon, 9 Nov 2026 10:57:19 +0100 Subject: [PATCH] =?UTF-8?q?Energy-Charts:=20B=C3=B6rsenpreisobergrenze=20e?= =?UTF-8?q?rg=C3=A4nzt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/helpermodules/update_config.py | 17 +++++++++- packages/helpermodules/update_config_test.py | 32 +++++++++++++++++++ .../flexible_tariffs/energycharts/config.py | 2 ++ .../flexible_tariffs/energycharts/tariff.py | 5 ++- 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/packages/helpermodules/update_config.py b/packages/helpermodules/update_config.py index aa2e69aa40..de5f64bc1a 100644 --- a/packages/helpermodules/update_config.py +++ b/packages/helpermodules/update_config.py @@ -63,7 +63,7 @@ class UpdateConfig: - DATASTORE_VERSION = 155 + DATASTORE_VERSION = 156 FILE_OPERATION_VERSION = 0 @@ -4191,3 +4191,18 @@ def upgrade(topic: str, payload) -> Optional[dict]: self._loop_all_received_topics(upgrade) self._append_datastore_version(155) + + def upgrade_datastore_156(self) -> None: + """Optionale Preisobergrenze für Energy-Charts ergänzen.""" + def upgrade(topic: str, payload) -> Optional[dict]: + if topic == "openWB/optional/ep/flexible_tariff/provider": + provider = decode_payload(payload) + if provider.get("type") == "energycharts": + config = provider["configuration"] + if "price_cap" not in config: + config["price_cap"] = None + return {topic: provider} + return None + + self._loop_all_received_topics(upgrade) + self._append_datastore_version(156) diff --git a/packages/helpermodules/update_config_test.py b/packages/helpermodules/update_config_test.py index 27a214b17a..aee711d241 100644 --- a/packages/helpermodules/update_config_test.py +++ b/packages/helpermodules/update_config_test.py @@ -634,3 +634,35 @@ def test_file_operation_0_start_behavior(file_operation_version, finished, expec with patch.object(update_config, "upgrade_file_operation_0") as upgrade_mock: update_config._UpdateConfig__solve_breaking_changes_filesystem() assert upgrade_mock.call_count == expected_calls + + +@pytest.mark.parametrize("provider_type, configuration, expected_configuration, updated", [ + ("energycharts", {"surcharge": 0.0001}, {"surcharge": 0.0001, "price_cap": None}, True), + ("energycharts", {"price_cap": 0.0002}, {"price_cap": 0.0002}, False), + ("energycharts", {"price_cap": None}, {"price_cap": None}, False), + ("tibber", {}, {}, False), + (None, {}, {}, False), +]) +def test_upgrade_datastore_156_adds_energycharts_price_cap( + mock_pub: Mock, provider_type, configuration, expected_configuration, updated +): + topic = "openWB/optional/ep/flexible_tariff/provider" + uc = UpdateConfig() + uc.all_received_topics = { + topic: json.dumps({"type": provider_type, "configuration": configuration}).encode(), + "openWB/system/datastore_version": [155], + } + + uc.upgrade_datastore_156() + + if updated: + assert uc.all_received_topics[topic] == { + "type": provider_type, "configuration": expected_configuration + } + else: + assert json.loads(uc.all_received_topics[topic]) == { + "type": provider_type, "configuration": expected_configuration + } + updated_topics = [call.args[0] for call in mock_pub.pub.call_args_list] + assert (topic in updated_topics) is updated + assert uc.all_received_topics["openWB/system/datastore_version"] == [155, 156] diff --git a/packages/modules/electricity_pricing/flexible_tariffs/energycharts/config.py b/packages/modules/electricity_pricing/flexible_tariffs/energycharts/config.py index 46b120af96..7a8e4ff78f 100644 --- a/packages/modules/electricity_pricing/flexible_tariffs/energycharts/config.py +++ b/packages/modules/electricity_pricing/flexible_tariffs/energycharts/config.py @@ -1,4 +1,5 @@ from dataclasses import dataclass, field +from typing import Optional @dataclass @@ -7,6 +8,7 @@ class EnergyChartsTariffConfiguration: surcharge: float = 0 net: bool = True tax: float = 19 + price_cap: Optional[float] = None @dataclass diff --git a/packages/modules/electricity_pricing/flexible_tariffs/energycharts/tariff.py b/packages/modules/electricity_pricing/flexible_tariffs/energycharts/tariff.py index 75fa6dbc82..0f40c1e287 100644 --- a/packages/modules/electricity_pricing/flexible_tariffs/energycharts/tariff.py +++ b/packages/modules/electricity_pricing/flexible_tariffs/energycharts/tariff.py @@ -27,7 +27,10 @@ def create_request_url(config: EnergyChartsTariffConfiguration) -> str: def parse_response(config: EnergyChartsTariffConfiguration, raw_prices: Dict[str, float]) -> Dict[str, float]: prices: Dict[str, float] = {} for timestamp, price_per_MWh in zip(raw_prices['unix_seconds'], raw_prices['price']): - formatted_price = float(price_per_MWh)/1000000 + config.surcharge + formatted_price = float(price_per_MWh)/1000000 + if config.price_cap is not None: + formatted_price = min(formatted_price, config.price_cap) + formatted_price += config.surcharge if config.net is False: formatted_price = formatted_price * (1 + config.tax / 100) prices[str(int(timestamp))] = formatted_price