diff --git a/aioesphomeapi/client.py b/aioesphomeapi/client.py index 04a927d..44a9320 100644 --- a/aioesphomeapi/client.py +++ b/aioesphomeapi/client.py @@ -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( diff --git a/aioesphomeapi/model.py b/aioesphomeapi/model.py index 06110c3..df83c5a 100644 --- a/aioesphomeapi/model.py +++ b/aioesphomeapi/model.py @@ -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)