Skip to content

Commit

Permalink
fix: ensure discovered_devices returns an empty list for offline scan…
Browse files Browse the repository at this point in the history
…ners (#35)
  • Loading branch information
bdraco authored Apr 16, 2024
1 parent c8febb9 commit 2350543
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/habluetooth/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import asyncio
import logging
import platform
from typing import TYPE_CHECKING, Any, Coroutine, Iterable
from typing import Any, Coroutine, Iterable

import bleak
from bleak import BleakError
Expand Down Expand Up @@ -166,17 +166,17 @@ def _create_background_task(self, coro: Coroutine[Any, Any, None]) -> None:
@property
def discovered_devices(self) -> list[BLEDevice]:
"""Return a list of discovered devices."""
if TYPE_CHECKING:
assert self.scanner is not None
if not self.scanner:
return []
return self.scanner.discovered_devices

@property
def discovered_devices_and_advertisement_data(
self,
) -> dict[str, tuple[BLEDevice, AdvertisementData]]:
"""Return a list of discovered devices and advertisement data."""
if TYPE_CHECKING:
assert self.scanner is not None
if not self.scanner:
return {}
return self.scanner.discovered_devices_and_advertisement_data

@property
Expand Down
9 changes: 9 additions & 0 deletions tests/test_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ def manager():
return manager


@pytest.mark.asyncio
async def test_empty_data_no_scanner() -> None:
"""Test we handle empty data."""
scanner = HaScanner(BluetoothScanningMode.ACTIVE, "hci0", "AA:BB:CC:DD:EE:FF")
scanner.async_setup()
assert scanner.discovered_devices == []
assert scanner.discovered_devices_and_advertisement_data == {}


@pytest.mark.asyncio
@pytest.mark.skipif("platform.system() != 'Linux'")
async def test_dbus_socket_missing_in_container(
Expand Down

0 comments on commit 2350543

Please sign in to comment.