diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 8c6efbf..d6bfa6b 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -195,6 +195,11 @@ def onConnected(interface): print(f"Setting device owner to {args.set_owner}") interface.getNode(args.dest).setOwner(args.set_owner) + if args.set_owner_short: + closeNow = True + print(f"Setting device owner short to {args.set_owner_short}") + interface.getNode(args.dest).setOwner(long_name=None, short_name=args.set_owner_short) + if args.pos_fields: # If --pos-fields invoked with args, set position fields closeNow = True @@ -332,6 +337,10 @@ def onConnected(interface): print(f"Setting device owner to {configuration['owner']}") interface.getNode(args.dest).setOwner(configuration['owner']) + if 'owner_short' in configuration: + print(f"Setting device owner short to {configuration['owner_short']}") + interface.getNode(args.dest).setOwner(long_name=None, short_owner=configuration['owner_short']) + if 'channel_url' in configuration: print("Setting channel url to", configuration['channel_url']) interface.getNode(args.dest).setURL(configuration['channel_url']) @@ -569,6 +578,7 @@ def subscribe(): def export_config(interface): """used in--export-config""" owner = interface.getLongName() + owner_short = interface.getShortName() channel_url = interface.localNode.getURL() myinfo = interface.getMyNodeInfo() pos = myinfo.get('position') @@ -583,6 +593,8 @@ def export_config(interface): config = "# start of Meshtastic configure yaml\n" if owner: config += f"owner: {owner}\n\n" + if owner_short: + config += f"owner_short: {owner_short}\n\n" if channel_url: if Globals.getInstance().get_camel_case(): config += f"channelUrl: {channel_url}\n\n" @@ -785,10 +797,12 @@ def initParser(): parser.add_argument( "--ch-shortfast", help="Change to the short-range and fast channel", action='store_true') - parser.add_argument( "--set-owner", help="Set device owner name", action="store") + parser.add_argument( + "--set-owner-short", help="Set device owner short name", action="store") + parser.add_argument( "--set-team", help="Set team affiliation (an invalid team will list valid values)", action="store") diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index 52b3ad7..3bc722b 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -425,6 +425,23 @@ def test_main_set_owner_to_bob(capsys): mo.assert_called() +@pytest.mark.unit +@pytest.mark.usefixtures("reset_globals") +def test_main_set_owner_short_to_bob(capsys): + """Test --set-owner-short bob""" + sys.argv = ['', '--set-owner-short', 'bob'] + Globals.getInstance().set_args(sys.argv) + + iface = MagicMock(autospec=SerialInterface) + with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: + main() + out, err = capsys.readouterr() + assert re.search(r'Connected to radio', out, re.MULTILINE) + assert re.search(r'Setting device owner short to bob', out, re.MULTILINE) + assert err == '' + mo.assert_called() + + @pytest.mark.unit @pytest.mark.usefixtures("reset_globals") def test_main_set_ham_to_KI123(capsys): diff --git a/meshtastic/tests/test_node.py b/meshtastic/tests/test_node.py index 8c13ee2..9761274 100644 --- a/meshtastic/tests/test_node.py +++ b/meshtastic/tests/test_node.py @@ -55,6 +55,15 @@ def test_setOwner_and_team(caplog): assert re.search(r'p.set_owner.team:1', caplog.text, re.MULTILINE) +@pytest.mark.unit +def test_setOwnerShort(caplog): + """Test setOwner""" + anode = Node('foo', 'bar', noProto=True) + with caplog.at_level(logging.DEBUG): + anode.setOwner(long_name=None, short_name='123') + assert re.search(r'p.set_owner.short_name:123:', caplog.text, re.MULTILINE) + + @pytest.mark.unit def test_setOwner_no_short_name(caplog): """Test setOwner"""