fix test with cython
This commit is contained in:
parent
9350227f70
commit
8db97a744a
|
@ -72,8 +72,10 @@ from .api_pb2 import ( # type: ignore
|
|||
VoiceAssistantResponse,
|
||||
)
|
||||
from .client_callbacks import (
|
||||
handle_timeout,
|
||||
on_ble_raw_advertisement_response,
|
||||
on_bluetooth_connections_free_response,
|
||||
on_bluetooth_device_connection_response,
|
||||
on_bluetooth_gatt_notify_data_response,
|
||||
on_bluetooth_le_advertising_response,
|
||||
on_home_assistant_service_response,
|
||||
|
@ -528,27 +530,6 @@ class APIClient:
|
|||
(BluetoothConnectionsFreeResponse,),
|
||||
)
|
||||
|
||||
def _handle_timeout(self, fut: asyncio.Future[None]) -> None:
|
||||
"""Handle a timeout."""
|
||||
if not fut.done():
|
||||
fut.set_exception(asyncio.TimeoutError)
|
||||
|
||||
def _on_bluetooth_device_connection_response(
|
||||
self,
|
||||
connect_future: asyncio.Future[None],
|
||||
address: int,
|
||||
on_bluetooth_connection_state: Callable[[bool, int, int], None],
|
||||
msg: BluetoothDeviceConnectionResponse,
|
||||
) -> None:
|
||||
"""Handle a BluetoothDeviceConnectionResponse message.""" ""
|
||||
if address == msg.address:
|
||||
on_bluetooth_connection_state(msg.connected, msg.mtu, msg.error)
|
||||
# Resolve on ANY connection state since we do not want
|
||||
# to wait the whole timeout if the device disconnects
|
||||
# or we get an error.
|
||||
if not connect_future.done():
|
||||
connect_future.set_result(None)
|
||||
|
||||
async def bluetooth_device_connect( # pylint: disable=too-many-locals, too-many-branches
|
||||
self,
|
||||
address: int,
|
||||
|
@ -583,7 +564,7 @@ class APIClient:
|
|||
address_type=address_type or 0,
|
||||
),
|
||||
partial(
|
||||
self._on_bluetooth_device_connection_response,
|
||||
on_bluetooth_device_connection_response,
|
||||
connect_future,
|
||||
address,
|
||||
on_bluetooth_connection_state,
|
||||
|
@ -593,7 +574,7 @@ class APIClient:
|
|||
|
||||
loop = self._loop
|
||||
timeout_handle = loop.call_at(
|
||||
loop.time() + timeout, self._handle_timeout, connect_future
|
||||
loop.time() + timeout, handle_timeout, connect_future
|
||||
)
|
||||
timeout_expired = False
|
||||
connect_ok = False
|
||||
|
|
|
@ -8,3 +8,5 @@ cdef object CameraImageResponse, CameraState
|
|||
cdef object HomeassistantServiceCall
|
||||
|
||||
cdef object BluetoothLEAdvertisement
|
||||
|
||||
cdef object asyncio_TimeoutError
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from asyncio import Future
|
||||
from asyncio import TimeoutError as asyncio_TimeoutError
|
||||
from typing import TYPE_CHECKING, Callable
|
||||
|
||||
from google.protobuf import message
|
||||
|
||||
from .api_pb2 import ( # type: ignore
|
||||
BluetoothConnectionsFreeResponse,
|
||||
BluetoothDeviceConnectionResponse,
|
||||
BluetoothGATTNotifyDataResponse,
|
||||
BluetoothLEAdvertisementResponse,
|
||||
BluetoothLERawAdvertisement,
|
||||
|
@ -93,3 +96,25 @@ def on_subscribe_home_assistant_state_response(
|
|||
msg: SubscribeHomeAssistantStateResponse,
|
||||
) -> None:
|
||||
on_state_sub(msg.entity_id, msg.attribute)
|
||||
|
||||
|
||||
def handle_timeout(fut: Future[None]) -> None:
|
||||
"""Handle a timeout."""
|
||||
if not fut.done():
|
||||
fut.set_exception(asyncio_TimeoutError)
|
||||
|
||||
|
||||
def on_bluetooth_device_connection_response(
|
||||
connect_future: Future[None],
|
||||
address: int,
|
||||
on_bluetooth_connection_state: Callable[[bool, int, int], None],
|
||||
msg: BluetoothDeviceConnectionResponse,
|
||||
) -> None:
|
||||
"""Handle a BluetoothDeviceConnectionResponse message.""" ""
|
||||
if address == msg.address:
|
||||
on_bluetooth_connection_state(msg.connected, msg.mtu, msg.error)
|
||||
# Resolve on ANY connection state since we do not want
|
||||
# to wait the whole timeout if the device disconnects
|
||||
# or we get an error.
|
||||
if not connect_future.done():
|
||||
connect_future.set_result(None)
|
||||
|
|
Loading…
Reference in New Issue