From 7f16a37123307328f536780ec40c4e066b7bb15c Mon Sep 17 00:00:00 2001 From: Lutz Bender Date: Sun, 20 Apr 2025 23:58:41 +0200 Subject: [PATCH] switch to sim counter in JSON api v2 --- .../modules/devices/sonnen/sonnenbatterie/api.py | 12 +++++++++--- .../sonnen/sonnenbatterie/counter_consumption.py | 6 ++++-- .../modules/devices/sonnen/sonnenbatterie/device.py | 1 + 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/modules/devices/sonnen/sonnenbatterie/api.py b/packages/modules/devices/sonnen/sonnenbatterie/api.py index e965bd0f30..1db08cc1bd 100644 --- a/packages/modules/devices/sonnen/sonnenbatterie/api.py +++ b/packages/modules/devices/sonnen/sonnenbatterie/api.py @@ -333,17 +333,23 @@ def update_inverter(self, sim_counter: SimCounter) -> InverterState: return InverterState(exported=exported, power=pv_power) else: - return self.__state_from_channel( + inverter_state = self.__state_from_channel( self.__read_power_meter(direction=self.PowerMeterDirection.PRODUCTION)[0]) + # meter value is updated way too slow, so we use a sim counter to get the exported energy + _, inverter_state.exported = sim_counter.sim_count(inverter_state.power) + return inverter_state - def update_consumption_counter(self) -> CounterState: + def update_consumption_counter(self, sim_counter: SimCounter) -> CounterState: """ Updates the consumption counter state by reading data from the JSON API. Returns: CounterState: The updated consumption counter state. """ - return self.__state_from_channel( + counter_state = self.__state_from_channel( self.__read_power_meter(direction=self.PowerMeterDirection.CONSUMPTION)[0]) + # meter value is updated way too slow, so we use a sim counter to get the im-/exported energy + counter_state.imported, counter_state.exported = sim_counter.sim_count(counter_state.power) + return counter_state def set_power_limit(self, power_limit: Optional[int]) -> None: if self.power_limit_controllable() is False: diff --git a/packages/modules/devices/sonnen/sonnenbatterie/counter_consumption.py b/packages/modules/devices/sonnen/sonnenbatterie/counter_consumption.py index 36f0de4bcf..a7e46f7163 100644 --- a/packages/modules/devices/sonnen/sonnenbatterie/counter_consumption.py +++ b/packages/modules/devices/sonnen/sonnenbatterie/counter_consumption.py @@ -5,6 +5,7 @@ from modules.common.abstract_device import AbstractCounter from modules.common.component_type import ComponentDescriptor from modules.common.fault_state import ComponentInfo, FaultState +from modules.common.simcount import SimCounter from modules.common.store import get_counter_value_store from modules.devices.sonnen.sonnenbatterie.api import JsonApi, JsonApiVersion @@ -25,6 +26,7 @@ def __init__(self, component_config: SonnenbatterieConsumptionCounterSetup, **kw self.kwargs: KwargsDict = kwargs def initialize(self) -> None: + self.__device_id: int = self.kwargs['device_id'] self.__device_address: str = self.kwargs['device_address'] self.__device_variant: int = self.kwargs['device_variant'] self.__api_v2_token: Optional[str] = self.kwargs['device_api_v2_token'] @@ -32,7 +34,7 @@ def initialize(self) -> None: raise ValueError("Die ausgewählte API bietet keine Verbrauchsdaten!") if self.__device_variant != 3: raise ValueError("Unbekannte API: " + str(self.__device_variant)) - + self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="consumption") self.store = get_counter_value_store(self.component_config.id) self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config)) self.api = JsonApi(host=self.__device_address, @@ -40,7 +42,7 @@ def initialize(self) -> None: auth_token=self.__api_v2_token) def update(self) -> None: - self.store.set(self.api.update_consumption_counter()) + self.store.set(self.api.update_consumption_counter(sim_counter=self.sim_counter)) component_descriptor = ComponentDescriptor(configuration_factory=SonnenbatterieConsumptionCounterSetup) diff --git a/packages/modules/devices/sonnen/sonnenbatterie/device.py b/packages/modules/devices/sonnen/sonnenbatterie/device.py index 5ec5f3cc1f..e70a6e6b83 100644 --- a/packages/modules/devices/sonnen/sonnenbatterie/device.py +++ b/packages/modules/devices/sonnen/sonnenbatterie/device.py @@ -35,6 +35,7 @@ def create_evu_counter_component(component_config: SonnenbatterieCounterSetup): def create_consumption_counter_component(component_config: SonnenbatterieConsumptionCounterSetup): return SonnenbatterieConsumptionCounter(component_config, + device_id=device_config.id, device_address=device_config.configuration.ip_address, device_variant=device_config.configuration.variant, device_api_v2_token=device_config.configuration.api_v2_token)