Skip to content

Commit e363948

Browse files
committed
Keep protocol errors in the read ExceptionGroup
The ModbusProtocolError branch logged but never appended to errs, so a group where every attempt desynced raised ValueError: second argument (exceptions) must be a non-empty sequence instead of the Modbus failure. Reachable today on a noisy RS485 link, and more so now that the identity read shares this path.
1 parent 457df52 commit e363948

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎src/sunsynk/sunsynk.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ async def read_holding_registers(self, start: int, length: int) -> Sequence[int]
183183
self.connection._params, ModbusSerialParams
184184
):
185185
await self._flush_modbus_connection(reason=type(err).__name__)
186-
except ModbusProtocolError:
186+
except ModbusProtocolError as err:
187+
errs.append(err)
187188
_LOG.error(
188189
"Read register %s x%s: protocol error [attempt %s/%s]",
189190
start,

‎src/tests/sunsynk/test_sunsynk.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from unittest.mock import MagicMock, call, patch
77

88
import pytest
9+
from modbus_connection import ModbusProtocolError
910

1011
from sunsynk import Sunsynk
1112
from sunsynk.rwsensors import NumberRWSensor
@@ -177,3 +178,14 @@ def rhr_side_effect(start: int, length: int) -> Sequence[int]:
177178
assert state[single] == 5
178179
assert state[pair] == 7 << 16
179180
assert state.registers == {1: 5, 10: 0, 11: 7}
181+
182+
183+
async def test_ss_protocol_error_raises_exception_group() -> None:
184+
"""Protocol errors must reach the ExceptionGroup, not leave it empty."""
185+
unit = MagicMock()
186+
unit.read_holding_registers.side_effect = ModbusProtocolError("desync")
187+
ss = Sunsynk(unit=unit, read_attempts=2) # type: ignore[arg-type]
188+
189+
with pytest.raises(ExceptionGroup) as excinfo:
190+
await ss.read_holding_registers(622, 4)
191+
assert len(excinfo.value.exceptions) == 2

0 commit comments

Comments
 (0)