diff --git a/meshtastic/remote_hardware.py b/meshtastic/remote_hardware.py index 41534a02a..8e9c66cb9 100644 --- a/meshtastic/remote_hardware.py +++ b/meshtastic/remote_hardware.py @@ -23,10 +23,13 @@ def onGPIOreceive(packet, interface) -> None: # so, we set it here gpioValue = 0 - # print(f'mask:{interface.mask}') - value = int(gpioValue) & int(interface.mask) + # Keep read replies consistent with the hexadecimal mask/value notation used + # when the CLI writes GPIOs. + mask = int(interface.mask) + value = int(gpioValue) & mask print( - f'Received RemoteHardware type={hw["type"]}, gpio_value={gpioValue} value={value}' + f'Received Remote_Hardware type={hw["type"]}, ' + f"mask=0x{mask:x} value=0x{value:x}" ) interface.gotResponse = True diff --git a/meshtastic/tests/test_remote_hardware.py b/meshtastic/tests/test_remote_hardware.py index 7d4cf6f1a..9fd43bfe4 100644 --- a/meshtastic/tests/test_remote_hardware.py +++ b/meshtastic/tests/test_remote_hardware.py @@ -20,14 +20,35 @@ def test_RemoteHardwareClient(): @pytest.mark.unit -def test_onGPIOreceive(capsys): - """Test onGPIOreceive""" +@pytest.mark.parametrize( + ("gpio_value", "mask", "expected_value"), + [ + ("8192", 0x2000, "0x2000"), + ("12288", 0x2000, "0x2000"), + (None, 0x10, "0x0"), + ], +) +def test_onGPIOreceive_formats_masked_values_as_hex( + capsys: pytest.CaptureFixture[str], + gpio_value: str | None, + mask: int, + expected_value: str, +) -> None: + """GPIO replies use the same hexadecimal notation as write requests.""" iface = MagicMock(autospec=SerialInterface) - packet = {"decoded": {"remotehw": {"type": "foo", "gpioValue": "4096"}}} + iface.mask = mask + remotehw = {"type": "READ_GPIOS_REPLY"} + if gpio_value is not None: + remotehw["gpioValue"] = gpio_value + packet = {"decoded": {"remotehw": remotehw}} onGPIOreceive(packet, iface) out, err = capsys.readouterr() - assert re.search(r"Received RemoteHardware", out) + assert out == ( + "Received Remote_Hardware type=READ_GPIOS_REPLY, " + f"mask=0x{mask:x} value={expected_value}\n" + ) assert err == "" + assert iface.gotResponse is True @pytest.mark.unit