@@ -50,12 +50,12 @@ def __init__(self) -> None:
5050 def register (self , bus : BusABC , notifier : "Notifier" ) -> None :
5151 """Register a bus and its associated notifier.
5252
53- Ensures that a bus is not added to multiple active Notifier instances.
53+ Ensures that a bus is not added to multiple active :class:`~can. Notifier` instances.
5454
5555 :param bus:
5656 The CAN bus to register.
5757 :param notifier:
58- The Notifier instance associated with the bus.
58+ The :class:`~can. Notifier` instance associated with the bus.
5959 :raises ValueError:
6060 If the bus is already assigned to an active Notifier.
6161 """
@@ -75,7 +75,7 @@ def unregister(self, bus: BusABC, notifier: "Notifier") -> None:
7575 :param bus:
7676 The CAN bus to unregister.
7777 :param notifier:
78- The Notifier instance associated with the bus.
78+ The :class:`~can. Notifier` instance associated with the bus.
7979 """
8080 with self .lock :
8181 registered_pairs_to_remove : List [_BusNotifierPair ] = []
@@ -85,6 +85,26 @@ def unregister(self, bus: BusABC, notifier: "Notifier") -> None:
8585 for pair in registered_pairs_to_remove :
8686 self .pairs .remove (pair )
8787
88+ def find_instance (self , bus : BusABC ) -> Optional ["Notifier" ]:
89+ """Find the :class:`~can.Notifier` instance associated with a given CAN bus.
90+
91+ This method searches the registry for the :class:`~can.Notifier`
92+ that is linked to the specified bus. If the bus is found, the
93+ corresponding :class:`~can.Notifier` instance is returned. If the bus is not
94+ found in the registry, `None` is returned.
95+
96+ :param bus:
97+ The CAN bus for which to find the associated :class:`~can.Notifier` .
98+ :return:
99+ The :class:`~can.Notifier` instance associated with the given bus,
100+ or `None` if no such association exists.
101+ """
102+ with self .lock :
103+ for pair in self .pairs :
104+ if bus is pair .bus :
105+ return pair .notifier
106+ return None
107+
88108
89109class Notifier (AbstractContextManager ):
90110
@@ -274,6 +294,23 @@ def stopped(self) -> bool:
274294 """Return ``True``, if Notifier was properly shut down with :meth:`~can.Notifier.stop`."""
275295 return self ._stopped
276296
297+ @classmethod
298+ def find_instance (cls , bus : BusABC ) -> Optional ["Notifier" ]:
299+ """Find the :class:`~can.Notifier` instance associated with a given CAN bus.
300+
301+ This method searches the registry for the :class:`~can.Notifier`
302+ that is linked to the specified bus. If the bus is found, the
303+ corresponding :class:`~can.Notifier` instance is returned. If the bus is not
304+ found in the registry, `None` is returned.
305+
306+ :param bus:
307+ The CAN bus for which to find the associated :class:`~can.Notifier` .
308+ :return:
309+ The :class:`~can.Notifier` instance associated with the given bus,
310+ or `None` if no such association exists.
311+ """
312+ return cls ._registry .find_instance (bus )
313+
277314 def __exit__ (
278315 self ,
279316 exc_type : Optional [Type [BaseException ]],
0 commit comments