Skip to content

Commit

Permalink
bleak: fix disconnected_callback arg
Browse files Browse the repository at this point in the history
Since reworking to have a common top-level `BleakClient` class, the
`disconnected_callback` function was called with the backend object
rather than the top-level `BleakClient` object.

This fixes the mistake by using functools.partial to bind the top-level
object to the function and eliminates the need to pass an argument in
the backends.
  • Loading branch information
dlech committed Mar 17, 2023
1 parent 284c3c8 commit af12363
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Fixed
not send a scan response. Fixes #1211.
* Fixed ``BLEDevice`` name sometimes incorrectly ``None``.
* Fixed unhandled exception in ``CentralManagerDelegate`` destructor on macOS. Fixes #1219.
* Fixed object passed to ``disconnected_callback`` is not ``BleakClient``. Fixes #1200.

`0.19.5`_ (2022-11-19)
======================
Expand Down
8 changes: 6 additions & 2 deletions bleak/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ def __init__(

self._backend = PlatformBleakClient(
address_or_ble_device,
disconnected_callback=disconnected_callback,
disconnected_callback=None
if disconnected_callback is None
else functools.partial(disconnected_callback, self),
services=None
if services is None
else set(map(normalize_uuid_str, services)),
Expand Down Expand Up @@ -507,7 +509,9 @@ def set_disconnected_callback(
FutureWarning,
stacklevel=2,
)
self._backend.set_disconnected_callback(callback, **kwargs)
self._backend.set_disconnected_callback(
None if callback is None else functools.partial(callback, self), **kwargs
)

async def connect(self, **kwargs) -> bool:
"""Connect to the specified GATT server.
Expand Down
2 changes: 1 addition & 1 deletion bleak/backends/bluezdbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def on_connected_changed(connected: bool) -> None:

self._cleanup_all()
if self._disconnected_callback is not None:
self._disconnected_callback(self)
self._disconnected_callback()
disconnecting_event = self._disconnecting_event
if disconnecting_event:
disconnecting_event.set()
Expand Down
16 changes: 4 additions & 12 deletions bleak/backends/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def __init__(self, address_or_ble_device: Union[BLEDevice, str], **kwargs):
self.services: Optional[BleakGATTServiceCollection] = None

self._timeout = kwargs.get("timeout", 10.0)
self._disconnected_callback = kwargs.get("disconnected_callback")
self._disconnected_callback: Optional[Callable[[], None]] = kwargs.get(
"disconnected_callback"
)

@property
@abc.abstractmethod
Expand All @@ -56,23 +58,13 @@ def mtu_size(self) -> int:
# Connectivity methods

def set_disconnected_callback(
self, callback: Optional[Callable[["BaseBleakClient"], None]], **kwargs
self, callback: Optional[Callable[[], None]], **kwargs
) -> None:
"""Set the disconnect callback.
The callback will only be called on unsolicited disconnect event.
Callbacks must accept one input which is the client object itself.
Set the callback to ``None`` to remove any existing callback.
.. code-block:: python
def callback(client):
print("Client with address {} got disconnected!".format(client.address))
client.set_disconnected_callback(callback)
client.connect()
Args:
callback: callback to be called on disconnection.
Expand Down
2 changes: 1 addition & 1 deletion bleak/backends/corebluetooth/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def disconnect_callback():
pass

if self._disconnected_callback:
self._disconnected_callback(self)
self._disconnected_callback()

manager = self._central_manager_delegate
logger.debug("CentralManagerDelegate at {}".format(manager))
Expand Down
2 changes: 1 addition & 1 deletion bleak/backends/p4android/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def onConnectionStateChange(self, status, new_state):
new_state == defs.BluetoothProfile.STATE_DISCONNECTED
and self._client._disconnected_callback is not None
):
self._client._disconnected_callback(self._client)
self._client._disconnected_callback()

@java_method("(II)V")
def onMtuChanged(self, mtu, status):
Expand Down
2 changes: 1 addition & 1 deletion bleak/backends/winrt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def handle_session_status_changed(

elif args.status == GattSessionStatus.CLOSED:
if self._disconnected_callback:
self._disconnected_callback(self)
self._disconnected_callback()

for e in self._session_closed_events:
e.set()
Expand Down

0 comments on commit af12363

Please sign in to comment.