Skip to content

Commit 7d065c3

Browse files
committed
Added way to surface exceptions in a controlled manner.
1 parent 7ddd9df commit 7d065c3

4 files changed

Lines changed: 17 additions & 7 deletions

File tree

src/powersensor_local/async_event_emitter.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ def unsubscribe(self, evstr, cb):
2121
async def emit(self, evstr, *args):
2222
"""Emits an event to all registered listeners for that event type.
2323
Additional arguments may be supplied with event as appropriate. Each
24-
event handler is awaited before delivering the event to the next."""
24+
event handler is awaited before delivering the event to the next.
25+
If an event handler raises an exception, this is funneled through
26+
to an 'exception' event being emitted. This can chain."""
2527
if self._listeners.get(evstr) is None:
2628
return
2729
for cb in self._listeners[evstr]:
28-
await cb(evstr, *args)
30+
try:
31+
await cb(evstr, *args)
32+
except BaseException as e:
33+
await self.emit('exception', e)

src/powersensor_local/plug_api.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(self, mac, ip, port=49476):
2727
self._mac = mac
2828
self._listener = PlugListener(ip, port)
2929
self._listener.subscribe('message', self._on_message)
30+
self._listener.subscribe('exception', self._on_exception)
3031
self._seen = set()
3132

3233
def connect(self):
@@ -43,6 +44,10 @@ async def disconnect(self):
4344
await self._listener.disconnect()
4445

4546
async def _on_message(self, _, message):
47+
"""Translates the raw message and emits the resulting messages, if any.
48+
49+
Also synthesises 'now_relaying_for' messages as needed.
50+
"""
4651
evs = None
4752
try:
4853
evs = translate_raw_message(message, self._mac)
@@ -64,5 +69,6 @@ async def _on_message(self, _, message):
6469
for name, ev in evs.items():
6570
await self.emit(name, ev)
6671

67-
# FIXME - we'll want to use mac:role for the unique ID in HA, so a reassigned
68-
# sensor shows up differently
72+
async def _on_exception(self, _, e):
73+
"""Propagates exceptions from the plug listener."""
74+
await self.emit('exception', e)

src/powersensor_local/plugevents.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ async def do_exit():
2323
async def on_evt_msg(evt, msg):
2424
print(evt, msg)
2525

26-
async def on_evt(evt):
27-
print(evt)
28-
2926
async def main():
3027
if len(sys.argv) < 3:
3128
print(f"Syntax: {sys.argv[0]} <id> <ip> [port]")
@@ -41,6 +38,7 @@ def handle_sigint(signum, frame):
4138
global plug
4239
plug = PlugApi(sys.argv[1], sys.argv[2], *sys.argv[3:3])
4340
known_evs = [
41+
'exception',
4442
'average_flow',
4543
'average_power',
4644
'average_power_components',

src/powersensor_local/rawplug.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def handle_sigint(signum, frame):
4040

4141
global plug
4242
plug = PlugListener(sys.argv[1], *sys.argv[2:2])
43+
plug.subscribe('exception', on_evt_msg)
4344
plug.subscribe('message', on_evt_msg)
4445
plug.subscribe('connecting', on_evt)
4546
plug.subscribe('connecting', on_evt)

0 commit comments

Comments
 (0)