Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion meshtastic/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions meshtastic/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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