Skip to content

Commit 1b1d0e4

Browse files
authored
feat: port local scanner support from HA (#9)
1 parent af37754 commit 1b1d0e4

File tree

11 files changed

+539
-5
lines changed

11 files changed

+539
-5
lines changed

build_ext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def build(setup_kwargs: Any) -> None:
3030
setup_kwargs.update(
3131
{
3232
"ext_modules": cythonize(
33-
["src/habluetooth/base_scanner.py"],
33+
["src/habluetooth/base_scanner.py", "src/habluetooth/scanner.py"],
3434
compiler_directives={"language_level": "3"}, # Python 3
3535
),
3636
"cmdclass": {"build_ext": BuildExt},

poetry.lock

Lines changed: 49 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ bleak-retry-connector = ">=3.3.0"
3333
bluetooth-data-tools = ">=1.16.0"
3434
home-assistant-bluetooth = ">=1.10.4"
3535
bluetooth-adapters = ">=0.16.1"
36+
bluetooth-auto-recovery = ">=1.2.3"
3637

3738
[tool.poetry.group.dev.dependencies]
3839
pytest = "^7.0"

src/habluetooth/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
SCANNER_WATCHDOG_TIMEOUT,
99
)
1010
from .models import HaBluetoothConnector
11+
from .scanner import BluetoothScanningMode, HaScanner, ScannerStartError
1112

1213
__all__ = [
14+
"BluetoothScanningMode",
15+
"ScannerStartError",
16+
"HaScanner",
1317
"BaseHaScanner",
1418
"BaseHaRemoteScanner",
1519
"HaBluetoothConnector",

src/habluetooth/base_scanner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from home_assistant_bluetooth import BluetoothServiceInfoBleak
1616

1717
from .const import (
18+
CALLBACK_TYPE,
1819
CONNECTABLE_FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
1920
SCANNER_WATCHDOG_INTERVAL,
2021
SCANNER_WATCHDOG_TIMEOUT,
@@ -24,7 +25,6 @@
2425
SCANNER_WATCHDOG_INTERVAL_SECONDS: Final = SCANNER_WATCHDOG_INTERVAL.total_seconds()
2526
MONOTONIC_TIME: Final = monotonic_time_coarse
2627
_LOGGER = logging.getLogger(__name__)
27-
CALLBACK_TYPE = Callable[[], None]
2828

2929

3030
_float = float

src/habluetooth/const.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
from __future__ import annotations
33

44
from datetime import timedelta
5-
from typing import Final
5+
from typing import Callable, Final
6+
7+
CALLBACK_TYPE = Callable[[], None]
8+
9+
SOURCE_LOCAL: Final = "local"
10+
11+
START_TIMEOUT = 15
612

713
# The maximum time between advertisements for a device to be considered
814
# stale when the advertisement tracker cannot determine the interval.

src/habluetooth/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from collections.abc import Callable
55
from dataclasses import dataclass
6+
from enum import Enum
67
from typing import Final
78

89
from bleak import BaseBleakClient
@@ -18,3 +19,10 @@ class HaBluetoothConnector:
1819
client: type[BaseBleakClient]
1920
source: str
2021
can_connect: Callable[[], bool]
22+
23+
24+
class BluetoothScanningMode(Enum):
25+
"""The mode of scanning for bluetooth devices."""
26+
27+
PASSIVE = "passive"
28+
ACTIVE = "active"

src/habluetooth/scanner.pxd

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from .base_scanner cimport BaseHaScanner
2+
3+
4+
cdef object NO_RSSI_VALUE
5+
cdef object BluetoothServiceInfoBleak
6+
cdef object AdvertisementData
7+
cdef object BLEDevice
8+
cdef bint TYPE_CHECKING
9+
10+
11+
cdef class HaScanner(BaseHaScanner):
12+
13+
cdef public object mac_address
14+
cdef public object mode
15+
cdef public object _start_stop_lock
16+
cdef public object _new_info_callback
17+
cdef public object _background_tasks
18+
cdef public object scanner
19+
20+
cpdef void _async_detection_callback(
21+
self,
22+
object device,
23+
object advertisement_data
24+
)

0 commit comments

Comments
 (0)