From aa5af53348516a7546b626d98bdca095b1ae4d8a Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Sun, 23 Jan 2022 11:17:41 -0800 Subject: [PATCH 1/2] add option to configure just the owner short name --- meshtastic/__main__.py | 16 +++++++++++++++- meshtastic/tests/test_main.py | 17 +++++++++++++++++ meshtastic/tests/test_node.py | 9 +++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 8c6efbfc..d6bfa6bf 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 52b3ad74..3bc722bb 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 8c13ee2e..97612744 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""" From 4f72987a29541d0b7da7a0ab56f4985ec5c4d36e Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Sun, 23 Jan 2022 11:22:05 -0800 Subject: [PATCH 2/2] add some config tests --- exampleConfig.yaml | 1 + example_config.yaml | 1 + meshtastic/__main__.py | 4 ++++ meshtastic/tests/test_main.py | 4 ++++ 4 files changed, 10 insertions(+) diff --git a/exampleConfig.yaml b/exampleConfig.yaml index c4539fa5..35590f36 100644 --- a/exampleConfig.yaml +++ b/exampleConfig.yaml @@ -1,5 +1,6 @@ # example config using camelCase keys owner: Bob TBeam +ownerShort: BOB channelUrl: https://www.meshtastic.org/d/#CgUYAyIBAQ diff --git a/example_config.yaml b/example_config.yaml index eff8c3f8..ac3d2633 100644 --- a/example_config.yaml +++ b/example_config.yaml @@ -1,5 +1,6 @@ # example configuration file with snake_case keys owner: Bob TBeam +owner_short: BOB channel_url: https://www.meshtastic.org/d/#CgUYAyIBAQ diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index d6bfa6bf..e851139a 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -341,6 +341,10 @@ def onConnected(interface): print(f"Setting device owner short to {configuration['owner_short']}") interface.getNode(args.dest).setOwner(long_name=None, short_owner=configuration['owner_short']) + if 'ownerShort' in configuration: + print(f"Setting device owner short to {configuration['ownerShort']}") + interface.getNode(args.dest).setOwner(long_name=None, short_owner=configuration['ownerShort']) + if 'channel_url' in configuration: print("Setting channel url to", configuration['channel_url']) interface.getNode(args.dest).setURL(configuration['channel_url']) diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index 3bc722bb..ce01da51 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -930,6 +930,7 @@ def test_main_configure_with_snake_case(capsys): out, err = capsys.readouterr() assert re.search(r'Connected to radio', out, re.MULTILINE) assert re.search(r'Setting device owner', out, re.MULTILINE) + assert re.search(r'Setting device owner short', out, re.MULTILINE) assert re.search(r'Setting channel url', out, re.MULTILINE) assert re.search(r'Fixing altitude', out, re.MULTILINE) assert re.search(r'Fixing latitude', out, re.MULTILINE) @@ -957,6 +958,7 @@ def test_main_configure_with_camel_case_keys(capsys): out, err = capsys.readouterr() assert re.search(r'Connected to radio', out, re.MULTILINE) assert re.search(r'Setting device owner', out, re.MULTILINE) + assert re.search(r'Setting device owner short', out, re.MULTILINE) assert re.search(r'Setting channel url', out, re.MULTILINE) assert re.search(r'Fixing altitude', out, re.MULTILINE) assert re.search(r'Fixing latitude', out, re.MULTILINE) @@ -1621,6 +1623,7 @@ def test_main_export_config(capsys): iface = MagicMock(autospec=SerialInterface) with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: mo.getLongName.return_value = 'foo' + mo.getShortName.return_value = 'oof' mo.localNode.getURL.return_value = 'bar' mo.getMyNodeInfo().get.return_value = { 'latitudeI': 1100000000, 'longitudeI': 1200000000, 'altitude': 100, 'batteryLevel': 34, 'latitude': 110.0, @@ -1637,6 +1640,7 @@ def test_main_export_config(capsys): assert not re.search(r'Connected to radio', out, re.MULTILINE) assert re.search(r'owner: foo', out, re.MULTILINE) + assert re.search(r'owner_short: oof', out, re.MULTILINE) assert re.search(r'channel_url: bar', out, re.MULTILINE) assert re.search(r'location:', out, re.MULTILINE) assert re.search(r'lat: 110.0', out, re.MULTILINE)