Raise BluetoothDeviceConnectionResponse if connection drops while getting GATT services

This commit is contained in:
J. Nick Koston 2023-11-28 07:53:29 -06:00
parent e182f68b42
commit 4b4510b644
No known key found for this signature in database
1 changed files with 14 additions and 9 deletions

View File

@ -757,30 +757,35 @@ class APIClient:
self, address: int
) -> ESPHomeBluetoothGATTServices:
append_types = (BluetoothGATTGetServicesResponse, BluetoothGATTErrorResponse)
stop_types = (BluetoothGATTGetServicesDoneResponse, BluetoothGATTErrorResponse)
stop_types = (
BluetoothDeviceConnectionResponse,
BluetoothGATTGetServicesDoneResponse,
BluetoothGATTErrorResponse,
)
msg_types = (
BluetoothGATTGetServicesResponse,
BluetoothGATTGetServicesDoneResponse,
BluetoothGATTErrorResponse,
)
def do_append(msg: message.Message) -> bool:
return isinstance(msg, append_types) and msg.address == address
return type(msg) in append_types and msg.address == address
def do_stop(msg: message.Message) -> bool:
return isinstance(msg, stop_types) and msg.address == address
return type(msg) in stop_types and msg.address == address
resp = await self._get_connection().send_messages_await_response_complex(
(BluetoothGATTGetServicesRequest(address=address),),
do_append,
do_stop,
(
BluetoothGATTGetServicesResponse,
BluetoothGATTGetServicesDoneResponse,
BluetoothGATTErrorResponse,
),
(*msg_types, BluetoothDeviceConnectionResponse),
DEFAULT_BLE_TIMEOUT,
)
services = []
for msg in resp:
self._raise_for_ble_connection_change(address, msg, msg_types)
if isinstance(msg, BluetoothGATTErrorResponse):
raise BluetoothGATTAPIError(BluetoothGATTError.from_pb(msg))
services.extend(BluetoothGATTServices.from_pb(msg).services)
return ESPHomeBluetoothGATTServices(address=address, services=services) # type: ignore[call-arg]