A few fixes for BLE GATT (#261)

This commit is contained in:
Jesse Hills 2022-09-30 07:25:18 +13:00 committed by GitHub
parent dcd8d2af9a
commit 45d727cc03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 106 additions and 99 deletions

View File

@ -1199,20 +1199,19 @@ message BluetoothGATTGetServicesRequest {
}
message BluetoothGATTDescriptor {
string uuid = 1;
repeated uint64 uuid = 1;
uint32 handle = 2;
string description = 3;
}
message BluetoothGATTCharacteristic {
string uuid = 1;
repeated uint64 uuid = 1;
uint32 handle = 2;
uint32 properties = 3;
repeated BluetoothGATTDescriptor descriptors = 4;
}
message BluetoothGATTService {
string uuid = 1;
repeated uint64 uuid = 1;
uint32 handle = 2;
repeated BluetoothGATTCharacteristic characteristics = 3;
}

File diff suppressed because one or more lines are too long

View File

@ -119,6 +119,7 @@ from .model import (
DeviceInfo,
EntityInfo,
EntityState,
ESPHomeBluetoothGATTServices,
FanDirection,
FanInfo,
FanSpeed,
@ -472,16 +473,17 @@ class APIClient:
on_msg,
)
def unsub() -> None:
assert self._connection is not None
self._connection.remove_message_callback(on_msg)
try:
async with async_timeout.timeout(timeout):
await event.wait()
except asyncio.TimeoutError as err:
unsub()
raise TimeoutAPIError("Timeout waiting for connect response") from err
def unsub() -> None:
assert self._connection is not None
self._connection.remove_message_callback(on_msg)
return unsub
async def bluetooth_device_disconnect(self, address: int) -> None:
@ -495,7 +497,9 @@ class APIClient:
)
)
async def bluetooth_gatt_get_services(self, address: int) -> BluetoothGATTServices:
async def bluetooth_gatt_get_services(
self, address: int
) -> ESPHomeBluetoothGATTServices:
self._check_authenticated()
def do_append(msg: message.Message) -> bool:
@ -511,7 +515,7 @@ class APIClient:
services = []
for msg in resp:
services.extend(BluetoothGATTServices.from_pb(msg).services)
return BluetoothGATTServices(address=address, services=services)
return ESPHomeBluetoothGATTServices(address=address, services=services)
async def bluetooth_gatt_read(
self, address: int, characteristic_handle: int, timeout: float = 10.0

View File

@ -13,6 +13,7 @@ from typing import (
Union,
cast,
)
from uuid import UUID
from .util import fix_float_single_double_conversion
@ -762,6 +763,11 @@ def _long_uuid(uuid: str) -> str:
).lower()
def _join_split_uuid(value: List[int]) -> str:
"""Convert a high/low uuid into a single string."""
return str(UUID(int=((value[0] << 64) | value[1])))
def _convert_bluetooth_le_service_uuids(value: List[str]) -> List[str]:
return [_long_uuid(v) for v in value]
@ -818,9 +824,8 @@ class BluetoothGATTRead(APIModelBase):
@dataclass(frozen=True)
class BluetoothGATTDescriptor(APIModelBase):
uuid: str = converter_field(default="", converter=_long_uuid)
uuid: str = converter_field(default="", converter=_join_split_uuid)
handle: int = 0
description: str = ""
@classmethod
def convert_list(cls, value: List[Any]) -> List["BluetoothGATTDescriptor"]:
@ -835,7 +840,7 @@ class BluetoothGATTDescriptor(APIModelBase):
@dataclass(frozen=True)
class BluetoothGATTCharacteristic(APIModelBase):
uuid: str = converter_field(default="", converter=_long_uuid)
uuid: str = converter_field(default="", converter=_join_split_uuid)
handle: int = 0
properties: int = 0
@ -856,7 +861,7 @@ class BluetoothGATTCharacteristic(APIModelBase):
@dataclass(frozen=True)
class BluetoothGATTService(APIModelBase):
uuid: str = converter_field(default="", converter=_long_uuid)
uuid: str = converter_field(default="", converter=_join_split_uuid)
handle: int = 0
characteristics: List[BluetoothGATTCharacteristic] = converter_field(
default_factory=list, converter=BluetoothGATTCharacteristic.convert_list
@ -881,6 +886,12 @@ class BluetoothGATTServices(APIModelBase):
)
@dataclass(frozen=True)
class ESPHomeBluetoothGATTServices:
address: int = 0
services: List[BluetoothGATTService] = field(default_factory=list)
@dataclass(frozen=True)
class BluetoothConnectionsFree(APIModelBase):
free: int = 0