From 67d61f0b2a17828e3a3f52e334c7f01cff1e4fe0 Mon Sep 17 00:00:00 2001 From: LKuemmel <76958050+LKuemmel@users.noreply.github.com> Date: Fri, 5 Jul 2024 12:56:49 +0200 Subject: [PATCH] handle errors during configurable initialization - electronic tariffs (#1632) * handle errors during configurable initialization - electronic tariffs * docs * energycharts --- .../sample_electricity_tariff/tariff.py | 2 +- packages/helpermodules/subdata.py | 3 +- .../modules/common/configurable_tariff.py | 31 ++++++++++--------- .../electricity_tariffs/awattar/tariff.py | 3 +- .../energycharts/tariff.py | 3 +- .../electricity_tariffs/tibber/tariff.py | 3 +- .../electricity_tariffs/voltego/tariff.py | 4 +-- 7 files changed, 25 insertions(+), 24 deletions(-) diff --git a/docs/samples/sample_electricity_tariff/tariff.py b/docs/samples/sample_electricity_tariff/tariff.py index bebf1f5778..ea23689b6d 100644 --- a/docs/samples/sample_electricity_tariff/tariff.py +++ b/docs/samples/sample_electricity_tariff/tariff.py @@ -19,7 +19,7 @@ def fetch(config: SampleTariffConfiguration) -> None: def create_electricity_tariff(config: SampleTariff): def updater(): return fetch(config.configuration) - return ConfigurableTariff(config=config, component_updater=updater) + return updater device_descriptor = DeviceDescriptor(configuration_factory=SampleTariff) diff --git a/packages/helpermodules/subdata.py b/packages/helpermodules/subdata.py index 0027b09e84..fa2eefba43 100644 --- a/packages/helpermodules/subdata.py +++ b/packages/helpermodules/subdata.py @@ -33,6 +33,7 @@ from modules.common.abstract_vehicle import CalculatedSocState, GeneralVehicleConfig from modules.common.configurable_backup_cloud import ConfigurableBackupCloud from modules.common.configurable_ripple_control_receiver import ConfigurableRcr +from modules.common.configurable_tariff import ConfigurableElectricityTariff from modules.common.simcount.simcounter_state import SimCounterState from modules.internal_chargepoint_handler.internal_chargepoint_handler_config import ( GlobalHandlerData, InternalChargepoint, RfidData) @@ -653,7 +654,7 @@ def process_optional_topic(self, var: optional.Optional, msg: mqtt.MQTTMessage): mod = importlib.import_module( f".electricity_tariffs.{config_dict['type']}.tariff", "modules") config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config_dict) - var.et_module = mod.create_electricity_tariff(config) + var.et_module = ConfigurableElectricityTariff(config, mod.create_electricity_tariff) var.et_get_prices() else: self.set_json_payload_class(var.data.et, msg) diff --git a/packages/modules/common/configurable_tariff.py b/packages/modules/common/configurable_tariff.py index 65d16dd4d8..6c83747861 100644 --- a/packages/modules/common/configurable_tariff.py +++ b/packages/modules/common/configurable_tariff.py @@ -13,22 +13,25 @@ class ConfigurableElectricityTariff(Generic[T_TARIFF_CONFIG]): def __init__(self, config: T_TARIFF_CONFIG, - component_updater: Callable[[], None]) -> None: - self.__component_updater = component_updater + component_initialiser: Callable[[], float]) -> None: self.config = config self.store = store.get_electricity_tariff_value_store() self.fault_state = FaultState(ComponentInfo(None, self.config.name, ComponentType.ELECTRICITY_TARIFF.value)) + with SingleComponentUpdateContext(self.fault_state): + self._component_updater = component_initialiser(config) def update(self): - with SingleComponentUpdateContext(self.fault_state): - tariff_state = self.__component_updater() - current_hour = create_unix_timestamp_current_full_hour() - 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.') + 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 = create_unix_timestamp_current_full_hour() + 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.') diff --git a/packages/modules/electricity_tariffs/awattar/tariff.py b/packages/modules/electricity_tariffs/awattar/tariff.py index 26e8772126..0568798959 100644 --- a/packages/modules/electricity_tariffs/awattar/tariff.py +++ b/packages/modules/electricity_tariffs/awattar/tariff.py @@ -4,7 +4,6 @@ from modules.common.abstract_device import DeviceDescriptor from modules.common.component_state import TariffState -from modules.common.configurable_tariff import ConfigurableElectricityTariff from modules.common import req from modules.electricity_tariffs.awattar.config import AwattarTariffConfiguration from modules.electricity_tariffs.awattar.config import AwattarTariff @@ -46,7 +45,7 @@ def fetch_prices(config: AwattarTariffConfiguration) -> Dict[int, float]: def create_electricity_tariff(config: AwattarTariff): def updater(): return TariffState(prices=fetch_prices(config.configuration)) - return ConfigurableElectricityTariff(config=config, component_updater=updater) + return updater device_descriptor = DeviceDescriptor(configuration_factory=AwattarTariff) diff --git a/packages/modules/electricity_tariffs/energycharts/tariff.py b/packages/modules/electricity_tariffs/energycharts/tariff.py index 1cd6507e3a..2808705774 100644 --- a/packages/modules/electricity_tariffs/energycharts/tariff.py +++ b/packages/modules/electricity_tariffs/energycharts/tariff.py @@ -5,7 +5,6 @@ from modules.common.abstract_device import DeviceDescriptor from modules.common.component_state import TariffState -from modules.common.configurable_tariff import ConfigurableElectricityTariff from modules.electricity_tariffs.energycharts.config import EnergyChartsTariffConfiguration from modules.electricity_tariffs.energycharts.config import EnergyChartsTariff @@ -35,7 +34,7 @@ def fetch_prices(config: EnergyChartsTariffConfiguration) -> Dict[int, float]: def create_electricity_tariff(config: EnergyChartsTariff): def updater(): return TariffState(prices=fetch_prices(config.configuration)) - return ConfigurableElectricityTariff(config=config, component_updater=updater) + return updater device_descriptor = DeviceDescriptor(configuration_factory=EnergyChartsTariff) diff --git a/packages/modules/electricity_tariffs/tibber/tariff.py b/packages/modules/electricity_tariffs/tibber/tariff.py index ce50fe3cde..c3bac5f61b 100644 --- a/packages/modules/electricity_tariffs/tibber/tariff.py +++ b/packages/modules/electricity_tariffs/tibber/tariff.py @@ -5,7 +5,6 @@ from modules.common.abstract_device import DeviceDescriptor from modules.common.component_state import TariffState -from modules.common.configurable_tariff import ConfigurableElectricityTariff from modules.common import req from modules.electricity_tariffs.tibber.config import TibberTariffConfiguration from modules.electricity_tariffs.tibber.config import TibberTariff @@ -52,7 +51,7 @@ def fetch_prices(config: TibberTariffConfiguration) -> Dict[int, float]: def create_electricity_tariff(config: TibberTariff): def updater(): return TariffState(prices=fetch_prices(config.configuration)) - return ConfigurableElectricityTariff(config=config, component_updater=updater) + return updater device_descriptor = DeviceDescriptor(configuration_factory=TibberTariff) diff --git a/packages/modules/electricity_tariffs/voltego/tariff.py b/packages/modules/electricity_tariffs/voltego/tariff.py index 66eba904ac..fff55c4592 100644 --- a/packages/modules/electricity_tariffs/voltego/tariff.py +++ b/packages/modules/electricity_tariffs/voltego/tariff.py @@ -8,7 +8,7 @@ from dataclass_utils import asdict from helpermodules import timecheck from helpermodules.pub import Pub -from modules.common import configurable_tariff, req +from modules.common import req from modules.common.abstract_device import DeviceDescriptor from modules.common.component_state import TariffState from modules.electricity_tariffs.voltego.config import VoltegoTariff, VoltegoToken @@ -81,7 +81,7 @@ def create_electricity_tariff(config: VoltegoTariff): def updater(): return TariffState(prices=fetch(config)) - return configurable_tariff.ConfigurableElectricityTariff(config=config, component_updater=updater) + return updater device_descriptor = DeviceDescriptor(configuration_factory=VoltegoTariff)