Skip to content

Commit

Permalink
handle errors during configurable initialization - electronic tariffs (
Browse files Browse the repository at this point in the history
…openWB#1632)

* handle errors during configurable initialization - electronic tariffs

* docs

* energycharts
  • Loading branch information
LKuemmel authored Jul 5, 2024
1 parent 79e75d9 commit 67d61f0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion docs/samples/sample_electricity_tariff/tariff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 2 additions & 1 deletion packages/helpermodules/subdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 17 additions & 14 deletions packages/modules/common/configurable_tariff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
3 changes: 1 addition & 2 deletions packages/modules/electricity_tariffs/awattar/tariff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
3 changes: 1 addition & 2 deletions packages/modules/electricity_tariffs/energycharts/tariff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
3 changes: 1 addition & 2 deletions packages/modules/electricity_tariffs/tibber/tariff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions packages/modules/electricity_tariffs/voltego/tariff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 67d61f0

Please sign in to comment.