From 16d696a40a130918d8224b512b263bca8f210c84 Mon Sep 17 00:00:00 2001 From: Filip Krakowski Date: Thu, 29 Sep 2022 13:00:48 +0200 Subject: [PATCH 1/3] Fix parameter type in notification callback and initialize asyncio Event after event loop has been initialized --- eq3bt/bleakconnection.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/eq3bt/bleakconnection.py b/eq3bt/bleakconnection.py index d0d040d..d0673f5 100644 --- a/eq3bt/bleakconnection.py +++ b/eq3bt/bleakconnection.py @@ -31,15 +31,19 @@ def __init__(self, mac, iface): self._mac = mac self._iface = iface self._callbacks = {} - self._notifyevent = asyncio.Event() self._notification_handle = None try: self._loop = asyncio.get_running_loop() + _LOGGER.debug("Using existing asyncio loop") except RuntimeError: self._loop = asyncio.new_event_loop() + _LOGGER.debug("Creating new asyncio loop") asyncio.set_event_loop(self._loop) + self._notifyevent = asyncio.Event() + + def __enter__(self): """ Context manager __enter__ for connecting the device @@ -79,10 +83,10 @@ def __exit__(self, exc_type, exc_val, exc_tb): self._loop.run_until_complete(self._conn.disconnect()) self._conn = None - async def on_notification(self, handle, data): + async def on_notification(self, characteristic, data): """Handle Callback from a Bluetooth (GATT) request.""" # The notification handles are off-by-one compared to gattlib and bluepy - handle = handle + 1 + handle = characteristic.handle + 1 _LOGGER.debug( "Got notification from %s: %s", handle, codecs.encode(data, "hex") ) From aa25c6a8fe743dd895d51518ce3231c97d777458 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Mon, 28 Nov 2022 20:51:55 +0100 Subject: [PATCH 2/3] Fix linting & fallback to older characteristic access on exceptions --- eq3bt/bleakconnection.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/eq3bt/bleakconnection.py b/eq3bt/bleakconnection.py index d0673f5..f573fa9 100644 --- a/eq3bt/bleakconnection.py +++ b/eq3bt/bleakconnection.py @@ -43,7 +43,6 @@ def __init__(self, mac, iface): self._notifyevent = asyncio.Event() - def __enter__(self): """ Context manager __enter__ for connecting the device @@ -86,7 +85,11 @@ def __exit__(self, exc_type, exc_val, exc_tb): async def on_notification(self, characteristic, data): """Handle Callback from a Bluetooth (GATT) request.""" # The notification handles are off-by-one compared to gattlib and bluepy - handle = characteristic.handle + 1 + try: + handle = characteristic.handle + 1 + except: # noqa: E722 # fallback to old-style, int-based handle + handle = characteristic + 1 + _LOGGER.debug( "Got notification from %s: %s", handle, codecs.encode(data, "hex") ) From 1571b8dc049cb85027eaae302a760333e1a695fb Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Mon, 28 Nov 2022 20:55:59 +0100 Subject: [PATCH 3/3] Fix flake8 precommit url --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 378ac53..5faad6f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,7 +20,7 @@ repos: hooks: - id: black -- repo: https://gitlab.com/pycqa/flake8 +- repo: https://github.com/pycqa/flake8 rev: 3.9.2 hooks: - id: flake8