@@ -52,6 +52,20 @@ def __repr__(self):
5252 r += ")"
5353 return r
5454
55+ def _to_node_num (self , nodeId : Union [int , str ]) -> int :
56+ """Normalize node id from int | '!hex' | '0xhex' | 'decimal' to int."""
57+ if isinstance (nodeId , int ):
58+ return nodeId
59+ s = str (nodeId ).strip ()
60+ if s .startswith ("!" ):
61+ s = s [1 :]
62+ if s .lower ().startswith ("0x" ):
63+ return int (s , 16 )
64+ try :
65+ return int (s , 10 )
66+ except ValueError :
67+ return int (s , 16 )
68+
5569 def module_available (self , excluded_bit : int ) -> bool :
5670 """Check DeviceMetadata.excluded_modules to see if a module is available."""
5771 meta = getattr (self .iface , "metadata" , None )
@@ -714,11 +728,7 @@ def factoryReset(self, full: bool = False):
714728 def removeNode (self , nodeId : Union [int , str ]):
715729 """Tell the node to remove a specific node by ID"""
716730 self .ensureSessionKey ()
717- if isinstance (nodeId , str ):
718- if nodeId .startswith ("!" ):
719- nodeId = int (nodeId [1 :], 16 )
720- else :
721- nodeId = int (nodeId )
731+ nodeId = self ._to_node_num (nodeId )
722732
723733 p = admin_pb2 .AdminMessage ()
724734 p .remove_by_nodenum = nodeId
@@ -732,11 +742,7 @@ def removeNode(self, nodeId: Union[int, str]):
732742 def setFavorite (self , nodeId : Union [int , str ]):
733743 """Tell the node to set the specified node ID to be favorited on the NodeDB on the device"""
734744 self .ensureSessionKey ()
735- if isinstance (nodeId , str ):
736- if nodeId .startswith ("!" ):
737- nodeId = int (nodeId [1 :], 16 )
738- else :
739- nodeId = int (nodeId )
745+ nodeId = self ._to_node_num (nodeId )
740746
741747 p = admin_pb2 .AdminMessage ()
742748 p .set_favorite_node = nodeId
@@ -750,11 +756,7 @@ def setFavorite(self, nodeId: Union[int, str]):
750756 def removeFavorite (self , nodeId : Union [int , str ]):
751757 """Tell the node to set the specified node ID to be un-favorited on the NodeDB on the device"""
752758 self .ensureSessionKey ()
753- if isinstance (nodeId , str ):
754- if nodeId .startswith ("!" ):
755- nodeId = int (nodeId [1 :], 16 )
756- else :
757- nodeId = int (nodeId )
759+ nodeId = self ._to_node_num (nodeId )
758760
759761 p = admin_pb2 .AdminMessage ()
760762 p .remove_favorite_node = nodeId
@@ -768,11 +770,7 @@ def removeFavorite(self, nodeId: Union[int, str]):
768770 def setIgnored (self , nodeId : Union [int , str ]):
769771 """Tell the node to set the specified node ID to be ignored on the NodeDB on the device"""
770772 self .ensureSessionKey ()
771- if isinstance (nodeId , str ):
772- if nodeId .startswith ("!" ):
773- nodeId = int (nodeId [1 :], 16 )
774- else :
775- nodeId = int (nodeId )
773+ nodeId = self ._to_node_num (nodeId )
776774
777775 p = admin_pb2 .AdminMessage ()
778776 p .set_ignored_node = nodeId
@@ -786,11 +784,7 @@ def setIgnored(self, nodeId: Union[int, str]):
786784 def removeIgnored (self , nodeId : Union [int , str ]):
787785 """Tell the node to set the specified node ID to be un-ignored on the NodeDB on the device"""
788786 self .ensureSessionKey ()
789- if isinstance (nodeId , str ):
790- if nodeId .startswith ("!" ):
791- nodeId = int (nodeId [1 :], 16 )
792- else :
793- nodeId = int (nodeId )
787+ nodeId = self ._to_node_num (nodeId )
794788
795789 p = admin_pb2 .AdminMessage ()
796790 p .remove_ignored_node = nodeId
@@ -1013,10 +1007,7 @@ def _sendAdmin(
10131007 ): # unless a special channel index was used, we want to use the admin index
10141008 adminIndex = self .iface .localNode ._getAdminChannelIndex ()
10151009 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 )
1010+ nodeid = self ._to_node_num (self .nodeNum )
10201011 if "adminSessionPassKey" in self .iface ._getOrCreateByNum (nodeid ):
10211012 p .session_passkey = self .iface ._getOrCreateByNum (nodeid ).get ("adminSessionPassKey" )
10221013 return self .iface .sendData (
@@ -1037,9 +1028,6 @@ def ensureSessionKey(self):
10371028 f"Not ensuring session key, because protocol use is disabled by noProto"
10381029 )
10391030 else :
1040- if isinstance (self .nodeNum , int ):
1041- nodeid = self .nodeNum
1042- else : # assume string starting with !
1043- nodeid = int (self .nodeNum [1 :],16 )
1031+ nodeid = self ._to_node_num (self .nodeNum )
10441032 if self .iface ._getOrCreateByNum (nodeid ).get ("adminSessionPassKey" ) is None :
10451033 self .requestConfig (admin_pb2 .AdminMessage .SESSIONKEY_CONFIG )
0 commit comments