From 2f24dc4ecc77af2eb2af9fea0a9be56abd3a9786 Mon Sep 17 00:00:00 2001 From: Philippe Date: Mon, 29 Jan 2024 02:56:52 +0000 Subject: [PATCH 1/7] Modifications to support Solax modbusmap --- examples/config_solax_x1_ess_g44.yaml | 203 ++++++++++++++++++++++++++ modbus2mqtt.py | 25 +++- 2 files changed, 223 insertions(+), 5 deletions(-) create mode 100644 examples/config_solax_x1_ess_g44.yaml diff --git a/examples/config_solax_x1_ess_g44.yaml b/examples/config_solax_x1_ess_g44.yaml new file mode 100644 index 0000000..747ba3b --- /dev/null +++ b/examples/config_solax_x1_ess_g44.yaml @@ -0,0 +1,203 @@ +mqtt: + host: "192.168.0.147" + port: 1883 + tls: false + username: "jeedom" + password: "0Ta9jUaIdFO8R1kFXPyeKCpLdmJeIJWi3mjV3bEi6B7psBBPw2QySBLjHhKl4mJV" + topic_prefix: "pv" + +# Modbus register example for Deye SUN-xK-SG04LP3-EU +schema: +- name: solax_x1_g4 + readings: + - name: "System State" + topic: "system/state" + register: 9 + length: 1 + signed: 0 + substract: 0 + divide: 1 + typereg: "Input" + - name: "System Use Mode" + topic: "system/usemode" + register: 139 + length: 1 + signed: 0 + substract: 0 + divide: 1 + typereg: "holding" + - name: "To grid energytotal" + topic: "system/totals/grid_feedin" + register: 72 + length: 2 + substract: 0 + decimals: 1 + divide: 100 + typereg: "Input" + - name: "From grid energy total" + topic: "system/totals/grid_energy" + register: 74 + length: 2 + substract: 0 + decimals: 1 + divide: 100 + typereg: "Input" + - name: "Yield total" + topic: "system/totals/yield" + register: 82 + length: 2 + substract: 0 + decimals: 1 + divide: 10 + typereg: "Input" + - name: "Grid yield Today" + topic: "grid/today/yield" + register: 80 + length: 2 + substract: 0 + decimals: 1 + divide: 10 + typereg: "Input" + - name: "Grid Sold Today" + topic: "grid/today/sold" + register: 152 + length: 2 + substract: 0 + decimals: 1 + divide: 100 + typereg: "Input" + - name: "Grid Bought Today" + topic: "grid/today/bought" + register: 154 + length: 2 + substract: 0 + decimals: 1 + divide: 100 + typereg: "Input" + - name: "Grid Frequency" + topic: "grid/frequency" + register: 7 + length: 1 + substract: 0 + decimals: 0 + divide: 100 + typereg: "Input" + - name: "Grid L1 Power" + topic: "grid/now/l1/power" + register: 70 + length: 1 + substract: 0 + decimals: 0 + signed: 1 + divide: 1 + typereg: "Input" + - name: "Grid L1 Volt" + topic: "grid/now/l1/volt" + register: 0 + length: 1 + substract: 0 + decimals: 1 + divide: 10 + typereg: "Input" + - name: "Load Power L1" + topic: "load/now/l1/power" + register: 2 + length: 1 + substract: 0 + decimals: 0 + signed: 1 + divide: 1 + typereg: "Input" + - name: "Load Voltage L1" + topic: "load/now/l1/volt" + register: 0 + length: 1 + substract: 0 + decimals: 1 + divide: 10 + typereg: "Input" + - name: "PV Production Today" + topic: "pv/today" + register: 150 + length: 1 + substract: 0 + decimals: 1 + divide: 10 + typereg: "Input" + - name: "PV String 1 Power" + topic: "pv/string/1/power" + register: 10 + length: 1 + substract: 0 + divide: 1 + typereg: "Input" + - name: "PV String 2 Power" + topic: "pv/string/2/power" + register: 11 + length: 1 + substract: 0 + divide: 1 + typereg: "Input" + - name: "Battery SOC" + topic: "battery/soc" + register: 28 + length: 1 + substract: 0 + divide: 1 + typereg: "Input" + - name: "Battery Charged Today" + topic: "battery/today/charged" + register: 35 + length: 1 + substract: 0 + decimals: 1 + divide: 10 + typereg: "Input" + - name: "Battery Discharged Today" + topic: "battery/today/discharged" + register: 32 + length: 1 + substract: 0 + divide: 10 + typereg: "Input" + - name: "Battery Temperature" + topic: "battery/temperature" + register: 24 + length: 1 + substract: 0 + decimals: 1 + divide: 1 + typereg: "Input" + - name: "Battery Voltage" + topic: "battery/volt" + register: 20 + length: 1 + substract: 0 + decimals: 2 + divide: 10 + typereg: "Input" + - name: "Battery Output Power" + topic: "battery/power" + register: 22 + length: 1 + signed: 1 + substract: 0 + decimals: 0 + divide: 1 + typereg: "Input" + - name: "Battery Output Current" + topic: "battery/current" + register: 21 + length: 1 + signed: 1 + substract: 0 + decimals: 2 + divide: 10 + typereg: "Input" + +sources: +- name: "Solax X1" + host: "192.168.0.87" + port: 502 + schema: solax_x1_g4 + topic_prefix: "x1" diff --git a/modbus2mqtt.py b/modbus2mqtt.py index b609e33..8e1739e 100755 --- a/modbus2mqtt.py +++ b/modbus2mqtt.py @@ -8,6 +8,7 @@ import yaml import json import re +import struct from typing import List from queue import Queue from threading import Thread, Lock @@ -104,7 +105,7 @@ def rpc_subscribe(self, src): def rpc_unsubscribe(self, src): t = src.topic_prefix if self.topic_prefix is not None: - t = self.topic_prefix + '/' + t + t = self.topic_prefix + '/' + t if self.subscribers.get(t.lower() + '/rpc', None) is not None: log.info(f"Unsubscribing from rpc topic {t}/rpc.") self.client.unsubscribe(t + '/rpc') @@ -233,7 +234,7 @@ def set_value(self, src, params): class HoldingRegister(Register): - def __init__(self, name: str, topic: str, register: int, length: int = 2, + def __init__(self, name: str, topic: str, register: int, typereg: str, length: int = 2, mode: str = "r", substract: float = 0, divide: float = 1, decimals: int = 0, signed: bool = False, unitid: int = None, **kvargs): super().__init__(name, topic, register, length, mode, unitid=unitid) @@ -241,13 +242,18 @@ def __init__(self, name: str, topic: str, register: int, length: int = 2, self.decimals = decimals self.substract = substract self.signed = signed + self.typereg = typereg def get_value(self, src): unitid = self.unitid if unitid is None: unitid = src.unitid - - rr = src.client.read_holding_registers(self.start, self.length, slave=unitid) + if ( self.typereg == "holding" ): + rr = src.client.read_holding_registers(self.start, self.length, slave=unitid) + else: + rr = src.client.read_input_registers(self.start, 1, slave=unitid) + if (self.length>1): + rr2 = src.client.read_input_registers(self.start+1, 1, slave=unitid) if not rr: raise ModbusException("Received empty modbus respone.") if rr.isError(): @@ -255,7 +261,16 @@ def get_value(self, src): if isinstance(rr, ExceptionResponse): raise ModbusException(f"Received Modbus library exception ({rr}).") - val = rr.registers[0] + if (self.length>1): + v1 = rr.registers[0] + v2 = rr2.registers[0] + h1 = hex(v1).split('x')[-1] + h2 = hex(v2).split('x')[-1] + h=h2+h1 + val=int(h,16) + else: + val = rr.registers[0] + if self.signed and int(val) >= 32768: val = int(val) - 65535 if self.decimals > 0: From 0ae326ce877a879b3e5b87cfcb02d73714517bef Mon Sep 17 00:00:00 2001 From: Philippe Date: Tue, 27 Feb 2024 21:34:37 +0000 Subject: [PATCH 2/7] Add better indeaness support --- README.md | 4 ++ examples/config_solax_x1_ess_g44.yaml | 94 +++++++++++++++++++++++---- modbus2mqtt.py | 17 +++-- 3 files changed, 99 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 8819339..f5d5a61 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ This is a gateway between Modbus and MQTT. It was originally developed to read r can read from one or more Modbus TCP gateways (e.g. the Waveshare RS485 to Ethernet) simultaneously. The state of the coils can be published as a json message and their state can also be changed with a json message sent to the rpc topic. +## Changes from the upstream version +1) Read input register +2) Basic support of big and little endian registers + ## Configuration Create a configuration file (see examples/). Here is the basic syntax: diff --git a/examples/config_solax_x1_ess_g44.yaml b/examples/config_solax_x1_ess_g44.yaml index 747ba3b..e79b4bb 100644 --- a/examples/config_solax_x1_ess_g44.yaml +++ b/examples/config_solax_x1_ess_g44.yaml @@ -3,10 +3,10 @@ mqtt: port: 1883 tls: false username: "jeedom" - password: "0Ta9jUaIdFO8R1kFXPyeKCpLdmJeIJWi3mjV3bEi6B7psBBPw2QySBLjHhKl4mJV" + password: "0Ta9jUaIdFO8R1V" topic_prefix: "pv" -# Modbus register example for Deye SUN-xK-SG04LP3-EU +# Modbus register example for Solax ESS X1 G4 schema: - name: solax_x1_g4 readings: @@ -26,10 +26,19 @@ schema: substract: 0 divide: 1 typereg: "holding" + - name: "System Temperature" + topic: "system/temperature" + register: 8 + length: 1 + signed: 1 + substract: 0 + divide: 1 + typereg: "Input" - name: "To grid energytotal" topic: "system/totals/grid_feedin" register: 72 length: 2 + lindean: 1 substract: 0 decimals: 1 divide: 100 @@ -40,12 +49,14 @@ schema: length: 2 substract: 0 decimals: 1 + lindean: 1 divide: 100 typereg: "Input" - name: "Yield total" - topic: "system/totals/yield" + topic: "system/totals/gridyield" register: 82 length: 2 + lindean: 1 substract: 0 decimals: 1 divide: 10 @@ -54,6 +65,24 @@ schema: topic: "grid/today/yield" register: 80 length: 2 + lindean: 1 + substract: 0 + decimals: 1 + divide: 10 + typereg: "Input" + - name: "OffGrid Yield total" + topic: "system/totals/epsyield" + register: 142 + length: 2 + lindean: 1 + substract: 0 + decimals: 1 + divide: 10 + typereg: "Input" + - name: "OffGrid yield Today" + topic: "eps/today/yield" + register: 144 + length: 1 substract: 0 decimals: 1 divide: 10 @@ -62,6 +91,7 @@ schema: topic: "grid/today/sold" register: 152 length: 2 + lindean: 1 substract: 0 decimals: 1 divide: 100 @@ -70,6 +100,7 @@ schema: topic: "grid/today/bought" register: 154 length: 2 + lindean: 1 substract: 0 decimals: 1 divide: 100 @@ -99,23 +130,64 @@ schema: decimals: 1 divide: 10 typereg: "Input" - - name: "Load Power L1" - topic: "load/now/l1/power" - register: 2 + - name: "Grid L1 Current" + topic: "grid/now/l1/amp" + register: 70 + length: 1 + signed: 1 + substract: 0 + decimals: 1 + divide: 230 + typereg: "Input" + - name: "EPS L1 Power" + topic: "eps/now/l1/power" + register: 78 length: 1 substract: 0 decimals: 0 - signed: 1 - divide: 1 typereg: "Input" - - name: "Load Voltage L1" - topic: "load/now/l1/volt" - register: 0 + - name: "EPS L1 Volt" + topic: "eps/now/l1/volt" + register: 76 length: 1 substract: 0 decimals: 1 divide: 10 typereg: "Input" + - name: "EPS L1 Current" + topic: "eps/now/l1/amp" + register: 77 + length: 1 + substract: 0 + decimals: 1 + divide: 10 + typereg: "Input" + - name: "EPS L1 frequency" + topic: "eps/frequency" + register: 79 + length: 1 + substract: 0 + decimals: 1 + divide: 100 + typereg: "Input" + - name: "Load L1 Current" + topic: "load/now/l1/amp" + register: 1 + length: 1 + substract: 0 + signed: 1 + decimals: 1 + divide: 10 + typereg: "Input" + - name: "Grid Power L1" + topic: "load/now/l1/power" + register: 2 + length: 1 + substract: 0 + decimals: 0 + signed: 1 + divide: 1 + typereg: "Input" - name: "PV Production Today" topic: "pv/today" register: 150 diff --git a/modbus2mqtt.py b/modbus2mqtt.py index 8e1739e..52357b8 100755 --- a/modbus2mqtt.py +++ b/modbus2mqtt.py @@ -62,7 +62,7 @@ def on_publish(self, client, userdata, result): def on_connect(self, client, userdata, flags, rc): if rc == 0: - log.info("Connected dto broker %s", self.host) + log.info("Connected to broker %s", self.host) self.is_connected = True else: log.error("Connection to broker %s failed.", self.host) @@ -233,8 +233,11 @@ def set_value(self, src, params): class HoldingRegister(Register): +# Keep the function name but can read holding and input registers. +# pass the parameter "typereg" with the value "holding" or "input" to define the type of register to read. +# pass the parameter "lindean" with the value 0 or 1 (little indean) to define the indeaness of the register to read. (Solax use little endian) - def __init__(self, name: str, topic: str, register: int, typereg: str, length: int = 2, + def __init__(self, name: str, topic: str, register: int, typereg: str, lindean: int = 0, length: int = 2, mode: str = "r", substract: float = 0, divide: float = 1, decimals: int = 0, signed: bool = False, unitid: int = None, **kvargs): super().__init__(name, topic, register, length, mode, unitid=unitid) @@ -243,6 +246,7 @@ def __init__(self, name: str, topic: str, register: int, typereg: str, length: i self.substract = substract self.signed = signed self.typereg = typereg + self.lindean = lindean def get_value(self, src): unitid = self.unitid @@ -251,8 +255,11 @@ def get_value(self, src): if ( self.typereg == "holding" ): rr = src.client.read_holding_registers(self.start, self.length, slave=unitid) else: - rr = src.client.read_input_registers(self.start, 1, slave=unitid) - if (self.length>1): + if (self.lindean == 0): + rr = src.client.read_input_registers(self.start, self.length, slave=unitid) + else: + rr = src.client.read_input_registers(self.start, 1, slave=unitid) + if (self.length>1): rr2 = src.client.read_input_registers(self.start+1, 1, slave=unitid) if not rr: raise ModbusException("Received empty modbus respone.") @@ -261,7 +268,7 @@ def get_value(self, src): if isinstance(rr, ExceptionResponse): raise ModbusException(f"Received Modbus library exception ({rr}).") - if (self.length>1): + if ((self.length>1) and (self.lindean == 1)): v1 = rr.registers[0] v2 = rr2.registers[0] h1 = hex(v1).split('x')[-1] From 0e29b35f4527dbd07f2f26cbcf4f09813d06fbaf Mon Sep 17 00:00:00 2001 From: Philippe Date: Tue, 27 Feb 2024 21:47:45 +0000 Subject: [PATCH 3/7] Add better indeaness support --- modbus2mqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modbus2mqtt.py b/modbus2mqtt.py index 52357b8..01480ba 100755 --- a/modbus2mqtt.py +++ b/modbus2mqtt.py @@ -105,7 +105,7 @@ def rpc_subscribe(self, src): def rpc_unsubscribe(self, src): t = src.topic_prefix if self.topic_prefix is not None: - t = self.topic_prefix + '/' + t + t = self.topic_prefix + '/' + t if self.subscribers.get(t.lower() + '/rpc', None) is not None: log.info(f"Unsubscribing from rpc topic {t}/rpc.") self.client.unsubscribe(t + '/rpc') From bf81b3e10c578366e5c24f4a9b5c3bb1ce854dff Mon Sep 17 00:00:00 2001 From: Philippe Date: Tue, 27 Feb 2024 22:47:39 +0000 Subject: [PATCH 4/7] Pad HEX strings to avoid conversion errors --- modbus2mqtt.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modbus2mqtt.py b/modbus2mqtt.py index 01480ba..e006da4 100755 --- a/modbus2mqtt.py +++ b/modbus2mqtt.py @@ -272,8 +272,11 @@ def get_value(self, src): v1 = rr.registers[0] v2 = rr2.registers[0] h1 = hex(v1).split('x')[-1] + h1 = h1.zfill(4) h2 = hex(v2).split('x')[-1] + h2 = h2.zfill(4) h=h2+h1 + # log.info(f"Got Value {h1} and {h2} from {self.typereg} register {self.start} with length {self.length} from unit {unitid} in little endian mode.") val=int(h,16) else: val = rr.registers[0] From 3f4a65cbe2aa1dfecf4c9c5e3a53212f15e38dd1 Mon Sep 17 00:00:00 2001 From: Philippe Date: Wed, 28 Feb 2024 10:30:19 +0000 Subject: [PATCH 5/7] Change the byte order method, fix multi-byte readings, fix typos --- README.md | 8 ++++++-- modbus2mqtt.py | 37 +++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index f5d5a61..1434454 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ state can also be changed with a json message sent to the rpc topic. ## Changes from the upstream version 1) Read input register 2) Basic support of big and little endian registers +3) Read multi-byte value in big and little endian order +4) Fix the default value of length to 1 ( as in the readme) ## Configuration @@ -46,6 +48,8 @@ schema: substract: 0 # <--- (optional) Value to substract before publishing (default is 0) signed: 1 # <--- (optional) True/false for signed/unsigned value (default is 0) divide: 1 # <--- (optional) Value to divide by before publishing (default is 1) + typereg: holding # <--- (optional) Read holding or input register (default is holding) + littleendian: 0 # <--- (optional) Byte order (default is 0) # Coils @@ -180,11 +184,11 @@ sources: Result: -![MQQT Explorer Screenshot](examples/Deye-SG04LP3-EU/deye-modbus2mqtt-example.jpg) +![MQTT Explorer Screenshot](examples/Deye-SG04LP3-EU/deye-modbus2mqtt-example.jpg) Home Assistant device sensors: -![MQQT Explorer Screenshot](examples/Deye-SG04LP3-EU/ha-mqtt-deye-data.jpg) +![MQTT Explorer Screenshot](examples/Deye-SG04LP3-EU/ha-mqtt-deye-data.jpg) ## Known Issues diff --git a/modbus2mqtt.py b/modbus2mqtt.py index e006da4..7752caf 100755 --- a/modbus2mqtt.py +++ b/modbus2mqtt.py @@ -235,9 +235,9 @@ def set_value(self, src, params): class HoldingRegister(Register): # Keep the function name but can read holding and input registers. # pass the parameter "typereg" with the value "holding" or "input" to define the type of register to read. -# pass the parameter "lindean" with the value 0 or 1 (little indean) to define the indeaness of the register to read. (Solax use little endian) +# pass the parameter "littleendian" with the value False or true (little endian) to define the endianness of the register to read. (Solax use little endian) - def __init__(self, name: str, topic: str, register: int, typereg: str, lindean: int = 0, length: int = 2, + def __init__(self, name: str, topic: str, register: int, typereg: str = "holding", littleendian: bool = False, length: int = 1, mode: str = "r", substract: float = 0, divide: float = 1, decimals: int = 0, signed: bool = False, unitid: int = None, **kvargs): super().__init__(name, topic, register, length, mode, unitid=unitid) @@ -246,7 +246,7 @@ def __init__(self, name: str, topic: str, register: int, typereg: str, lindean: self.substract = substract self.signed = signed self.typereg = typereg - self.lindean = lindean + self.littleendian = littleendian def get_value(self, src): unitid = self.unitid @@ -255,12 +255,7 @@ def get_value(self, src): if ( self.typereg == "holding" ): rr = src.client.read_holding_registers(self.start, self.length, slave=unitid) else: - if (self.lindean == 0): - rr = src.client.read_input_registers(self.start, self.length, slave=unitid) - else: - rr = src.client.read_input_registers(self.start, 1, slave=unitid) - if (self.length>1): - rr2 = src.client.read_input_registers(self.start+1, 1, slave=unitid) + rr = src.client.read_input_registers(self.start, self.length, slave=unitid) if not rr: raise ModbusException("Received empty modbus respone.") if rr.isError(): @@ -268,19 +263,21 @@ def get_value(self, src): if isinstance(rr, ExceptionResponse): raise ModbusException(f"Received Modbus library exception ({rr}).") - if ((self.length>1) and (self.lindean == 1)): - v1 = rr.registers[0] - v2 = rr2.registers[0] - h1 = hex(v1).split('x')[-1] - h1 = h1.zfill(4) - h2 = hex(v2).split('x')[-1] - h2 = h2.zfill(4) - h=h2+h1 - # log.info(f"Got Value {h1} and {h2} from {self.typereg} register {self.start} with length {self.length} from unit {unitid} in little endian mode.") + if ((self.littleendian)): + # Read multiple bytes in little endian mode + h="" + for i in range(0,self.length): + h = hex(rr.registers[i]).split('x')[-1].zfill(4) + h + log.debug(f"Got Value {h} from {self.typereg} register {self.start} with length {self.length} from unit {unitid} in little endian mode.") val=int(h,16) else: - val = rr.registers[0] - + # Read multiple bytes in big endian mode + h="" + for i in range(0,self.length): + h = h + hex(rr.registers[i]).split('x')[-1].zfill(4) + log.debug(f"Got Value {h} from {self.typereg} register {self.start} with length {self.length} from unit {unitid} in big endian mode.") + val=int(h,16) + if self.signed and int(val) >= 32768: val = int(val) - 65535 if self.decimals > 0: From 56f970b887ef9fa47b6f9e7b625ba71652a7b06c Mon Sep 17 00:00:00 2001 From: Philippe Date: Wed, 28 Feb 2024 10:47:41 +0000 Subject: [PATCH 6/7] Fix Code quality check --- modbus2mqtt.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/modbus2mqtt.py b/modbus2mqtt.py index 7752caf..979b0a1 100755 --- a/modbus2mqtt.py +++ b/modbus2mqtt.py @@ -8,7 +8,6 @@ import yaml import json import re -import struct from typing import List from queue import Queue from threading import Thread, Lock @@ -233,9 +232,9 @@ def set_value(self, src, params): class HoldingRegister(Register): -# Keep the function name but can read holding and input registers. -# pass the parameter "typereg" with the value "holding" or "input" to define the type of register to read. -# pass the parameter "littleendian" with the value False or true (little endian) to define the endianness of the register to read. (Solax use little endian) + # Keep the function name but can read holding and input registers. + # pass the parameter "typereg" with the value "holding" or "input" to define the type of register to read. + # pass the parameter "littleendian" with the value False or true (little endian) to define the endianness of the register to read. (Solax use little endian) def __init__(self, name: str, topic: str, register: int, typereg: str = "holding", littleendian: bool = False, length: int = 1, mode: str = "r", substract: float = 0, divide: float = 1, @@ -252,7 +251,7 @@ def get_value(self, src): unitid = self.unitid if unitid is None: unitid = src.unitid - if ( self.typereg == "holding" ): + if (self.typereg == "holding"): rr = src.client.read_holding_registers(self.start, self.length, slave=unitid) else: rr = src.client.read_input_registers(self.start, self.length, slave=unitid) @@ -266,18 +265,18 @@ def get_value(self, src): if ((self.littleendian)): # Read multiple bytes in little endian mode h="" - for i in range(0,self.length): + for i in range(0, self.length): h = hex(rr.registers[i]).split('x')[-1].zfill(4) + h log.debug(f"Got Value {h} from {self.typereg} register {self.start} with length {self.length} from unit {unitid} in little endian mode.") - val=int(h,16) + val=int(h, 16) else: # Read multiple bytes in big endian mode h="" - for i in range(0,self.length): + for i in range(0, self.length): h = h + hex(rr.registers[i]).split('x')[-1].zfill(4) log.debug(f"Got Value {h} from {self.typereg} register {self.start} with length {self.length} from unit {unitid} in big endian mode.") - val=int(h,16) - + val=int(h, 16) + if self.signed and int(val) >= 32768: val = int(val) - 65535 if self.decimals > 0: From 2c15c721e963f944e8d0bc7254680dc9da38d132 Mon Sep 17 00:00:00 2001 From: Philippe Date: Wed, 28 Feb 2024 10:53:58 +0000 Subject: [PATCH 7/7] Fix Code quality check --- modbus2mqtt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modbus2mqtt.py b/modbus2mqtt.py index 979b0a1..c5e43b9 100755 --- a/modbus2mqtt.py +++ b/modbus2mqtt.py @@ -264,18 +264,18 @@ def get_value(self, src): if ((self.littleendian)): # Read multiple bytes in little endian mode - h="" + h = "" for i in range(0, self.length): h = hex(rr.registers[i]).split('x')[-1].zfill(4) + h log.debug(f"Got Value {h} from {self.typereg} register {self.start} with length {self.length} from unit {unitid} in little endian mode.") - val=int(h, 16) + val = int(h, 16) else: # Read multiple bytes in big endian mode - h="" + h = "" for i in range(0, self.length): h = h + hex(rr.registers[i]).split('x')[-1].zfill(4) log.debug(f"Got Value {h} from {self.typereg} register {self.start} with length {self.length} from unit {unitid} in big endian mode.") - val=int(h, 16) + val = int(h, 16) if self.signed and int(val) >= 32768: val = int(val) - 65535