diff --git a/docs/samples/sample_modbus/bat.py b/docs/samples/sample_modbus/bat.py index e28d497e55..97303c9940 100644 --- a/docs/samples/sample_modbus/bat.py +++ b/docs/samples/sample_modbus/bat.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +from typing import Optional from dataclass_utils import dataclass_from_dict from modules.common.abstract_device import AbstractBat from modules.common.component_state import BatState @@ -11,15 +12,16 @@ class SampleBat(AbstractBat): - def __init__(self, device_id: int, component_config: SampleBatSetup) -> None: + def __init__(self, device_id: int, component_config: SampleBatSetup, client: ModbusTcpClient_) -> None: self.__device_id = device_id self.component_config = dataclass_from_dict(SampleBatSetup, component_config) self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="speicher") self.store = get_bat_value_store(self.component_config.id) self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config)) + self.client = client - def update(self, client: ModbusTcpClient_) -> None: - power = client.read_holding_registers(reg, ModbusDataType.INT_32, unit=unit) + def update(self) -> None: + power = self.client.read_holding_registers(reg, ModbusDataType.INT_32, unit=unit) imported, exported = self.sim_counter.sim_count(power) bat_state = BatState( @@ -30,5 +32,9 @@ def update(self, client: ModbusTcpClient_) -> None: ) self.store.set(bat_state) + def set_power_limit(self, power_limit: Optional[int]) -> None: + # Methode entfernen, falls die Batterie keine Steuerung der Ladeleistung unterstützt + self.client.write_registers(reg, power_limit) + component_descriptor = ComponentDescriptor(configuration_factory=SampleBatSetup) diff --git a/docs/samples/sample_modbus/counter.py b/docs/samples/sample_modbus/counter.py index 1b878663c6..5b9650c742 100644 --- a/docs/samples/sample_modbus/counter.py +++ b/docs/samples/sample_modbus/counter.py @@ -11,15 +11,16 @@ class SampleCounter(AbstractCounter): - def __init__(self, device_id: int, component_config: SampleCounterSetup) -> None: + def __init__(self, device_id: int, component_config: SampleCounterSetup, client: ModbusTcpClient_) -> None: self.__device_id = device_id self.component_config = dataclass_from_dict(SampleCounterSetup, component_config) self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="bezug") self.store = get_counter_value_store(self.component_config.id) self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config)) + self.client = client - def update(self, client: ModbusTcpClient_): - power = client.read_holding_registers(reg, ModbusDataType.INT_32, unit=unit) + def update(self): + power = self.client.read_holding_registers(reg, ModbusDataType.INT_32, unit=unit) imported, exported = self.sim_counter.sim_count(power) counter_state = CounterState( diff --git a/docs/samples/sample_modbus/device.py b/docs/samples/sample_modbus/device.py index 22b1957857..07f9a88026 100644 --- a/docs/samples/sample_modbus/device.py +++ b/docs/samples/sample_modbus/device.py @@ -18,19 +18,19 @@ def create_device(device_config: Sample): def create_bat_component(component_config: SampleBatSetup): - return SampleBat(device_config.id, component_config, device_config.configuration.ip_address) + return SampleBat(device_config.id, component_config, device_config.configuration.ip_address, client) def create_counter_component(component_config: SampleCounterSetup): - return SampleCounter(device_config.id, component_config, device_config.configuration.ip_address) + return SampleCounter(device_config.id, component_config, device_config.configuration.ip_address, client) def create_inverter_component(component_config: SampleInverterSetup): - return SampleInverter(device_config.id, component_config, device_config.configuration.ip_address) + return SampleInverter(device_config.id, component_config, device_config.configuration.ip_address, client) def update_components(components: Iterable[Union[SampleBat, SampleCounter, SampleInverter]]): - with client as c: + with client: for component in components: with SingleComponentUpdateContext(component.fault_state): - component.update(c) + component.update() try: client = ModbusTcpClient_(device_config.configuration.ip_address, device_config.configuration.port) diff --git a/docs/samples/sample_modbus/inverter.py b/docs/samples/sample_modbus/inverter.py index 28011f5fd4..4cf593294a 100644 --- a/docs/samples/sample_modbus/inverter.py +++ b/docs/samples/sample_modbus/inverter.py @@ -11,15 +11,16 @@ class SampleInverter(AbstractInverter): - def __init__(self, device_id: int, component_config: SampleInverterSetup) -> None: + def __init__(self, device_id: int, component_config: SampleInverterSetup, client: ModbusTcpClient_) -> None: self.__device_id = device_id self.component_config = dataclass_from_dict(SampleInverterSetup, component_config) self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="pv") self.store = get_inverter_value_store(self.component_config.id) self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config)) + self.client = client - def update(self, client: ModbusTcpClient_) -> None: - power = client.read_holding_registers(reg, ModbusDataType.INT_32, unit=unit) + def update(self) -> None: + power = self.client.read_holding_registers(reg, ModbusDataType.INT_32, unit=unit) exported = self.sim_counter.sim_count(power)[1] inverter_state = InverterState(