From eef0b5b5ddbf252b0b3ff140f0bdfc75a4124422 Mon Sep 17 00:00:00 2001 From: the8thbit Date: Fri, 23 Oct 2026 14:12:56 -0500 Subject: [PATCH] Report the connected radio transport and endpoint --- meshtastic/__main__.py | 13 ++++++++++- meshtastic/tests/test_main.py | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index ca1680a58..2e102858c 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -47,6 +47,8 @@ from meshtastic import BROADCAST_ADDR, mt_config, remote_hardware from meshtastic.ble_interface import BLEInterface from meshtastic.mesh_interface import MeshInterface +from meshtastic.serial_interface import SerialInterface +from meshtastic.tcp_interface import TCPInterface try: from meshtastic.powermon import ( PowerMeter, @@ -336,7 +338,16 @@ def onConnected(interface): # do not print this line if we are exporting the config if not args.export_config: - print("Connected to radio") + connection = "Connected to radio" + if isinstance(interface, SerialInterface): + connection += f" via serial {interface.devPath}" + elif isinstance(interface, TCPInterface): + connection += f" via TCP {interface.hostname}:{interface.portNumber}" + elif isinstance(interface, BLEInterface): + connection += " via BLE" + if interface.client is not None: + connection += f" {interface.client.address}" + print(connection) if args.set_time is not None: interface.getNode(args.dest, False, **getNode_kwargs).setTime(args.set_time) diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index e0d65f60f..f630f7f74 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -3839,3 +3839,47 @@ def test_main_setPref_bitfield_invalid_name(capsys): assert "Unknown flag 'TCP'" in out assert "NO_BROADCAST" in out assert "UDP_BROADCAST" in out + + +@pytest.mark.unit +@pytest.mark.usefixtures("reset_mt_config") +@pytest.mark.parametrize( + "interface_type,attributes,expected", + [ + (SerialInterface, {"devPath": "/dev/ttyUSB0"}, + "Connected to radio via serial /dev/ttyUSB0"), + (TCPInterface, {"hostname": "meshtastic.local", "portNumber": 4403}, + "Connected to radio via TCP meshtastic.local:4403"), + (TCPInterface, {"hostname": "localhost", "portNumber": 4404}, + "Connected to radio via TCP localhost:4404"), + (mt_main.BLEInterface, {"client": SimpleNamespace(address="AA:BB:CC:DD:EE:FF")}, + "Connected to radio via BLE AA:BB:CC:DD:EE:FF"), + (mt_main.BLEInterface, {"client": None}, "Connected to radio via BLE"), + (MeshInterface, {}, "Connected to radio"), + ], +) +def test_on_connected_transport(interface_type, attributes, expected, monkeypatch, capsys): + """Report the connected interface, including discovered or fallback endpoints.""" + monkeypatch.setattr(sys, "argv", [""]) + initParser() + iface = MagicMock(spec=interface_type) + for name, value in attributes.items(): + setattr(iface, name, value) + + mt_main.onConnected(iface) + + assert capsys.readouterr().out == expected + "\n" + + +@pytest.mark.unit +@pytest.mark.usefixtures("reset_mt_config") +def test_on_connected_transport_export_config(monkeypatch, capsys): + """Keep connection status out of exported configuration.""" + monkeypatch.setattr(sys, "argv", ["", "--export-config"]) + initParser() + iface = MagicMock(spec=TCPInterface) + iface.hostname = "meshtastic.local" + iface.portNumber = 4403 + with patch("meshtastic.__main__.export_config", return_value="config: exported"): + mt_main.onConnected(iface) + assert "Connected to radio" not in capsys.readouterr().out