11"""Interface abstraction for Powersensor plugs."""
22import sys
33
4+ from logging import Logger
5+
46from .async_event_emitter import AsyncEventEmitter
57from .plug_listener_tcp import PlugListenerTcp
68from .plug_listener_udp import PlugListenerUdp
@@ -17,7 +19,7 @@ class PlugApi(AsyncEventEmitter):
1719 documented in xlatemsg.translate_raw_message.
1820 """
1921
20- def __init__ (self , mac : str , ip : str , port : int = 49476 , proto : str = 'udp' ):
22+ def __init__ (self , mac : str , ip : str , port : int = 49476 , proto : str = 'udp' , logger : Logger | None = None ):
2123 """Create a :class:`PlugApi` instance for a single plug.
2224
2325 Parameters
@@ -32,13 +34,15 @@ def __init__(self, mac: str, ip: str, port: int = 49476, proto: str = 'udp'):
3234 Protocol used for communication. ``'udp'`` selects :class:`PlugListenerUdp`,
3335 while ``'tcp'`` selects :class:`PlugListenerTcp`. Any other value raises a
3436 :class:`ValueError`.
37+ logger : Logger, optional
38+ If provided, enables the logging of escaped exceptions from callbacks.
3539
3640 Raises
3741 ------
3842 ValueError
3943 If *proto* is not ``'udp'`` or ``'tcp'``.
4044 """
41- super ().__init__ ()
45+ super ().__init__ (logger )
4246 self ._mac : str = mac
4347 self ._listener : PlugListenerUdp | PlugListenerTcp
4448 if proto == 'udp' :
@@ -48,7 +52,6 @@ def __init__(self, mac: str, ip: str, port: int = 49476, proto: str = 'udp'):
4852 else :
4953 raise ValueError (f'Unsupported proto: { proto } ' )
5054 self ._listener .subscribe ('message' , self ._on_message )
51- self ._listener .subscribe ('exception' , self ._on_exception )
5255 self ._seen : set [str ] = set ()
5356
5457 def connect (self ) -> None :
@@ -93,10 +96,6 @@ async def _on_message(self, _: str, message: Message) -> None:
9396 for name , ev in evs .items ():
9497 await self .emit (name , ev )
9598
96- async def _on_exception (self , _ : str , e : Exception ) -> None :
97- """Propagates exceptions from the plug listener."""
98- await self .emit ('exception' , e )
99-
10099 @property
101100 def ip_address (self ) -> str :
102101 """
0 commit comments