@@ -75,10 +75,10 @@ def unsubscribe(self, can_id, callback=None) -> None:
7575 If given, remove only this callback. Otherwise all callbacks for
7676 the CAN ID.
7777 """
78- if callback is None :
79- del self .subscribers [can_id ]
80- else :
78+ if callback is not None :
8179 self .subscribers [can_id ].remove (callback )
80+ if not self .subscribers [can_id ] or callback is None :
81+ del self .subscribers [can_id ]
8282
8383 def connect (self , * args , ** kwargs ) -> Network :
8484 """Connect to CAN bus using python-can.
@@ -392,7 +392,9 @@ class NodeScanner:
392392 SERVICES = (0x700 , 0x580 , 0x180 , 0x280 , 0x380 , 0x480 , 0x80 )
393393
394394 def __init__ (self , network : Optional [Network ] = None ):
395- self .network = network
395+ if network is None :
396+ network = _UNINITIALIZED_NETWORK
397+ self .network : Network = network
396398 #: A :class:`list` of nodes discovered
397399 self .nodes : List [int ] = []
398400
@@ -408,8 +410,6 @@ def reset(self):
408410
409411 def search (self , limit : int = 127 ) -> None :
410412 """Search for nodes by sending SDO requests to all node IDs."""
411- if self .network is None :
412- raise RuntimeError ("A Network is required to do active scanning" )
413413 sdo_req = b"\x40 \x00 \x10 \x00 \x00 \x00 \x00 \x00 "
414414 for node_id in range (1 , limit + 1 ):
415415 self .network .send_message (0x600 + node_id , sdo_req )
0 commit comments