From ec2364892b2609107cd221c7e2f4190c073fa0e3 Mon Sep 17 00:00:00 2001 From: Stefan Hellermann Date: Thu, 7 Nov 2024 11:07:47 +0100 Subject: [PATCH] Fix Solis (#1990) * Solis: fix modbus address for SOC The Solis support was created based on Fox ESS. It looks like the creator missed to adjust the modbus address for SOC, it's 33139 based on a register map I got from solis support. Total discharge energy is also in kWh, not 1/10kWh units. * Solis: read all registers as input registers Every modbus query fails, as all registers need to be read as input registers (code 0x04), instead of holding register. * Solis: Fix for unreachable local variables right now I get the following error: UnboundLocalError: local variable 'power' referenced before assignment Fix it by moving some code around. * Review --------- Co-authored-by: LKuemmel <76958050+LKuemmel@users.noreply.github.com> --- packages/modules/devices/solis/solis/bat.py | 10 +++++----- packages/modules/devices/solis/solis/counter.py | 15 ++++++++------- packages/modules/devices/solis/solis/inverter.py | 10 ++++------ 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/packages/modules/devices/solis/solis/bat.py b/packages/modules/devices/solis/solis/bat.py index fdc29e1f6..3bc72335e 100644 --- a/packages/modules/devices/solis/solis/bat.py +++ b/packages/modules/devices/solis/solis/bat.py @@ -21,12 +21,12 @@ def __init__(self, component_config: SolisBatSetup) -> None: def update(self, client: ModbusTcpClient_) -> None: unit = self.component_config.configuration.modbus_id - power = client.read_holding_registers(33149, ModbusDataType.INT_32, unit=unit) * -1 - soc = client.read_holding_registers(31038, ModbusDataType.UINT_16, unit=unit) + power = client.read_input_registers(33149, ModbusDataType.INT_32, unit=unit) * -1 + soc = client.read_input_registers(33139, ModbusDataType.UINT_16, unit=unit) # Geladen in kWh - imported = client.read_holding_registers(33161, ModbusDataType.UINT_32, unit=unit) * 1000 - # Entladen in kWh * 0,1 - exported = client.read_holding_registers(33165, ModbusDataType.UINT_32, unit=unit) * 100 + imported = client.read_input_registers(33161, ModbusDataType.UINT_32, unit=unit) * 1000 + # Entladen in kWh + exported = client.read_input_registers(33165, ModbusDataType.UINT_32, unit=unit) * 1000 bat_state = BatState( power=power, diff --git a/packages/modules/devices/solis/solis/counter.py b/packages/modules/devices/solis/solis/counter.py index 4da7613cc..346c5d4f0 100644 --- a/packages/modules/devices/solis/solis/counter.py +++ b/packages/modules/devices/solis/solis/counter.py @@ -19,15 +19,16 @@ def __init__(self, component_config: SolisCounterSetup, def update(self, client: ModbusTcpClient_): unit = self.component_config.configuration.modbus_id + + register_offset = 30000 if self.version == SolisVersion.inverter: register_offset = -1 - elif self.version == SolisVersion.hybrid: - register_offset = 30000 - power = client.read_holding_registers(3263 + register_offset, ModbusDataType.INT_32, unit=unit) - powers = client.read_holding_registers(3257 + register_offset, [ModbusDataType.INT_32]*3, unit=unit) - frequency = client.read_holding_registers(3282 + register_offset, ModbusDataType.UINT_16, unit=unit) / 100 - imported = client.read_holding_registers(3283 + register_offset, ModbusDataType.UINT_32, unit=unit) * 10 - exported = client.read_holding_registers(3285 + register_offset, ModbusDataType.UINT_32, unit=unit) * 10 + + power = client.read_input_registers(3263 + register_offset, ModbusDataType.INT_32, unit=unit) + powers = client.read_input_registers(3257 + register_offset, [ModbusDataType.INT_32]*3, unit=unit) + frequency = client.read_input_registers(3282 + register_offset, ModbusDataType.UINT_16, unit=unit) / 100 + imported = client.read_input_registers(3283 + register_offset, ModbusDataType.UINT_32, unit=unit) * 10 + exported = client.read_input_registers(3285 + register_offset, ModbusDataType.UINT_32, unit=unit) * 10 counter_state = CounterState( imported=imported, diff --git a/packages/modules/devices/solis/solis/inverter.py b/packages/modules/devices/solis/solis/inverter.py index 11a353fd8..b8fcb5588 100644 --- a/packages/modules/devices/solis/solis/inverter.py +++ b/packages/modules/devices/solis/solis/inverter.py @@ -23,13 +23,11 @@ def update(self, client: ModbusTcpClient_) -> None: unit = self.component_config.configuration.modbus_id if self.version == SolisVersion.inverter: - power = client.read_holding_registers(3004, ModbusDataType.UINT_32, unit=unit) * -1 - # Unit 1kWh - exported = client.read_holding_registers(3008, ModbusDataType.UINT_32, unit=unit) * 1000 + power = client.read_input_registers(3004, ModbusDataType.UINT_32, unit=unit) * -1 + exported = client.read_input_registers(3008, ModbusDataType.UINT_32, unit=unit) * 1000 elif self.version == SolisVersion.hybrid: - power = client.read_holding_registers(33057, ModbusDataType.UINT_32, unit=unit) * -1 - # Unit 1kWh - exported = client.read_holding_registers(33029, ModbusDataType.UINT_32, unit=unit) * 1000 + power = client.read_input_registers(33057, ModbusDataType.UINT_32, unit=unit) * -1 + exported = client.read_input_registers(33029, ModbusDataType.UINT_32, unit=unit) * 1000 inverter_state = InverterState( power=power,