Speed up parsing/conversion of BluetoothLEAdvertisement (#353)

This commit is contained in:
J. Nick Koston 2023-01-05 16:23:40 -10:00 committed by GitHub
parent 15edff86a3
commit 049dc8bb56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 15 deletions

View File

@ -448,7 +448,7 @@ class APIClient:
msg_types = (BluetoothLEAdvertisementResponse,)
def on_msg(msg: BluetoothLEAdvertisementResponse) -> None:
on_bluetooth_le_advertisement(BluetoothLEAdvertisement.from_pb(msg))
on_bluetooth_le_advertisement(BluetoothLEAdvertisement.from_pb(msg)) # type: ignore[misc]
assert self._connection is not None
await self._connection.send_message_callback_response(

View File

@ -19,7 +19,11 @@ from uuid import UUID
from .util import fix_float_single_double_conversion
if TYPE_CHECKING:
from .api_pb2 import BluetoothServiceData, HomeassistantServiceMap # type: ignore
from .api_pb2 import ( # type: ignore
BluetoothLEAdvertisementResponse,
BluetoothServiceData,
HomeassistantServiceMap,
)
# All fields in here should have defaults set
# Home Assistant depends on these fields being constructible
@ -821,20 +825,32 @@ def _convert_bluetooth_le_name(value: bytes) -> str:
@dataclass(frozen=True)
class BluetoothLEAdvertisement(APIModelBase):
address: int = 0
rssi: int = 0
address_type: int = 0
def __post_init__(self) -> None:
"""Post init hook disabled."""
name: str = converter_field(default="", converter=_convert_bluetooth_le_name)
service_uuids: List[str] = converter_field(
default_factory=list, converter=_convert_bluetooth_le_service_uuids
)
service_data: Dict[str, bytes] = converter_field(
default_factory=dict, converter=_convert_bluetooth_le_service_data
)
manufacturer_data: Dict[int, bytes] = converter_field(
default_factory=dict, converter=_convert_bluetooth_le_manufacturer_data
)
address: int
rssi: int
address_type: int
name: str
service_uuids: List[str]
service_data: Dict[str, bytes]
manufacturer_data: Dict[int, bytes]
@classmethod
def from_pb( # type: ignore[misc]
cls: "BluetoothLEAdvertisement", data: "BluetoothLEAdvertisementResponse"
) -> "BluetoothLEAdvertisement":
return cls( # type: ignore[operator, no-any-return]
address=data.address,
rssi=data.rssi,
address_type=data.address_type,
name=_convert_bluetooth_le_name(data.name),
service_uuids=_convert_bluetooth_le_service_uuids(data.service_uuids),
service_data=_convert_bluetooth_le_service_data(data.service_data),
manufacturer_data=_convert_bluetooth_le_manufacturer_data(
data.manufacturer_data
),
)
@dataclass(frozen=True)