Skip to content

Commit 9c71ba9

Browse files
authored
Add support for passing the BluetoothScanningMode to the scanner (#817)
1 parent cf3d81e commit 9c71ba9

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

aioshelly/ble/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import logging
66

7-
from habluetooth import HaBluetoothConnector
7+
from habluetooth import BluetoothScanningMode, HaBluetoothConnector
88

99
from ..exceptions import RpcCallError
1010
from ..rpc_device import RpcDevice
@@ -73,6 +73,8 @@ async def async_start_scanner(
7373
def create_scanner(
7474
source: str,
7575
name: str,
76+
requested_mode: BluetoothScanningMode | None = None,
77+
current_mode: BluetoothScanningMode | None = None,
7678
) -> ShellyBLEScanner:
7779
"""Create scanner."""
7880
return ShellyBLEScanner(
@@ -85,6 +87,8 @@ def create_scanner(
8587
can_connect=lambda: False,
8688
),
8789
False,
90+
requested_mode=requested_mode,
91+
current_mode=current_mode,
8892
)
8993

9094

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
bluetooth-data-tools>=1.19.0
22
aiohttp>=3.11.1
3-
habluetooth>=2.1.0
3+
habluetooth>=3.22.0
44
yarl
55
orjson>=3.8.1

tests/ble/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Shelly BLE tests."""

tests/ble/test_init.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Tests for the BLE initialization."""
2+
3+
from __future__ import annotations
4+
5+
from unittest.mock import MagicMock
6+
7+
import habluetooth
8+
import pytest
9+
import pytest_asyncio
10+
from habluetooth import BluetoothScanningMode
11+
12+
from aioshelly.ble import create_scanner
13+
14+
15+
@pytest_asyncio.fixture(autouse=True)
16+
async def ha_manager() -> MagicMock:
17+
"""Mock ha manager."""
18+
await habluetooth.BluetoothManager().async_setup()
19+
20+
21+
@pytest.mark.asyncio
22+
@pytest.mark.parametrize(
23+
("requested_mode", "current_mode"),
24+
[
25+
(BluetoothScanningMode.ACTIVE, BluetoothScanningMode.ACTIVE),
26+
(BluetoothScanningMode.PASSIVE, BluetoothScanningMode.PASSIVE),
27+
(BluetoothScanningMode.ACTIVE, BluetoothScanningMode.PASSIVE),
28+
(BluetoothScanningMode.PASSIVE, BluetoothScanningMode.ACTIVE),
29+
],
30+
)
31+
async def test_create_scanner(
32+
requested_mode: BluetoothScanningMode, current_mode: BluetoothScanningMode
33+
) -> None:
34+
"""Test create scanner."""
35+
scanner = create_scanner(
36+
"AA:BB:CC:DD:EE:FF", "shelly", requested_mode, current_mode
37+
)
38+
assert scanner.requested_mode == requested_mode
39+
assert scanner.current_mode == current_mode
40+
41+
42+
@pytest.mark.asyncio
43+
async def test_create_scanner_back_compat() -> None:
44+
"""Test create scanner works without modes."""
45+
scanner = create_scanner("AA:BB:CC:DD:EE:FF", "shelly")
46+
assert scanner.requested_mode is None
47+
assert scanner.current_mode is None

0 commit comments

Comments
 (0)