diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a4e8d44f..c62859dc 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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) ====================== diff --git a/bleak/__init__.py b/bleak/__init__.py index 1c6f9143..9fd5db80 100644 --- a/bleak/__init__.py +++ b/bleak/__init__.py @@ -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)), @@ -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. diff --git a/bleak/backends/bluezdbus/client.py b/bleak/backends/bluezdbus/client.py index 587f3a5f..15e6b936 100644 --- a/bleak/backends/bluezdbus/client.py +++ b/bleak/backends/bluezdbus/client.py @@ -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() diff --git a/bleak/backends/client.py b/bleak/backends/client.py index d2bf6323..5b7041c1 100644 --- a/bleak/backends/client.py +++ b/bleak/backends/client.py @@ -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 @@ -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. diff --git a/bleak/backends/corebluetooth/client.py b/bleak/backends/corebluetooth/client.py index e08cf094..45781a04 100644 --- a/bleak/backends/corebluetooth/client.py +++ b/bleak/backends/corebluetooth/client.py @@ -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)) diff --git a/bleak/backends/p4android/client.py b/bleak/backends/p4android/client.py index 00b23c4c..6eceec28 100644 --- a/bleak/backends/p4android/client.py +++ b/bleak/backends/p4android/client.py @@ -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): diff --git a/bleak/backends/winrt/client.py b/bleak/backends/winrt/client.py index bc91d2df..61704abc 100644 --- a/bleak/backends/winrt/client.py +++ b/bleak/backends/winrt/client.py @@ -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()