Skip to content

Commit

Permalink
Update bluetooth deps for bleak 0.20.0
Browse files Browse the repository at this point in the history
replaces and closes #89906
  • Loading branch information
bdraco committed Mar 18, 2023
1 parent 538df5e commit 4cd064f
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions homeassistant/components/bluetooth/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,38 @@ def set_disconnected_callback(
self.__disconnected_callback = callback
if self._backend:
self._backend.set_disconnected_callback(
callback, # type: ignore[arg-type]
self._make_disconnected_callback(callback),
**kwargs,
)

def _make_disconnected_callback(
self, callback: Callable[[BleakClient], None] | None
) -> Callable[[], None] | None:
"""Make the disconnected callback.
https://github.com/hbldh/bleak/pull/1256
The disconnected callback needs to get the top level
BleakClientWrapper instance, not the backend instance.
The signature of the callback for the backend is:
Callable[[], None]
To make this work we need to wrap the callback in a partial
that passes the BleakClientWrapper instance as the first
argument.
"""
return None if callback is None else partial(callback, self)

async def connect(self, **kwargs: Any) -> bool:
"""Connect to the specified GATT server."""
assert models.MANAGER is not None
manager = models.MANAGER
wrapped_backend = self._async_get_best_available_backend_and_device(manager)
self._backend = wrapped_backend.client(
wrapped_backend.device,
disconnected_callback=self.__disconnected_callback,
disconnected_callback=self._make_disconnected_callback(
self.__disconnected_callback
),
timeout=self.__timeout,
hass=manager.hass,
)
Expand Down

0 comments on commit 4cd064f

Please sign in to comment.