Skip to content

Commit 373b8a3

Browse files
committed
combine with set_owner handling
1 parent 51b543f commit 373b8a3

File tree

3 files changed

+24
-43
lines changed

3 files changed

+24
-43
lines changed

meshtastic/__main__.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -339,36 +339,31 @@ def onConnected(interface):
339339
# can include lat/long/alt etc: latitude = 37.5, longitude = -122.1
340340
interface.getNode(args.dest, False, **getNode_kwargs).setFixedPosition(lat, lon, alt)
341341

342-
if args.set_owner or args.set_owner_short:
342+
if args.set_owner or args.set_owner_short or args.set_is_unmessageable or args.set_is_unmessagable:
343343
closeNow = True
344344
waitForAckNak = True
345345
if args.set_owner and args.set_owner_short:
346346
print(f"Setting device owner to {args.set_owner} and short name to {args.set_owner_short}")
347347
elif args.set_owner:
348348
print(f"Setting device owner to {args.set_owner}")
349-
else: # short name only
349+
elif args.set_owner_short and not args.set_owner:
350350
print(f"Setting device owner short to {args.set_owner_short}")
351-
interface.getNode(args.dest, False, **getNode_kwargs).setOwner(long_name=args.set_owner, short_name=args.set_owner_short)
352-
353-
if args.set_is_unmessageable:
354-
closeNow = True
355-
waitForAckNak = True
356-
print(f"Setting is_unmessagable to {args.set_is_unmessageable}")
357-
if isinstance(args.set_is_unmessageable, str):
358-
val = meshtastic.util.fromStr(args.set_is_unmessageable)
359-
else:
360-
val = args.set_is_unmessageable
361-
interface.getNode(args.dest, **getNode_kwargs).setIsUnmessageable(is_unmessagable=val)
362-
363-
if args.set_is_unmessagable:
364-
closeNow = True
365-
waitForAckNak = True
366-
print(f"Setting is_unmessagable to {args.set_is_unmessagable}")
367-
if isinstance(args.set_is_unmessagable, str):
368-
val = meshtastic.util.fromStr(args.set_is_unmessagable)
369-
else:
370-
val = args.set_is_unmessagable
371-
interface.getNode(args.dest, **getNode_kwargs).setIsUnmessageable(is_unmessagable=val)
351+
unmessageable = (
352+
args.set_is_unmessageable
353+
if args.set_is_unmessageable is not None
354+
else args.set_is_unmessagable
355+
)
356+
set_is_unmessagable = (
357+
meshtastic.util.fromStr(unmessageable)
358+
if isinstance(unmessageable, str)
359+
else unmessageable
360+
)
361+
if set_is_unmessagable is not None:
362+
print(f"Setting device owner is_unmessageable to {set_is_unmessagable}")
363+
interface.getNode(
364+
args.dest, False, **getNode_kwargs).setOwner(long_name=args.set_owner,
365+
short_name=args.set_owner_short, is_unmessagable=set_is_unmessagable
366+
)
372367

373368
# TODO: add to export-config and configure
374369
if args.set_canned_message:

meshtastic/node.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def _getAdminChannelIndex(self):
298298
return c.index
299299
return 0
300300

301-
def setOwner(self, long_name: Optional[str]=None, short_name: Optional[str]=None, is_licensed: bool=False):
301+
def setOwner(self, long_name: Optional[str]=None, short_name: Optional[str]=None, is_licensed: bool=False, is_unmessagable: Optional[bool]=None):
302302
"""Set device owner name"""
303303
logging.debug(f"in setOwner nodeNum:{self.nodeNum}")
304304
self.ensureSessionKey()
@@ -315,27 +315,13 @@ def setOwner(self, long_name: Optional[str]=None, short_name: Optional[str]=None
315315
short_name = short_name[:nChars]
316316
print(f"Maximum is 4 characters, truncated to {short_name}")
317317
p.set_owner.short_name = short_name
318-
318+
if is_unmessagable is not None:
319+
p.set_owner.is_unmessagable = is_unmessagable
320+
319321
# Note: These debug lines are used in unit tests
320322
logging.debug(f"p.set_owner.long_name:{p.set_owner.long_name}:")
321323
logging.debug(f"p.set_owner.short_name:{p.set_owner.short_name}:")
322324
logging.debug(f"p.set_owner.is_licensed:{p.set_owner.is_licensed}")
323-
# If sending to a remote node, wait for ACK/NAK
324-
if self == self.iface.localNode:
325-
onResponse = None
326-
else:
327-
onResponse = self.onAckNak
328-
return self._sendAdmin(p, onResponse=onResponse)
329-
330-
def setIsUnmessageable(self, is_unmessagable: Optional[bool]=False):
331-
"""Set if a device is messagable or not"""
332-
self.ensureSessionKey()
333-
p = admin_pb2.AdminMessage()
334-
335-
if is_unmessagable is not None:
336-
p.set_owner.is_unmessagable = is_unmessagable
337-
338-
# Note: These debug lines are used in unit tests
339325
logging.debug(f"p.set_owner.is_unmessagable:{p.set_owner.is_unmessagable}:")
340326
# If sending to a remote node, wait for ACK/NAK
341327
if self == self.iface.localNode:

meshtastic/tests/test_main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ def test_main_set_is_unmessageable_to_true(capsys):
466466
main()
467467
out, err = capsys.readouterr()
468468
assert re.search(r"Connected to radio", out, re.MULTILINE)
469-
assert re.search(r"Setting is_unmessagable to true", out, re.MULTILINE)
469+
assert re.search(r"Setting device owner is_unmessageable to True", out, re.MULTILINE)
470470
assert err == ""
471471
mo.assert_called()
472472

@@ -482,7 +482,7 @@ def test_main_set_is_unmessagable_to_true(capsys):
482482
main()
483483
out, err = capsys.readouterr()
484484
assert re.search(r"Connected to radio", out, re.MULTILINE)
485-
assert re.search(r"Setting is_unmessagable to true", out, re.MULTILINE)
485+
assert re.search(r"Setting device owner is_unmessageable to True", out, re.MULTILINE)
486486
assert err == ""
487487
mo.assert_called()
488488

0 commit comments

Comments
 (0)