diff --git a/minimateplus/client.py b/minimateplus/client.py index 599ae95..e5002bf 100644 --- a/minimateplus/client.py +++ b/minimateplus/client.py @@ -1305,12 +1305,15 @@ def _decode_serial_number(data: bytes) -> DeviceInfo: # Actual serial payload starts at data[11]. actual = data[11:] if len(data) > 11 else data + # The serial is a C string: cut at the first NUL. A short serial leaves a + # stray byte after its terminator (BE9441 sends b"BE9441\x00?"), so + # stripping trailing NULs alone is not enough. if len(actual) < 9: # Short payload — gracefully degrade - serial = actual.rstrip(b"\x00").decode("ascii", errors="replace") + serial = actual.split(b"\x00", 1)[0].decode("ascii", errors="replace") return DeviceInfo(serial=serial, firmware_minor=0) - serial = actual[:8].rstrip(b"\x00").decode("ascii", errors="replace") + serial = actual[:8].split(b"\x00", 1)[0].decode("ascii", errors="replace") trail_0 = actual[8] if len(actual) > 8 else None fw_minor = actual[9] if len(actual) > 9 else 0 diff --git a/tests/test_minimate_serial_decode.py b/tests/test_minimate_serial_decode.py new file mode 100644 index 0000000..ac7c01d --- /dev/null +++ b/tests/test_minimate_serial_decode.py @@ -0,0 +1,30 @@ +"""SUB 0x15's serial is a C string in an 8-byte field: cut at the first NUL. + +BE9441 (bench, 2026-10-25) answers `BE9441\\x00?` -- six characters, the +terminator, then a leftover byte. Stripping only trailing NULs kept the `?`, so +the serial read as "BE9441\\x00?" and every serial-named request was refused as +the wrong unit. Seven-character serials (BE11529, BE18189) fill the field up to +the terminator, which is why this never showed before. +""" +from __future__ import annotations + +from minimateplus.client import _decode_serial_number + +_HEADER = bytes(11) # data[0:11]: the section header + + +def _reply(field8: bytes, trail: int = 0x00, fw_minor: int = 0x11) -> bytes: + return _HEADER + field8 + bytes([trail, fw_minor]) + + +def test_a_six_character_serial_stops_at_its_terminator(): + assert _decode_serial_number(_reply(b"BE9441\x00?")).serial == "BE9441" + + +def test_a_seven_character_serial_is_unchanged(): + info = _decode_serial_number(_reply(b"BE18189\x00", trail=0x2A, fw_minor=0x11)) + assert (info.serial, info.firmware_minor) == ("BE18189", 0x11) + + +def test_a_short_payload_also_stops_at_the_terminator(): + assert _decode_serial_number(_HEADER + b"BE9441\x00?").serial == "BE9441"