Update esphome bluetooth client for python 3.11 (#86480)

This commit is contained in:
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

View File

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