Skip to content

Commit be659c0

Browse files
krakowskirytilahti
andauthored
Fix parameter type in notification callback and initialize Event after event loop initialization (#64)
* Fix parameter type in notification callback and initialize asyncio Event after event loop has been initialized * Fix linting & fallback to older characteristic access on exceptions * Fix flake8 precommit url Co-authored-by: Teemu Rytilahti <[email protected]>
1 parent ed73df0 commit be659c0

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

eq3bt/bleakconnection.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,18 @@ def __init__(self, mac, iface):
3131
self._mac = mac
3232
self._iface = iface
3333
self._callbacks = {}
34-
self._notifyevent = asyncio.Event()
3534
self._notification_handle = None
3635

3736
try:
3837
self._loop = asyncio.get_running_loop()
38+
_LOGGER.debug("Using existing asyncio loop")
3939
except RuntimeError:
4040
self._loop = asyncio.new_event_loop()
41+
_LOGGER.debug("Creating new asyncio loop")
4142
asyncio.set_event_loop(self._loop)
4243

44+
self._notifyevent = asyncio.Event()
45+
4346
def __enter__(self):
4447
"""
4548
Context manager __enter__ for connecting the device
@@ -79,10 +82,14 @@ def __exit__(self, exc_type, exc_val, exc_tb):
7982
self._loop.run_until_complete(self._conn.disconnect())
8083
self._conn = None
8184

82-
async def on_notification(self, handle, data):
85+
async def on_notification(self, characteristic, data):
8386
"""Handle Callback from a Bluetooth (GATT) request."""
8487
# The notification handles are off-by-one compared to gattlib and bluepy
85-
handle = handle + 1
88+
try:
89+
handle = characteristic.handle + 1
90+
except: # noqa: E722 # fallback to old-style, int-based handle
91+
handle = characteristic + 1
92+
8693
_LOGGER.debug(
8794
"Got notification from %s: %s", handle, codecs.encode(data, "hex")
8895
)

0 commit comments

Comments
 (0)