Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions aiohomekit/controller/ble/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Expand All @@ -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:
Expand Down
6 changes: 1 addition & 5 deletions aiohomekit/controller/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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()
Expand Down Expand Up @@ -99,15 +96,14 @@ 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
)

await self._async_register_backend(
BleController(
char_cache=self._char_cache,
bleak_scanner_instance=self._bleak_scanner_instance,
)
)

Expand Down
2 changes: 1 addition & 1 deletion aiohomekit/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 1 addition & 19 deletions tests/test_controller.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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.

Expand Down
13 changes: 3 additions & 10 deletions tests/test_controller_ble.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down
Loading