i3-companion: let @retry decorator handle exception for battery

This commit is contained in:
Vincent Bernat 2022-03-29 07:56:02 +02:00
parent f439a62b1c
commit fcd75c38b4

View file

@ -834,35 +834,33 @@ async def bluetooth_status(i3, event, *args):
# from PA 16, it may be exposed by PA, then to BlueZ. # from PA 16, it may be exposed by PA, then to BlueZ.
# See # See
# https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/482 # 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: try:
loop = asyncio.get_event_loop() sock.setblocking(False)
sock = socket.socket( # Workaround a bug in asyncio: https://bugs.python.org/issue27929
socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM fut = loop.create_future()
) loop._sock_connect(fut, sock, (mac, 8))
try: await fut
sock.setblocking(False) # Init
# Workaround a bug in asyncio: https://bugs.python.org/issue27929 await loop.sock_sendall(sock, b"\0\1\1\0")
fut = loop.create_future() ack = await loop.sock_recv(sock, 4)
loop._sock_connect(fut, sock, (mac, 8)) assert ack == b"\0\1\3\5"
await fut await loop.sock_recv(sock, 5) # firmware
# Init # Battery level
await loop.sock_sendall(sock, b"\0\1\1\0") await loop.sock_sendall(sock, b"\2\2\1\0")
ack = await loop.sock_recv(sock, 4) ack = await loop.sock_recv(sock, 4)
assert ack == b"\0\1\3\5" assert ack == b"\2\2\3\1"
await loop.sock_recv(sock, 5) # firmware battery = await loop.sock_recv(sock, 1)
# Battery level battery = battery[0]
await loop.sock_sendall(sock, b"\2\2\1\0") finally:
ack = await loop.sock_recv(sock, 4) sock.close()
assert ack == b"\2\2\3\1" # Choose an icon
battery = await loop.sock_recv(sock, 1) icon = f"battery-{(battery+12)//25*25}"
battery = battery[0] output[-1] = (output[-1], icon)
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)
# Schedule a refresh in 5 minutes # Schedule a refresh in 5 minutes
bluetooth_status.scheduled = loop.call_later( bluetooth_status.scheduled = loop.call_later(
600, 600,