Update esphome bluetooth client for python 3.11 (#86480)

pull/86483/head
J. Nick Koston 2023-01-23 10:36:19 -10:00 committed by GitHub
parent e15aaf2853
commit 978aafdd09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 7 deletions

View File

@ -68,15 +68,16 @@ def verify_connected(func: _WrapFuncType) -> _WrapFuncType:
)
if not disconnected_event:
raise BleakError("Not connected")
task = asyncio.create_task(func(self, *args, **kwargs))
done, _ = await asyncio.wait(
(task, disconnected_event.wait()),
action_task = asyncio.create_task(func(self, *args, **kwargs))
disconnect_task = asyncio.create_task(disconnected_event.wait())
await asyncio.wait(
(action_task, disconnect_task),
return_when=asyncio.FIRST_COMPLETED,
)
if disconnected_event.is_set():
task.cancel()
if disconnect_task.done():
action_task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await task
await action_task
raise BleakError(
f"{self._source_name}: " # pylint: disable=protected-access
@ -84,7 +85,7 @@ def verify_connected(func: _WrapFuncType) -> _WrapFuncType:
f" {self._ble_device.address}: " # pylint: disable=protected-access
"Disconnected during operation"
)
return next(iter(done)).result()
return action_task.result()
return cast(_WrapFuncType, _async_wrap_bluetooth_connected_operation)