diff --git a/aiohomekit/controller/ble/controller.py b/aiohomekit/controller/ble/controller.py index 4c0469ac..fb944993 100644 --- a/aiohomekit/controller/ble/controller.py +++ b/aiohomekit/controller/ble/controller.py @@ -42,10 +42,9 @@ class BleController(AbstractController): def __init__( self, char_cache: CharacteristicCacheType, - bleak_scanner_instance: BleakScanner | None = None, ) -> None: super().__init__(char_cache=char_cache) - self._scanner = bleak_scanner_instance + self._scanner: BleakScanner | None = None self._ble_futures: dict[str, list[asyncio.Future[BLEDevice]]] = {} def _device_detected(self, device: BLEDevice, advertisement_data: AdvertisementData) -> None: @@ -112,17 +111,15 @@ def _device_detected(self, device: BLEDevice, advertisement_data: AdvertisementD self.discoveries[data.id] = BleDiscovery(self, device, data, advertisement_data) async def async_start(self) -> None: - logger.debug("Starting BLE controller with instance: %s", self._scanner) - if not self._scanner: - try: - self._scanner = BleakScanner() - except (FileNotFoundError, BleakDBusError, BleakError) as e: - logger.debug("Failed to init scanner, HAP-BLE not available: %s", str(e)) - self._scanner = None - return + try: + self._scanner = BleakScanner(detection_callback=self._device_detected) + except (FileNotFoundError, BleakDBusError, BleakError) as e: + logger.debug("Failed to init scanner, HAP-BLE not available: %s", str(e)) + self._scanner = None + return + logger.debug("Starting BLE controller with instance: %s", self._scanner) try: - self._scanner.register_detection_callback(self._device_detected) await self._scanner.start() except (FileNotFoundError, BleakDBusError, BleakError) as e: logger.debug("Failed to start scanner, HAP-BLE not available: %s", str(e)) @@ -131,7 +128,6 @@ async def async_start(self) -> None: async def async_stop(self, *args): if self._scanner: await self._scanner.stop() - self._scanner.register_detection_callback(None) self._scanner = None async def async_reachable(self, device_id: str, timeout: float = 10) -> bool: diff --git a/aiohomekit/controller/controller.py b/aiohomekit/controller/controller.py index 1c7b8149..7e685bb6 100644 --- a/aiohomekit/controller/controller.py +++ b/aiohomekit/controller/controller.py @@ -21,7 +21,6 @@ from collections.abc import AsyncIterable from contextlib import AsyncExitStack -from bleak import BleakScanner from zeroconf.asyncio import AsyncZeroconf from aiohomekit import hkjson @@ -56,7 +55,6 @@ def __init__( self, async_zeroconf_instance: AsyncZeroconf | None = None, char_cache: CharacteristicCacheType | None = None, - bleak_scanner_instance: BleakScanner | None = None, ) -> None: """ Initialize an empty controller. Use 'load_data()' to load the pairing data. @@ -66,7 +64,6 @@ def __init__( super().__init__(char_cache=char_cache or CharacteristicCacheMemory()) self._async_zeroconf_instance = async_zeroconf_instance - self._bleak_scanner_instance = bleak_scanner_instance self.transports: dict[TransportType, AbstractController] = {} self._tasks = AsyncExitStack() @@ -99,7 +96,7 @@ async def async_start(self) -> None: ) ) - if BLE_TRANSPORT_SUPPORTED or self._bleak_scanner_instance: + if BLE_TRANSPORT_SUPPORTED: from .ble.controller import ( BleController, # pylint: disable=import-outside-toplevel ) @@ -107,7 +104,6 @@ async def async_start(self) -> None: await self._async_register_backend( BleController( char_cache=self._char_cache, - bleak_scanner_instance=self._bleak_scanner_instance, ) ) diff --git a/aiohomekit/testing.py b/aiohomekit/testing.py index debe7497..54f228e3 100644 --- a/aiohomekit/testing.py +++ b/aiohomekit/testing.py @@ -349,7 +349,7 @@ class FakeController(AbstractController): transport_type = TransportType.IP - def __init__(self, async_zeroconf_instance=None, char_cache=None, bleak_scanner_instance=None): + def __init__(self, async_zeroconf_instance=None, char_cache=None): super().__init__(char_cache=char_cache or CharacteristicCacheMemory()) def add_device(self, accessories): diff --git a/poetry.lock b/poetry.lock index fd193b8c..ffb4c98f 100644 --- a/poetry.lock +++ b/poetry.lock @@ -279,14 +279,14 @@ tests-mypy = ["mypy (>=1.11.1) ; platform_python_implementation == \"CPython\" a [[package]] name = "bleak" -version = "2.1.1" +version = "3.0.1" description = "Bluetooth Low Energy platform Agnostic Klient" optional = false python-versions = ">=3.10" groups = ["main"] files = [ - {file = "bleak-2.1.1-py3-none-any.whl", hash = "sha256:61ac1925073b580c896a92a8c404088c5e5ec9dc3c5bd6fc17554a15779d83de"}, - {file = "bleak-2.1.1.tar.gz", hash = "sha256:4600cc5852f2392ce886547e127623f188e689489c5946d422172adf80635cf9"}, + {file = "bleak-3.0.1-py3-none-any.whl", hash = "sha256:49f93f24ce96610529842da2d9856e7f46597e25966c0f1cfc737f0191566de6"}, + {file = "bleak-3.0.1.tar.gz", hash = "sha256:c8ff077519f8c30a972fd0d22f47a54b981184b2f2a0886d02e55acadbc1045d"}, ] [package.dependencies] @@ -2313,4 +2313,4 @@ ifaddr = ">=0.1.7" [metadata] lock-version = "2.1" python-versions = "^3.10,<3.15" -content-hash = "9662890b3d3ba29499eb9d41f90cc7aeb5c81aab3f078286efe41cdab393a0e3" +content-hash = "52b3a0c2e7b4311f4fef0ee71230a15af78ec75f9c8b6efc02d261dec81986f8" diff --git a/pyproject.toml b/pyproject.toml index 2eacb715..f034a85d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,7 @@ cryptography = ">=2.9.2" zeroconf = ">=0.132.2" commentjson = "^0.9.0" aiocoap = ">=0.4.5" -bleak = "<3" +bleak = ">=2" chacha20poly1305-reuseable = ">=0.12.1" bleak-retry-connector = ">=4.6.0" orjson = ">=3.7.8" diff --git a/tests/test_controller.py b/tests/test_controller.py index c070950c..6d825776 100644 --- a/tests/test_controller.py +++ b/tests/test_controller.py @@ -1,10 +1,9 @@ -from unittest.mock import AsyncMock, MagicMock, patch +from unittest.mock import patch import pytest from aiohomekit.controller import Controller, controller as controller_module from aiohomekit.controller.abstract import TransportType -from aiohomekit.controller.ble.controller import BleController from aiohomekit.controller.ip.controller import IpController from aiohomekit.exceptions import AccessoryDisconnectedError @@ -23,23 +22,6 @@ async def test_remove_pairing(controller_and_paired_accessory): await pairing.get_characteristics([(1, 9)]) -async def test_passing_in_bleak_to_controller(): - """Test we can pass in a bleak scanner instance to the controller. - - Passing in the instance should enable BLE scanning. - """ - with ( - patch.object(controller_module, "BLE_TRANSPORT_SUPPORTED", False), - patch.object(controller_module, "COAP_TRANSPORT_SUPPORTED", False), - patch.object(controller_module, "IP_TRANSPORT_SUPPORTED", False), - ): - controller = Controller(bleak_scanner_instance=AsyncMock(register_detection_callback=MagicMock())) - await controller.async_start() - - assert len(controller.transports) == 1 - assert isinstance(controller.transports[TransportType.BLE], BleController) - - async def test_passing_in_async_zeroconf(mock_asynczeroconf): """Test we can pass in a zeroconf ServiceBrowser instance to the controller. diff --git a/tests/test_controller_ble.py b/tests/test_controller_ble.py index 045b7e91..2d64a52d 100644 --- a/tests/test_controller_ble.py +++ b/tests/test_controller_ble.py @@ -1,7 +1,6 @@ from __future__ import annotations from typing import Any -from unittest.mock import MagicMock from bleak.backends.device import BLEDevice from bleak.backends.scanner import AdvertisementData @@ -58,17 +57,11 @@ def generate_ble_device( @pytest.fixture -def mock_bleak_scanner() -> MagicMock: - return MagicMock() +def ble_controller() -> BleController: + return BleController(CharacteristicCacheMemory()) -@pytest.fixture -def ble_controller(mock_bleak_scanner: MagicMock) -> BleController: - controller = BleController(CharacteristicCacheMemory(), mock_bleak_scanner) - return controller - - -def test_discovery_with_none_name(mock_bleak_scanner: MagicMock, ble_controller: BleController) -> None: +def test_discovery_with_none_name(ble_controller: BleController) -> None: ble_device_with_short_name = generate_ble_device(name="Nam", address="00:00:00:00:00:00") ble_device_with_name = generate_ble_device(name="Name in Full", address="00:00:00:00:00:00") ble_device = generate_ble_device(