Simplify raw ble advertisements callbacks (#594)

This commit is contained in:
J. Nick Koston 2023-10-20 17:34:05 -10:00 committed by GitHub
parent 359ed510a2
commit f34bfb5d1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 18 deletions

View File

@ -1,4 +1,8 @@
# flake8: noqa # flake8: noqa
from .api_pb2 import ( # type: ignore[attr-defined] # noqa: F401
BluetoothLERawAdvertisement,
BluetoothLERawAdvertisementsResponse,
)
from .ble_defs import ESP_CONNECTION_ERROR_DESCRIPTION, BLEConnectionError from .ble_defs import ESP_CONNECTION_ERROR_DESCRIPTION, BLEConnectionError
from .client import APIClient from .client import APIClient
from .connection import APIConnection, ConnectionParams from .connection import APIConnection, ConnectionParams

View File

@ -171,7 +171,6 @@ from .model import (
UserServiceArgType, UserServiceArgType,
VoiceAssistantCommand, VoiceAssistantCommand,
VoiceAssistantEventType, VoiceAssistantEventType,
make_ble_raw_advertisement_processor,
) )
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -585,12 +584,17 @@ class APIClient:
msg_types = (BluetoothLERawAdvertisementsResponse,) msg_types = (BluetoothLERawAdvertisementsResponse,)
assert self._connection is not None assert self._connection is not None
on_msg = make_ble_raw_advertisement_processor(on_advertisements)
def _on_ble_raw_advertisement_response(
data: BluetoothLERawAdvertisementsResponse,
) -> None:
on_advertisements(data.advertisements)
unsub_callback = self._connection.send_message_callback_response( unsub_callback = self._connection.send_message_callback_response(
SubscribeBluetoothLEAdvertisementsRequest( SubscribeBluetoothLEAdvertisementsRequest(
flags=BluetoothProxySubscriptionFlag.RAW_ADVERTISEMENTS flags=BluetoothProxySubscriptionFlag.RAW_ADVERTISEMENTS
), ),
on_msg, _on_ble_raw_advertisement_response,
msg_types, msg_types,
) )

View File

@ -8,7 +8,6 @@ from functools import cache, lru_cache, partial
from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast
from uuid import UUID from uuid import UUID
from .api_pb2 import BluetoothLERawAdvertisement # type: ignore[attr-defined]
from .util import fix_float_single_double_conversion from .util import fix_float_single_double_conversion
if sys.version_info[:2] < (3, 10): if sys.version_info[:2] < (3, 10):
@ -22,7 +21,6 @@ else:
if TYPE_CHECKING: if TYPE_CHECKING:
from .api_pb2 import ( # type: ignore from .api_pb2 import ( # type: ignore
BluetoothLEAdvertisementResponse, BluetoothLEAdvertisementResponse,
BluetoothLERawAdvertisementsResponse,
HomeassistantServiceMap, HomeassistantServiceMap,
) )
@ -927,19 +925,6 @@ class BluetoothLEAdvertisement:
) )
def make_ble_raw_advertisement_processor(
on_advertisements: Callable[[list[BluetoothLERawAdvertisement]], None]
) -> Callable[[BluetoothLERawAdvertisementsResponse], None]:
"""Make a processor for BluetoothLERawAdvertisementResponse."""
def _on_ble_raw_advertisement_response(
data: BluetoothLERawAdvertisementsResponse,
) -> None:
on_advertisements(data.advertisements)
return _on_ble_raw_advertisement_response
@_frozen_dataclass_decorator @_frozen_dataclass_decorator
class BluetoothDeviceConnection(APIModelBase): class BluetoothDeviceConnection(APIModelBase):
address: int = 0 address: int = 0