Add method to cancel bluetooth_gatt_start_notify upon ble device disconnect (#329)

This commit is contained in:
J. Nick Koston 2022-12-02 09:51:03 -10:00 committed by GitHub
parent de5cdfa230
commit 577868a84c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 2 deletions

View File

@ -721,7 +721,17 @@ class APIClient:
address: int,
handle: int,
on_bluetooth_gatt_notify: Callable[[int, bytearray], None],
) -> Callable[[], Coroutine[Any, Any, None]]:
) -> Tuple[Callable[[], Coroutine[Any, Any, None]], Callable[[], None]]:
"""Start a notify session for a GATT characteristic.
Returns two functions that can be used to stop the notify.
The first function is a coroutine that can be awaited to stop the notify.
The second function is a callback that can be called to remove the notify
callbacks without stopping the notify session on the remote device, which
should be used when the connection is lost.
"""
await self._send_bluetooth_message_await_response(
address,
@ -752,7 +762,7 @@ class APIClient:
BluetoothGATTNotifyRequest(address=address, handle=handle, enable=False)
)
return stop_notify
return stop_notify, remove_callback
async def subscribe_home_assistant_states(
self, on_state_sub: Callable[[str, Optional[str]], None]