Skip to content

Commit 87682c1

Browse files
authored
Merge pull request #834 from NillRudd/feat/cli-ignore-formats
CLI: normalize lora.ignore_incoming IDs (dec/!hex/0x), dedupe, YAML [] clear, fix bytes→int crash
2 parents e6c276f + 1d3a7d3 commit 87682c1

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed

meshtastic/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ def setPref(config, comp_name, raw_val) -> bool:
277277
else:
278278
print(f"Adding '{raw_val}' to the {pref.name} list")
279279
cur_vals = [x for x in getattr(config_values, pref.name) if x not in [0, "", b""]]
280-
cur_vals.append(val)
280+
if val not in cur_vals:
281+
cur_vals.append(val)
281282
getattr(config_values, pref.name)[:] = cur_vals
282283
return True
283284

meshtastic/node.py

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
pskToString,
1717
stripnl,
1818
message_to_json,
19+
to_node_num,
1920
)
2021

2122
logger = logging.getLogger(__name__)
@@ -714,11 +715,7 @@ def factoryReset(self, full: bool = False):
714715
def removeNode(self, nodeId: Union[int, str]):
715716
"""Tell the node to remove a specific node by ID"""
716717
self.ensureSessionKey()
717-
if isinstance(nodeId, str):
718-
if nodeId.startswith("!"):
719-
nodeId = int(nodeId[1:], 16)
720-
else:
721-
nodeId = int(nodeId)
718+
nodeId = to_node_num(nodeId)
722719

723720
p = admin_pb2.AdminMessage()
724721
p.remove_by_nodenum = nodeId
@@ -732,11 +729,7 @@ def removeNode(self, nodeId: Union[int, str]):
732729
def setFavorite(self, nodeId: Union[int, str]):
733730
"""Tell the node to set the specified node ID to be favorited on the NodeDB on the device"""
734731
self.ensureSessionKey()
735-
if isinstance(nodeId, str):
736-
if nodeId.startswith("!"):
737-
nodeId = int(nodeId[1:], 16)
738-
else:
739-
nodeId = int(nodeId)
732+
nodeId = to_node_num(nodeId)
740733

741734
p = admin_pb2.AdminMessage()
742735
p.set_favorite_node = nodeId
@@ -750,11 +743,7 @@ def setFavorite(self, nodeId: Union[int, str]):
750743
def removeFavorite(self, nodeId: Union[int, str]):
751744
"""Tell the node to set the specified node ID to be un-favorited on the NodeDB on the device"""
752745
self.ensureSessionKey()
753-
if isinstance(nodeId, str):
754-
if nodeId.startswith("!"):
755-
nodeId = int(nodeId[1:], 16)
756-
else:
757-
nodeId = int(nodeId)
746+
nodeId = to_node_num(nodeId)
758747

759748
p = admin_pb2.AdminMessage()
760749
p.remove_favorite_node = nodeId
@@ -768,11 +757,7 @@ def removeFavorite(self, nodeId: Union[int, str]):
768757
def setIgnored(self, nodeId: Union[int, str]):
769758
"""Tell the node to set the specified node ID to be ignored on the NodeDB on the device"""
770759
self.ensureSessionKey()
771-
if isinstance(nodeId, str):
772-
if nodeId.startswith("!"):
773-
nodeId = int(nodeId[1:], 16)
774-
else:
775-
nodeId = int(nodeId)
760+
nodeId = to_node_num(nodeId)
776761

777762
p = admin_pb2.AdminMessage()
778763
p.set_ignored_node = nodeId
@@ -786,11 +771,7 @@ def setIgnored(self, nodeId: Union[int, str]):
786771
def removeIgnored(self, nodeId: Union[int, str]):
787772
"""Tell the node to set the specified node ID to be un-ignored on the NodeDB on the device"""
788773
self.ensureSessionKey()
789-
if isinstance(nodeId, str):
790-
if nodeId.startswith("!"):
791-
nodeId = int(nodeId[1:], 16)
792-
else:
793-
nodeId = int(nodeId)
774+
nodeId = to_node_num(nodeId)
794775

795776
p = admin_pb2.AdminMessage()
796777
p.remove_ignored_node = nodeId
@@ -1013,10 +994,7 @@ def _sendAdmin(
1013994
): # unless a special channel index was used, we want to use the admin index
1014995
adminIndex = self.iface.localNode._getAdminChannelIndex()
1015996
logger.debug(f"adminIndex:{adminIndex}")
1016-
if isinstance(self.nodeNum, int):
1017-
nodeid = self.nodeNum
1018-
else: # assume string starting with !
1019-
nodeid = int(self.nodeNum[1:],16)
997+
nodeid = to_node_num(self.nodeNum)
1020998
if "adminSessionPassKey" in self.iface._getOrCreateByNum(nodeid):
1021999
p.session_passkey = self.iface._getOrCreateByNum(nodeid).get("adminSessionPassKey")
10221000
return self.iface.sendData(
@@ -1037,9 +1015,6 @@ def ensureSessionKey(self):
10371015
f"Not ensuring session key, because protocol use is disabled by noProto"
10381016
)
10391017
else:
1040-
if isinstance(self.nodeNum, int):
1041-
nodeid = self.nodeNum
1042-
else: # assume string starting with !
1043-
nodeid = int(self.nodeNum[1:],16)
1018+
nodeid = to_node_num(self.nodeNum)
10441019
if self.iface._getOrCreateByNum(nodeid).get("adminSessionPassKey") is None:
10451020
self.requestConfig(admin_pb2.AdminMessage.SESSIONKEY_CONFIG)

meshtastic/util.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,3 +692,20 @@ def message_to_json(message: Message, multiline: bool=False) -> str:
692692
except TypeError:
693693
json = MessageToJson(message, including_default_value_fields=True) # type: ignore[call-arg] # pylint: disable=E1123
694694
return stripnl(json) if not multiline else json
695+
696+
697+
def to_node_num(node_id: Union[int, str]) -> int:
698+
"""
699+
Normalize a node id from int | '!hex' | '0xhex' | 'decimal' to int.
700+
"""
701+
if isinstance(node_id, int):
702+
return node_id
703+
s = str(node_id).strip()
704+
if s.startswith("!"):
705+
s = s[1:]
706+
if s.lower().startswith("0x"):
707+
return int(s, 16)
708+
try:
709+
return int(s, 10)
710+
except ValueError:
711+
return int(s, 16)

0 commit comments

Comments
 (0)