Skip to content

Comments

update write register method#3155

Merged
LKuemmel merged 2 commits intoopenWB:masterfrom
LKuemmel:fixes
Feb 23, 2026
Merged

update write register method#3155
LKuemmel merged 2 commits intoopenWB:masterfrom
LKuemmel:fixes

Conversation

@LKuemmel
Copy link
Contributor

@LKuemmel LKuemmel commented Feb 23, 2026

Änderungen zu #3125 nachführen

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates battery modules and documentation samples to use a write_register API instead of write_registers when writing Modbus registers.

Changes:

  • Switch Solarmax battery control from __tcp_client.write_registers(...) to client.write_register(...).
  • Switch Sigenergy battery control from __tcp_client.write_registers(...) to client.write_register(...).
  • Update two documentation samples to use write_register(...) instead of write_registers(...).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.

File Description
packages/modules/devices/solarmax/solarmax/bat.py Replaces multiple write_registers calls with write_register for battery control registers.
packages/modules/devices/sigenergy/sigenergy/bat.py Replaces write_registers calls with write_register for SoC-based control register.
docs/samples/sample_request_by_device/sample_request_by_device/bat.py Updates sample code to call write_register instead of write_registers.
docs/samples/sample_request_by_component/sample_request_by_component/bat.py Updates sample code to call write_register instead of write_registers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if self.last_mode != 'stop':
# Entladesperre auch bei 100% SoC
self.__tcp_client.write_registers(40048, [1000], data_type=ModbusDataType.UINT_16, unit=unit)
self.client.write_register(40048, [1000], data_type=ModbusDataType.UINT_16, unit=unit)
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write_register() expects a scalar value; [1000] should be 1000 to avoid nested list / encoding issues with data_type=UINT_16.

Copilot uses AI. Check for mistakes.
@@ -42,7 +42,7 @@ def set_power_limit(self, power_limit: Optional[int]) -> None:
# Wenn der Speicher die Steuerung der Ladeleistung unterstützt, muss bei Übergabe einer Zahl auf aktive
# Speichersteurung umgeschaltet werden, sodass der Speicher mit der übergebenen Leistung lädt/entlädt. Wird
# None übergeben, muss der Speicher die Null-Punkt-Ausregelung selbst übernehmen.
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sample now calls write_register(reg, power_limit) but write_register expects a numeric value; power_limit is Optional[int] and may be None. Adjust the sample to either guard against None (and document the behavior) or write an explicit numeric fallback value.

Suggested change
# None übergeben, muss der Speicher die Null-Punkt-Ausregelung selbst übernehmen.
# None übergeben, muss der Speicher die Null-Punkt-Ausregelung selbst übernehmen.
if power_limit is None:
# Keine Vorgabe an den Speicher senden; dieser übernimmt die Null-Punkt-Ausregelung selbst.
return

Copilot uses AI. Check for mistakes.
@@ -45,7 +45,7 @@ def set_power_limit(self, power_limit: Optional[int]) -> None:
# Wenn der Speicher die Steuerung der Ladeleistung unterstützt, muss bei Übergabe einer Zahl auf aktive
# Speichersteurung umgeschaltet werden, sodass der Speicher mit der übergebenen Leistung lädt/entlädt. Wird
# None übergeben, muss der Speicher die Null-Punkt-Ausregelung selbst übernehmen.
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: write_register requires a numeric value, but power_limit is Optional[int]. Update the sample to handle the None case explicitly so the example doesn’t raise at runtime.

Suggested change
# None übergeben, muss der Speicher die Null-Punkt-Ausregelung selbst übernehmen.
# None übergeben, muss der Speicher die Null-Punkt-Ausregelung selbst übernehmen.
if power_limit is None:
# In diesem Fall übernimmt der Speicher selbst die Null-Punkt-Ausregelung, daher kein Register schreiben.
return

Copilot uses AI. Check for mistakes.
log.debug("Keine Batteriesteuerung, Selbstregelung durch Wechselrichter")
if self.last_mode is not None:
self.__tcp_client.write_registers(142, [0], data_type=ModbusDataType.INT_16, unit=unit)
self.client.write_register(142, [0], data_type=ModbusDataType.INT_16, unit=unit)
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ModbusTcpClient_.write_register() expects a scalar int|float value. Passing a list here will result in a nested list being written (or a type error during encoding when data_type is used). Pass 0 instead of [0].

Copilot uses AI. Check for mistakes.
Comment on lines 60 to 62
self.client.write_register(140, [0], data_type=ModbusDataType.INT_16, unit=unit)
self.client.write_register(141, [0], data_type=ModbusDataType.INT_16, unit=unit)
self.client.write_register(142, [1], data_type=ModbusDataType.INT_16, unit=unit)
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write_register() takes a single numeric value; these calls currently pass lists ([0]/[1]). This will break encoding / produce an invalid Modbus payload. Use scalar values (e.g. 0, 1) instead.

Copilot uses AI. Check for mistakes.
elif power_limit < 0:
log.debug(f"Aktive Batteriesteuerung. Batterie wird mit {power_limit} W entladen für den Hausverbrauch")
self.__tcp_client.write_registers(142, [1], data_type=ModbusDataType.INT_16, unit=unit)
self.client.write_register(142, [1], data_type=ModbusDataType.INT_16, unit=unit)
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue: write_register() expects a scalar but [1] is passed. Use 1 (and keep data_type=INT_16) so the wrapper writes [1] at the Modbus layer.

Copilot uses AI. Check for mistakes.
Comment on lines 71 to 72
self.client.write_register(140, [power_value], data_type=ModbusDataType.INT_16, unit=unit)
self.client.write_register(141, [power_value], data_type=ModbusDataType.INT_16, unit=unit)
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write_register() expects a scalar power_value, not [power_value]. Passing a list here leads to incorrect register encoding/writes.

Copilot uses AI. Check for mistakes.
if self.last_mode is not None:
# Entladesperre ab 5%, Ansonsten Eigenregelung
self.__tcp_client.write_registers(40048, [50], data_type=ModbusDataType.UINT_16, unit=unit)
self.client.write_register(40048, [50], data_type=ModbusDataType.UINT_16, unit=unit)
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ModbusTcpClient_.write_register() expects a scalar value. Passing [50] will break encoding / send the wrong payload. Use 50 instead.

Copilot uses AI. Check for mistakes.
@LKuemmel LKuemmel merged commit c0422ac into openWB:master Feb 23, 2026
1 check passed
@LKuemmel LKuemmel deleted the fixes branch February 23, 2026 14:28
@LKuemmel LKuemmel changed the title update write register methode update write register method Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant