From 7ff527df0700d646ab4e011f7c4c1d8b06b38b15 Mon Sep 17 00:00:00 2001 From: serversdown Date: Fri, 23 Oct 2026 16:20:12 -0400 Subject: [PATCH] feat(micromate): read the unit's run state and refresh its serial on demand Co-Authored-By: Claude Haiku 5.5 Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44 --- micromate/client.py | 27 ++++++++++++++++++++++--- micromate/models.py | 8 ++++++++ tests/test_micromate_client.py | 37 ++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/micromate/client.py b/micromate/client.py index 5b8cdf0..5b6a17e 100644 --- a/micromate/client.py +++ b/micromate/client.py @@ -40,7 +40,13 @@ from typing import Optional from minimateplus.transport import BaseTransport -from .models import MicromateDeviceInfo, MicromateEventRef, MicromateSetup, MicromateState +from .models import ( + MicromateDeviceInfo, + MicromateEventRef, + MicromateRunState, + MicromateSetup, + MicromateState, +) from .protocol import MicromateProtocol, ProtocolError from .setup_block import decode_setup @@ -365,16 +371,31 @@ class MicromateClient: # ── Events ──────────────────────────────────────────────────────────────── - def serial(self) -> str: + def serial(self, *, refresh: bool = False) -> str: """The unit's serial, cached from `connect()` or read on demand. Needed by anything that handles an event, because an event key is ambiguous without it — see `MicromateEventRef`. + + `refresh=True` forces a fresh `0x15` read. A change operation passes it + right before writing, so the identity check never runs against a serial + cached from an earlier call-up (a modem may have moved to another unit). """ - if self._serial is None: + if refresh or self._serial is None: self._serial = _cstring(_content(self._proto.read_serial())) return self._serial + _RUN_STATES = {0x00: "idle", 0x02: "monitoring", 0x03: "calling_home"} + + def read_run_state(self) -> MicromateRunState: + """`SUB 0x49` -> the unit's run state, with the in-call-home value kept + (connect()'s `monitoring` bool flattens 0x03 into True).""" + c = _content(self._proto.read_state()) + if not c: + raise ProtocolError("0x49 state reply has no content") + code = c[_STATE_FLAG] + return MicromateRunState(code, self._RUN_STATES.get(code, f"unknown(0x{code:02X})")) + def list_events(self, *, with_records: bool = True) -> list[MicromateEventRef]: """Walk the event chain. `0x93 → 1E`, then `0x93 → 1F` until the sentinel. diff --git a/micromate/models.py b/micromate/models.py index 68054c7..48bcba6 100644 --- a/micromate/models.py +++ b/micromate/models.py @@ -155,6 +155,14 @@ class IdfReport: ) +@dataclass(frozen=True) +class MicromateRunState: + """`SUB 0x49` content[0]. 0x00 idle, 0x02 monitoring, 0x03 calling home + (ten of ten in the 10-14 ACH session); anything else is named, not guessed.""" + code: int + name: str + + # ── IdfPeaks / IdfProjectInfo / IdfSensorCheck (narrow grouping types) ─────── diff --git a/tests/test_micromate_client.py b/tests/test_micromate_client.py index 22f0ce0..8efea13 100644 --- a/tests/test_micromate_client.py +++ b/tests/test_micromate_client.py @@ -574,3 +574,40 @@ def test_read_unsolicited_sends_nothing_and_returns_what_arrived(): mm, t = client([b"\r\nRADIO RING\r\n"]) assert mm.read_unsolicited() == b"\r\nRADIO RING\r\n" assert t.written == [] + +# ── run state (0x49) and a fresh serial ─────────────────────────────────────── + +import json as _json # noqa: E402 +from pathlib import Path # noqa: E402 + +_WRITES = Path(__file__).parent / "fixtures" / "micromate-writes" + + +def _captured_0x49(): + ex = _json.loads((_WRITES / "monitor-start-stop.json").read_text())["exchanges"] + return [bytes.fromhex(e["response_data"]) for e in ex if e["sub"] == 0x49] + + +def test_run_state_names_thors_captured_states(): + datas = _captured_0x49() + names = [] + for d in datas: + mm, _t = client([frame(0xB6, d)]) + names.append(mm.read_run_state().name) + assert "idle" in names and "monitoring" in names + + +@pytest.mark.parametrize("code,name", [(0x00, "idle"), (0x02, "monitoring"), + (0x03, "calling_home"), (0x07, "unknown(0x07)")]) +def test_run_state_names_every_value(code, name): + data = bytes(11) + bytes([code]) + bytes(4) + mm, _t = client([frame(0xB6, data)]) + st = mm.read_run_state() + assert (st.code, st.name) == (code, name) + + +def test_serial_refresh_reads_the_unit_again(): + mm, t = client([frame(0xEA, bytes(11) + b"UM20147\x00\x00\x00")]) + mm._serial = "UM99999" # a stale cached serial + assert mm.serial(refresh=True) == "UM20147" + assert len(t.written) == 1