From fcd75c38b4144d87a8f119c1a697a9558913df19 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Tue, 29 Mar 2022 07:56:02 +0200 Subject: [PATCH] i3-companion: let @retry decorator handle exception for battery --- bin/i3-companion | 54 +++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/bin/i3-companion b/bin/i3-companion index 818584c..adf2278 100755 --- a/bin/i3-companion +++ b/bin/i3-companion @@ -834,35 +834,33 @@ async def bluetooth_status(i3, event, *args): # from PA 16, it may be exposed by PA, then to BlueZ. # See # https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/482 + loop = asyncio.get_event_loop() + sock = socket.socket( + socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM + ) try: - loop = asyncio.get_event_loop() - sock = socket.socket( - socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM - ) - try: - sock.setblocking(False) - # Workaround a bug in asyncio: https://bugs.python.org/issue27929 - fut = loop.create_future() - loop._sock_connect(fut, sock, (mac, 8)) - await fut - # Init - await loop.sock_sendall(sock, b"\0\1\1\0") - ack = await loop.sock_recv(sock, 4) - assert ack == b"\0\1\3\5" - await loop.sock_recv(sock, 5) # firmware - # Battery level - await loop.sock_sendall(sock, b"\2\2\1\0") - ack = await loop.sock_recv(sock, 4) - assert ack == b"\2\2\3\1" - battery = await loop.sock_recv(sock, 1) - battery = battery[0] - finally: - sock.close() - # Choose an icon - icon = f"battery-{(battery+12)//25*25}" - output[-1] = (output[-1], icon) - except Exception as exc: - logger.info("cannot get battery status: %s", exc) + sock.setblocking(False) + # Workaround a bug in asyncio: https://bugs.python.org/issue27929 + fut = loop.create_future() + loop._sock_connect(fut, sock, (mac, 8)) + await fut + # Init + await loop.sock_sendall(sock, b"\0\1\1\0") + ack = await loop.sock_recv(sock, 4) + assert ack == b"\0\1\3\5" + await loop.sock_recv(sock, 5) # firmware + # Battery level + await loop.sock_sendall(sock, b"\2\2\1\0") + ack = await loop.sock_recv(sock, 4) + assert ack == b"\2\2\3\1" + battery = await loop.sock_recv(sock, 1) + battery = battery[0] + finally: + sock.close() + # Choose an icon + icon = f"battery-{(battery+12)//25*25}" + output[-1] = (output[-1], icon) + # Schedule a refresh in 5 minutes bluetooth_status.scheduled = loop.call_later( 600,