-
Notifications
You must be signed in to change notification settings - Fork 120
Add Universeller Modbus #3699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
LKuemmel
merged 7 commits into
openWB:master
from
AlexanderHa98:feature_universelles_modbus_device
Jul 28, 2026
Merged
Add Universeller Modbus #3699
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7caffa9
Add Universeller Modbus
AlexanderHa98 4189fca
fix
AlexanderHa98 adc6fed
fix
AlexanderHa98 c1986d7
Fix imports
AlexanderHa98 ea2c51c
Add suggestions and refactor
AlexanderHa98 39f9aa5
Add suggestions
AlexanderHa98 294ebe3
Add requested changes
AlexanderHa98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| #!/usr/bin/env python3 | ||
| from typing import TypedDict, Any | ||
|
|
||
| from modules.common.abstract_device import AbstractBat | ||
| from modules.common.component_state import BatState | ||
| from modules.common.component_type import ComponentDescriptor | ||
| from modules.common.fault_state import ComponentInfo, FaultState | ||
| from modules.common.modbus import ModbusTcpClient_ | ||
| from modules.common.store._factory import get_component_value_store | ||
| from modules.devices.generic.modbus.config import GenericModbusBatSetup, GenericModbusConfiguration | ||
| from modules.common.utils.peak_filter import PeakFilter | ||
| from modules.common.component_type import ComponentType | ||
| from modules.common.simcount import SimCounter | ||
|
|
||
| from modules.devices.generic.modbus.helper import read_phase_values, read_value | ||
|
|
||
|
|
||
| class KwargsDict(TypedDict): | ||
| device_id: int | ||
| client: ModbusTcpClient_ | ||
| device_configuration: GenericModbusConfiguration | ||
|
|
||
|
|
||
| class GenericModbusBat(AbstractBat): | ||
| def __init__(self, component_config: GenericModbusBatSetup, **kwargs: Any) -> None: | ||
| self.component_config = component_config | ||
| self.kwargs: KwargsDict = kwargs | ||
|
|
||
| def initialize(self) -> None: | ||
| self.__device_id: int = self.kwargs['device_id'] | ||
| self.client: ModbusTcpClient_ = self.kwargs['client'] | ||
| self.store = get_component_value_store(self.component_config.type, self.component_config.id) | ||
| self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config)) | ||
| self.peak_filter = PeakFilter(ComponentType.BAT, self.component_config.id, self.fault_state) | ||
| self.sim_counter = SimCounter(self.__device_id, self.component_config.id, self.component_config.type) | ||
|
|
||
| self.device_configuration = self.kwargs['device_configuration'] | ||
|
|
||
| def update(self) -> None: | ||
|
|
||
| unit = self.component_config.configuration.modbus_id | ||
| config = self.component_config.configuration | ||
|
|
||
| # power | ||
| power = read_value(self.client, unit, self.device_configuration, config.power) | ||
| if power is None: | ||
| raise ValueError("Leistungsregister muss angegeben werden.") | ||
|
|
||
| # SOC | ||
| soc_value = read_value(self.client, unit, self.device_configuration, config.soc) | ||
| if soc_value is not None: | ||
| soc = soc_value | ||
|
|
||
| # currents | ||
| currents_value = read_phase_values(self.client, unit, self.device_configuration, | ||
| config.current_L1, config.current_L2, config.current_L3) | ||
| if currents_value is not None: | ||
| currents = currents_value | ||
|
|
||
| # Import | ||
| imported = read_value(self.client, unit, self.device_configuration, config.imported) | ||
|
|
||
| # Export | ||
| exported = read_value(self.client, unit, self.device_configuration, config.exported) | ||
|
|
||
| # Serial Number | ||
| serial_number_value = read_value(self.client, unit, self.device_configuration, config.serial_number) | ||
| if serial_number_value is not None: | ||
| serial_number = serial_number_value | ||
|
|
||
| if imported is None or exported is None: | ||
| self.peak_filter.check_values(power) | ||
| imported, exported = self.sim_counter.sim_count(power) | ||
| else: | ||
| imported, exported = self.peak_filter.check_values(power, imported, exported) | ||
|
|
||
| bat_state = BatState( | ||
| imported=imported, | ||
| exported=exported, | ||
| power=power, | ||
| ) | ||
|
|
||
| if "soc" in locals(): | ||
| bat_state.soc = soc | ||
| if "currents" in locals(): | ||
| bat_state.currents = currents | ||
| if "serial_number" in locals(): | ||
| bat_state.serial_number = serial_number | ||
|
|
||
| self.store.set(bat_state) | ||
|
|
||
|
|
||
| component_descriptor = ComponentDescriptor(configuration_factory=GenericModbusBatSetup) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| #!/usr/bin/env python3 | ||
| from modules.common.component_setup import ComponentSetup | ||
| from typing import Optional | ||
| from ..vendor import vendor_descriptor | ||
|
|
||
| from dataclasses import dataclass, field | ||
|
|
||
|
|
||
| class GenericModbusConfiguration: | ||
| def __init__(self, ip_address: Optional[str] = None, | ||
| port: int = 502, | ||
| byteorder: Optional[str] = None, | ||
| wordorder: Optional[str] = None) -> None: | ||
| self.ip_address = ip_address | ||
| self.port = port | ||
| self.byteorder = byteorder | ||
| self.wordorder = wordorder | ||
|
|
||
|
|
||
| class GenericModbus: | ||
| def __init__(self, | ||
| name: str = "Modbus", | ||
| type: str = "modbus", | ||
| id: int = 0, | ||
| configuration: GenericModbusConfiguration = None) -> None: | ||
| self.name = name | ||
| self.type = type | ||
| self.vendor = vendor_descriptor.configuration_factory().type | ||
| self.id = id | ||
| self.configuration = configuration or GenericModbusConfiguration() | ||
|
|
||
|
|
||
| @dataclass | ||
| class RegisterConfig: | ||
| reg_address: Optional[int] = None | ||
| reg_type: Optional[str] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class GenericModbusCounterConfiguration: | ||
| modbus_id: int = 1 | ||
| voltage_L1: RegisterConfig = field(default_factory=RegisterConfig) | ||
| voltage_L2: RegisterConfig = field(default_factory=RegisterConfig) | ||
| voltage_L3: RegisterConfig = field(default_factory=RegisterConfig) | ||
| current_L1: RegisterConfig = field(default_factory=RegisterConfig) | ||
| current_L2: RegisterConfig = field(default_factory=RegisterConfig) | ||
| current_L3: RegisterConfig = field(default_factory=RegisterConfig) | ||
| powers_L1: RegisterConfig = field(default_factory=RegisterConfig) | ||
| powers_L2: RegisterConfig = field(default_factory=RegisterConfig) | ||
| powers_L3: RegisterConfig = field(default_factory=RegisterConfig) | ||
| power_factor_L1: RegisterConfig = field(default_factory=RegisterConfig) | ||
| power_factor_L2: RegisterConfig = field(default_factory=RegisterConfig) | ||
| power_factor_L3: RegisterConfig = field(default_factory=RegisterConfig) | ||
| imported: RegisterConfig = field(default_factory=RegisterConfig) | ||
| exported: RegisterConfig = field(default_factory=RegisterConfig) | ||
| power: RegisterConfig = field(default_factory=RegisterConfig) | ||
| frequency: RegisterConfig = field(default_factory=RegisterConfig) | ||
| serial_number: RegisterConfig = field(default_factory=RegisterConfig) | ||
|
|
||
|
|
||
| class GenericModbusCounterSetup(ComponentSetup[GenericModbusCounterConfiguration]): | ||
| def __init__(self, | ||
| name: str = "Universeller Modbus Zähler", | ||
| type: str = "counter", | ||
| id: int = 0, | ||
| configuration: GenericModbusCounterConfiguration = None) -> None: | ||
| super().__init__(name, type, id, configuration or GenericModbusCounterConfiguration()) | ||
|
|
||
|
|
||
| @dataclass | ||
| class GenericModbusBatConfiguration: | ||
| modbus_id: int = 1 | ||
| current_L1: RegisterConfig = field(default_factory=RegisterConfig) | ||
| current_L2: RegisterConfig = field(default_factory=RegisterConfig) | ||
| current_L3: RegisterConfig = field(default_factory=RegisterConfig) | ||
| imported: RegisterConfig = field(default_factory=RegisterConfig) | ||
| exported: RegisterConfig = field(default_factory=RegisterConfig) | ||
| power: RegisterConfig = field(default_factory=RegisterConfig) | ||
| soc: RegisterConfig = field(default_factory=RegisterConfig) | ||
| serial_number: RegisterConfig = field(default_factory=RegisterConfig) | ||
|
|
||
|
|
||
| class GenericModbusBatSetup(ComponentSetup[GenericModbusBatConfiguration]): | ||
| def __init__(self, | ||
| name: str = "Universeller Modbus Batterie", | ||
| type: str = "bat", | ||
| id: int = 0, | ||
| configuration: GenericModbusBatConfiguration = None) -> None: | ||
| super().__init__(name, type, id, configuration or GenericModbusBatConfiguration()) | ||
|
|
||
|
|
||
| @dataclass | ||
| class GenericModbusInverterConfiguration: | ||
| modbus_id: int = 1 | ||
| current_L1: RegisterConfig = field(default_factory=RegisterConfig) | ||
| current_L2: RegisterConfig = field(default_factory=RegisterConfig) | ||
| current_L3: RegisterConfig = field(default_factory=RegisterConfig) | ||
| imported: RegisterConfig = field(default_factory=RegisterConfig) | ||
| exported: RegisterConfig = field(default_factory=RegisterConfig) | ||
| power: RegisterConfig = field(default_factory=RegisterConfig) | ||
| dc_power: RegisterConfig = field(default_factory=RegisterConfig) | ||
| serial_number: RegisterConfig = field(default_factory=RegisterConfig) | ||
|
|
||
|
|
||
| class GenericModbusInverterSetup(ComponentSetup[GenericModbusInverterConfiguration]): | ||
| def __init__(self, | ||
| name: str = "Universeller Modbus Wechselrichter", | ||
| type: str = "inverter", | ||
| id: int = 0, | ||
| configuration: GenericModbusInverterConfiguration = None) -> None: | ||
| super().__init__(name, type, id, configuration or GenericModbusInverterConfiguration()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| #!/usr/bin/env python3 | ||
| from typing import Any, TypedDict | ||
| from modules.devices.generic.modbus.helper import read_phase_values, read_value | ||
| from modules.common.abstract_device import AbstractCounter | ||
| from modules.common.component_state import CounterState | ||
| from modules.common.fault_state import ComponentInfo, FaultState | ||
| from modules.common.component_type import ComponentDescriptor | ||
| from modules.common.simcount._simcounter import SimCounter | ||
| from modules.devices.generic.modbus.config import GenericModbusCounterSetup, GenericModbusConfiguration | ||
| from modules.common.utils.peak_filter import PeakFilter | ||
| from modules.common.component_type import ComponentType | ||
|
|
||
| from modules.common.store._factory import get_component_value_store | ||
|
|
||
| from modules.common.modbus import ModbusTcpClient_ | ||
|
|
||
| import logging | ||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class KwargsDict(TypedDict): | ||
| device_id: int | ||
| client: ModbusTcpClient_ | ||
| device_configuration: GenericModbusConfiguration | ||
|
|
||
|
|
||
| class GenericModbusCounter(AbstractCounter): | ||
| def __init__(self, component_config: GenericModbusCounterSetup, **kwargs: Any) -> None: | ||
| self.component_config = component_config | ||
| self.kwargs: KwargsDict = kwargs | ||
|
|
||
| def initialize(self) -> None: | ||
| self.__device_id: int = self.kwargs['device_id'] | ||
| self.client: ModbusTcpClient_ = self.kwargs['client'] | ||
| self.sim_counter = SimCounter(self.__device_id, self.component_config.id, self.component_config.type) | ||
| self.store = get_component_value_store(self.component_config.type, self.component_config.id) | ||
| self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config)) | ||
| self.peak_filter = PeakFilter(ComponentType.COUNTER, self.component_config.id, self.fault_state) | ||
|
|
||
| self.device_configuration = self.kwargs['device_configuration'] | ||
|
|
||
| def update(self) -> None: | ||
| unit = self.component_config.configuration.modbus_id | ||
| config = self.component_config.configuration | ||
|
|
||
| # Power | ||
| power = read_value(self.client, unit, self.device_configuration, config.power) | ||
| if power is None: | ||
| raise ValueError("Leistungsregister muss angegeben werden.") | ||
|
|
||
| # Voltages | ||
| voltages_value = read_phase_values(self.client, unit, self.device_configuration, | ||
| config.voltage_L1, config.voltage_L2, config.voltage_L3) | ||
| if voltages_value is not None: | ||
| voltages = voltages_value | ||
|
|
||
| # Currents | ||
| currents_value = read_phase_values(self.client, unit, self.device_configuration, | ||
| config.current_L1, config.current_L2, config.current_L3) | ||
| if currents_value is not None: | ||
| currents = currents_value | ||
|
|
||
| # Powers | ||
| powers_value = read_phase_values(self.client, unit, self.device_configuration, | ||
| config.powers_L1, config.powers_L2, config.powers_L3) | ||
| if powers_value is not None: | ||
| powers = powers_value | ||
|
|
||
| # Power Factors | ||
| power_factors_value = read_phase_values( | ||
| self.client, | ||
| unit, | ||
| self.device_configuration, | ||
| config.power_factor_L1, | ||
| config.power_factor_L2, | ||
| config.power_factor_L3, | ||
| ) | ||
| if power_factors_value is not None: | ||
| power_factors = power_factors_value | ||
|
|
||
| # Frequency | ||
| frequency_value = read_value(self.client, unit, self.device_configuration, config.frequency) | ||
| if frequency_value is not None: | ||
| frequency = frequency_value | ||
|
|
||
| # Imported | ||
| imported = read_value(self.client, unit, self.device_configuration, config.imported) | ||
|
|
||
| # Exported | ||
| exported = read_value(self.client, unit, self.device_configuration, config.exported) | ||
|
|
||
| # Serial Number | ||
| serial_number_value = read_value(self.client, unit, self.device_configuration, config.serial_number) | ||
| if serial_number_value is not None: | ||
| serial_number = serial_number_value | ||
|
|
||
| if power is not None: | ||
| self.peak_filter.check_values(power) | ||
| if imported is None or exported is None: | ||
| imported, exported = self.sim_counter.sim_count(power) | ||
| imported, exported = self.peak_filter.check_values(power, imported, exported) | ||
|
|
||
| counter_state = CounterState( | ||
| imported=imported, | ||
| exported=exported, | ||
| power=power | ||
| ) | ||
|
|
||
| if "voltages" in locals(): | ||
| counter_state.voltages = voltages | ||
| if "currents" in locals(): | ||
| counter_state.currents = currents | ||
| if "powers" in locals(): | ||
| counter_state.powers = powers | ||
| if "power_factors" in locals(): | ||
| counter_state.power_factors = power_factors | ||
| if "frequency" in locals(): | ||
| counter_state.frequency = frequency | ||
| if "serial_number" in locals(): | ||
| counter_state.serial_number = serial_number | ||
|
|
||
| self.store.set(counter_state) | ||
|
|
||
|
|
||
| component_descriptor = ComponentDescriptor(configuration_factory=GenericModbusCounterSetup) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #!/usr/bin/env python3 | ||
| from typing import Iterable, Union | ||
| import logging | ||
|
|
||
| from modules.devices.generic.modbus.bat import GenericModbusBat | ||
| from modules.devices.generic.modbus.inverter import GenericModbusInverter | ||
| from modules.common.abstract_device import DeviceDescriptor | ||
| from modules.common.component_context import SingleComponentUpdateContext | ||
| from modules.common.configurable_device import ComponentFactoryByType, ConfigurableDevice, MultiComponentUpdater | ||
| from modules.devices.generic.modbus.counter import GenericModbusCounter | ||
|
|
||
| from modules.devices.generic.modbus.config import ( | ||
| GenericModbus, | ||
| GenericModbusCounterSetup, | ||
| GenericModbusBatSetup, | ||
| GenericModbusInverterSetup, | ||
| ) | ||
|
|
||
| from modules.common import modbus | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def create_device(device_config: GenericModbus): | ||
| client = None | ||
|
|
||
| def create_counter_component(component_config: GenericModbusCounterSetup): | ||
| return GenericModbusCounter(component_config, | ||
| device_id=device_config.id, | ||
| client=client, | ||
| device_configuration=device_config.configuration) | ||
|
|
||
| def create_bat_component(component_config: GenericModbusBatSetup): | ||
| return GenericModbusBat(component_config, | ||
| device_id=device_config.id, | ||
| client=client, | ||
| device_configuration=device_config.configuration) | ||
|
|
||
| def create_inverter_component(component_config: GenericModbusInverterSetup): | ||
| return GenericModbusInverter(component_config, | ||
| device_id=device_config.id, | ||
| client=client, | ||
| device_configuration=device_config.configuration) | ||
|
|
||
| def update_components(components: Iterable[Union[GenericModbusCounter, GenericModbusBat, GenericModbusInverter]]): | ||
| for component in components: | ||
| with SingleComponentUpdateContext(component.fault_state): | ||
| component.update() | ||
|
|
||
| def initializer(): | ||
| nonlocal client | ||
| client = modbus.ModbusTcpClient_(device_config.configuration.ip_address, device_config.configuration.port) | ||
|
|
||
| return ConfigurableDevice( | ||
| device_config=device_config, | ||
| initializer=initializer, | ||
| component_factory=ComponentFactoryByType( | ||
| counter=create_counter_component, | ||
| inverter=create_inverter_component, | ||
| bat=create_bat_component | ||
| ), | ||
| component_updater=MultiComponentUpdater(update_components) | ||
| ) | ||
|
|
||
|
|
||
| device_descriptor = DeviceDescriptor(configuration_factory=GenericModbus) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unten setzt Du auch auf None, wenn es die Variable nicht in locals gibt. Das ist dann doppelt.