mirror of
https://github.com/vincentbernat/i3wm-configuration.git
synced 2025-07-03 14:54:21 +02:00
i3-companion: let @retry decorator handle exception for battery
This commit is contained in:
parent
f439a62b1c
commit
fcd75c38b4
1 changed files with 26 additions and 28 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue