Small speed up to bluetooth message wrapper (#490)

This commit is contained in:
J. Nick Koston 2023-07-18 14:11:06 -05:00 committed by GitHub
parent 7b215038f7
commit 8060c3c99d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -468,25 +468,44 @@ class APIClient:
(HomeassistantServiceResponse,), (HomeassistantServiceResponse,),
) )
def _filter_bluetooth_message(
self,
address: int,
handle: int,
msg: message.Message,
) -> bool:
"""Handle a Bluetooth message."""
if TYPE_CHECKING:
assert isinstance(
msg,
(
BluetoothGATTErrorResponse,
BluetoothGATTNotifyResponse,
BluetoothGATTReadResponse,
BluetoothGATTWriteResponse,
),
)
return bool(msg.address == address and msg.handle == handle)
async def _send_bluetooth_message_await_response( async def _send_bluetooth_message_await_response(
self, self,
address: int, address: int,
handle: int, handle: int,
request: message.Message, request: message.Message,
response_type: Type[message.Message], response_type: Union[
Type[BluetoothGATTNotifyResponse],
Type[BluetoothGATTReadResponse],
Type[BluetoothGATTWriteResponse],
],
timeout: float = 10.0, timeout: float = 10.0,
) -> message.Message: ) -> message.Message:
self._check_authenticated() self._check_authenticated()
msg_types = (response_type, BluetoothGATTErrorResponse) msg_types = (response_type, BluetoothGATTErrorResponse)
assert self._connection is not None assert self._connection is not None
def is_response(msg: message.Message) -> bool: message_filter = partial(self._filter_bluetooth_message, address, handle)
if TYPE_CHECKING:
assert isinstance(msg, msg_types)
return bool(msg.address == address and msg.handle == handle) # type: ignore[union-attr]
resp = await self._connection.send_message_await_response_complex( resp = await self._connection.send_message_await_response_complex(
request, is_response, is_response, msg_types, timeout=timeout request, message_filter, message_filter, msg_types, timeout=timeout
) )
if isinstance(resp[0], BluetoothGATTErrorResponse): if isinstance(resp[0], BluetoothGATTErrorResponse):