From 21384e34d5785e24e98fcc506cfef8e00ee9e093 Mon Sep 17 00:00:00 2001 From: Maxify23 <95931708+Maxify23@users.noreply.github.com> Date: Fri, 2 Jun 2023 14:30:47 +0200 Subject: [PATCH] Sungrow Fixes (#2736) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update main.sh * Update main.sh * Update Moduls * Update Modules * Update counter.py * Update device.py * Update inverter.py * Update device.py Längenanpassung * Update device.py Verbesserung Länge zeilen * Update modulconfigbat.php * Update modulconfigevu.php * Update modulconfigpv.php * Update modulconfigpv.php * Update device.py remove trailing whitespace * test * Reformat code to avoid Error: Wrong indentation type(spaces instead of tabs) --- modules/bezug_sungrow/main.sh | 8 ++- modules/speicher_sungrow/main.sh | 2 +- modules/wr2_sungrow/main.sh | 3 +- modules/wr_sungrow/main.sh | 7 +- packages/modules/devices/sungrow/counter.py | 13 ++-- packages/modules/devices/sungrow/device.py | 67 +++++++++++++------- packages/modules/devices/sungrow/inverter.py | 3 +- web/settings/modulconfigbat.php | 20 ++---- web/settings/modulconfigevu.php | 14 +--- web/settings/modulconfigpv.php | 35 +++++++++- 10 files changed, 110 insertions(+), 62 deletions(-) diff --git a/modules/bezug_sungrow/main.sh b/modules/bezug_sungrow/main.sh index 014157688..bd14d8cd2 100755 --- a/modules/bezug_sungrow/main.sh +++ b/modules/bezug_sungrow/main.sh @@ -10,8 +10,12 @@ else MYLOGFILE="$RAMDISKDIR/evu.log" fi -bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.sungrow.device" "counter" "$speicher1_ip" "$sungrowspeicherport" "$sungrowspeicherid" "$sungrowsr" >>"$MYLOGFILE" 2>&1 -ret=$? +if [[ "$pvwattmodul" == "wr_sungrow" ]]; then + echo "value read at pv modul" >/dev/null +else + bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.sungrow.device" "counter" "$pv1_ipa" "$pv1_ida" "$sungrowsr" "0" "0" >>"$MYLOGFILE" 2>&1 + ret=$? +fi openwbDebugLog $DMOD 2 "EVU RET: $ret" diff --git a/modules/speicher_sungrow/main.sh b/modules/speicher_sungrow/main.sh index e73f14ff3..e79b20a34 100755 --- a/modules/speicher_sungrow/main.sh +++ b/modules/speicher_sungrow/main.sh @@ -10,7 +10,7 @@ else MYLOGFILE="$RAMDISKDIR/bat.log" fi -bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.sungrow.device" "bat" "$speicher1_ip" "$sungrowspeicherport" "$sungrowspeicherid" >>"$MYLOGFILE" 2>&1 +bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.sungrow.device" "bat" "$pv1_ipa" "$pv1_ida" "0" "0" >>"$MYLOGFILE" 2>&1 ret=$? openwbDebugLog $DMOD 2 "BAT RET: $ret" diff --git a/modules/wr2_sungrow/main.sh b/modules/wr2_sungrow/main.sh index 4ec64e43c..4dffde2a6 100755 --- a/modules/wr2_sungrow/main.sh +++ b/modules/wr2_sungrow/main.sh @@ -9,7 +9,8 @@ if [ ${DMOD} == "MAIN" ]; then else MYLOGFILE="$RAMDISKDIR/nurpv.log" fi +read_counter=0 -bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.sungrow.device" "inverter" "$pv2ip" "$pv2port" "$pv2id" "2" >>"$MYLOGFILE" 2>&1 +bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.sungrow.device" "inverter" "$pv2ip" "$pv2id" "2" "$read_counter" "$sungrow2sr" >>"$MYLOGFILE" 2>&1 cat "$RAMDISKDIR/pv2watt" diff --git a/modules/wr_sungrow/main.sh b/modules/wr_sungrow/main.sh index eeb7ea2a1..1b8d6941a 100755 --- a/modules/wr_sungrow/main.sh +++ b/modules/wr_sungrow/main.sh @@ -10,6 +10,11 @@ else MYLOGFILE="$RAMDISKDIR/nurpv.log" fi -bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.sungrow.device" "inverter" "$speicher1_ip" "$sungrowspeicherport" "$sungrowspeicherid" "1" >>"$MYLOGFILE" 2>&1 +if [[ "$wattbezugmodul" == "bezug_sungrow" ]]; then + read_counter=1 +else + read_counter=0 +fi +bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.sungrow.device" "inverter" "$pv1_ipa" "$pv1_ida" "1" "$read_counter" "$sungrowsr" >>"$MYLOGFILE" 2>&1 cat "$RAMDISKDIR/pvwatt" diff --git a/packages/modules/devices/sungrow/counter.py b/packages/modules/devices/sungrow/counter.py index 01b2aae3a..e0802c43d 100644 --- a/packages/modules/devices/sungrow/counter.py +++ b/packages/modules/devices/sungrow/counter.py @@ -27,7 +27,7 @@ def __init__(self, self.store = get_counter_value_store(self.component_config.id) self.component_info = ComponentInfo.from_component_config(self.component_config) - def update(self): + def update(self, pv_power: float): unit = self.__device_modbus_id if self.component_config.configuration.version == Version.SH: power = self.__tcp_client.read_input_registers(13009, ModbusDataType.INT_32, @@ -38,10 +38,13 @@ def update(self): # powers = [power / 10 for power in powers] # log.info("power: " + str(power) + " powers?: " + str(powers)) else: - power = self.__tcp_client.read_input_registers(5082, ModbusDataType.INT_32, - wordorder=Endian.Little, unit=unit) - if self.component_config.configuration.version == Version.SG_winet_dongle: - power = power * -1 + if pv_power != 0: + power = self.__tcp_client.read_input_registers(5082, ModbusDataType.INT_32, + wordorder=Endian.Little, unit=unit) + else: + power = self.__tcp_client.read_input_registers(5090, ModbusDataType.INT_32, + wordorder=Endian.Little, unit=unit) + # no valid data for powers per phase # powers = self.__tcp_client.read_input_registers(5084, [ModbusDataType.UINT_16] * 3, # wordorder=Endian.Little, unit=unit) diff --git a/packages/modules/devices/sungrow/device.py b/packages/modules/devices/sungrow/device.py index 40f69619b..147f65326 100644 --- a/packages/modules/devices/sungrow/device.py +++ b/packages/modules/devices/sungrow/device.py @@ -6,7 +6,7 @@ from helpermodules.cli import run_using_positional_cli_args from modules.common import modbus from modules.common.abstract_device import AbstractDevice, DeviceDescriptor -from modules.common.component_context import SingleComponentUpdateContext +from modules.common.component_context import MultiComponentUpdateContext from modules.devices.sungrow import bat from modules.devices.sungrow import counter from modules.devices.sungrow import inverter @@ -14,10 +14,8 @@ SungrowInverterSetup) from modules.devices.sungrow.version import Version - log = logging.getLogger(__name__) - sungrow_component_classes = Union[bat.SungrowBat, counter.SungrowCounter, inverter.SungrowInverter] @@ -33,21 +31,21 @@ def __init__(self, device_config: Union[Dict, Sungrow]) -> None: try: self.device_config = dataclass_from_dict(Sungrow, device_config) self.client = modbus.ModbusTcpClient_(self.device_config.configuration.ip_address, - self.device_config.configuration.port) + 502) except Exception: - log.exception("Fehler im Modul "+self.device_config.name) + log.exception("Fehler im Modul " + self.device_config.name) def add_component(self, - component_config: Union[Dict, - SungrowBatSetup, SungrowCounterSetup, SungrowInverterSetup]) -> None: + component_config: Union[ + Dict, SungrowBatSetup, SungrowCounterSetup, SungrowInverterSetup]) -> None: if isinstance(component_config, Dict): component_type = component_config["type"] else: component_type = component_config.type - component_config = dataclass_from_dict(COMPONENT_TYPE_TO_MODULE[ - component_type].component_descriptor.configuration_factory, component_config) + component_config = dataclass_from_dict( + COMPONENT_TYPE_TO_MODULE[component_type].component_descriptor.configuration_factory, component_config) if component_type in self.COMPONENT_TYPE_TO_CLASS: - self.components["component"+str(component_config.id)] = (self.COMPONENT_TYPE_TO_CLASS[component_type]( + self.components["component" + str(component_config.id)] = (self.COMPONENT_TYPE_TO_CLASS[component_type]( self.device_config.id, self.device_config.configuration.modbus_id, component_config, self.client)) else: raise Exception( @@ -59,10 +57,13 @@ def update(self) -> None: log.debug("Start device reading " + str(self.components)) if self.components: with self.client: - for component in self.components: - # Auch wenn bei einer Komponente ein Fehler auftritt, sollen alle anderen noch ausgelesen werden. - with SingleComponentUpdateContext(self.components[component].component_info): - self.components[component].update() + with MultiComponentUpdateContext(self.components): + for component in self.components: + if isinstance(component, inverter.SungrowInverter): + pv_power = component.update() + for component in self.components: + if isinstance(component, counter.SungrowCounter): + component.update(pv_power) else: log.warning( self.device_config.name + @@ -77,27 +78,49 @@ def update(self) -> None: } -def read_legacy(ip_address: str, port: int, modbus_id: int, component_config: dict): +def read_legacy(ip_address: str, + modbus_id: int, + component_config: dict): device_config = Sungrow() device_config.configuration.ip_address = ip_address - device_config.configuration.port = port + device_config.configuration.port = 502 device_config.configuration.modbus_id = modbus_id dev = Device(device_config) dev.add_component(component_config) dev.update() -def read_legacy_bat(ip_address: str, port: int, modbus_id: int, num: Optional[int] = None): - read_legacy(ip_address, port, modbus_id, bat.component_descriptor.configuration_factory(id=None)) +def read_legacy_bat(ip_address: str, modbus_id: int, + num: Optional[int] = None, + read_counter: Optional[int] = None, + version: Optional[int] = None): + read_legacy(ip_address, modbus_id, bat.component_descriptor.configuration_factory(id=None)) -def read_legacy_counter(ip_address: str, port: int, modbus_id: int, version: int): - read_legacy(ip_address, port, modbus_id, counter.component_descriptor.configuration_factory( +def read_legacy_counter(ip_address: str, modbus_id: int, + version: int, + read_counter: int, + unused_version: int): + read_legacy(ip_address, modbus_id, counter.component_descriptor.configuration_factory( id=None, configuration=SungrowCounterConfiguration(version=Version(version)))) -def read_legacy_inverter(ip_address: str, port: int, modbus_id: int, num: int): - read_legacy(ip_address, port, modbus_id, inverter.component_descriptor.configuration_factory(id=num)) +def read_legacy_inverter(ip_address: str, + + modbus_id: int, + num: int, + read_counter: int, + version: int): + device_config = Sungrow() + device_config.configuration.ip_address = ip_address + device_config.configuration.port = 502 + device_config.configuration.modbus_id = modbus_id + dev = Device(device_config) + dev.add_component(inverter.component_descriptor.configuration_factory(id=num)) + if read_counter == 1: + dev.add_component(counter.component_descriptor.configuration_factory( + id=None, configuration=SungrowCounterConfiguration(version=Version(version)))) + dev.update() def main(argv: List[str]): diff --git a/packages/modules/devices/sungrow/inverter.py b/packages/modules/devices/sungrow/inverter.py index 0be1b1be1..375567a42 100644 --- a/packages/modules/devices/sungrow/inverter.py +++ b/packages/modules/devices/sungrow/inverter.py @@ -26,7 +26,7 @@ def __init__(self, self.store = get_inverter_value_store(self.component_config.id) self.component_info = ComponentInfo.from_component_config(self.component_config) - def update(self) -> None: + def update(self) -> float: unit = self.__device_modbus_id power = self.__tcp_client.read_input_registers(5016, ModbusDataType.UINT_32, @@ -40,6 +40,7 @@ def update(self) -> None: exported=exported ) self.store.set(inverter_state) + return power component_descriptor = ComponentDescriptor(configuration_factory=SungrowInverterSetup) diff --git a/web/settings/modulconfigbat.php b/web/settings/modulconfigbat.php index 117b0ac93..724568b7e 100644 --- a/web/settings/modulconfigbat.php +++ b/web/settings/modulconfigbat.php @@ -90,7 +90,7 @@ - + @@ -261,22 +261,11 @@ function visibility_solarwatt_ip2() {