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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,16 @@ Commands included in this library should only be used for testing.
### Scan for supported devices

```bash
usage: bluetti-scan [-h]
usage: bluetti-scan [-h] [-r REGEX] [-s SCAN_TIME]

Detect bluetti devices by bluetooth name

options:
-h, --help show this help message and exit
-h, --help show this help message and exit
-r REGEX, --regex REGEX
Custom regex to match device name
-s SCAN_TIME, --scan-time SCAN_TIME
How long to scan for devices (seconds)
```

Example output: `['EB3A', '00:00:00:00:00:00']`
Expand Down
46 changes: 36 additions & 10 deletions bluetti_bt_lib/scripts/bluetti_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,66 @@
import argparse
import asyncio
import logging
import re
from typing import List
from bleak import BleakScanner
from bleak.backends.device import BLEDevice

from ..utils.device_info import get_type_by_bt_name


async def scan_async():
async def scan_async(custom_regex, scan_time):
# maybe there is some other trigger we would want to stop scan on?
stop_event = asyncio.Event()
# We can set the above event to prematurely stop the scan e.g. with `stop_event.set()`

found: List[List[str]] = []

async def callback(device: BLEDevice, _):
result = get_type_by_bt_name(device.name)
print(f"Scanning for {scan_time} seconds (or until Ctrl+C)...")

if result is None:
async def callback(device: BLEDevice, _):
if device.name is None:
return

if custom_regex:
match = re.match(custom_regex, device.name)
result = None if match is None else match[0]
else:
result = get_type_by_bt_name(device.name)

if result is not None or device.name.startswith("PBOX"):
found.append(device.address)
stop_event.set()
print([result, device.address])
if not any(device.address in devices for devices in found):
found.append(device.address)
print([result, device.address])

async with BleakScanner(callback):
await stop_event.wait()
try:
await asyncio.wait_for(stop_event.wait(), scan_time)
except (asyncio.exceptions.CancelledError, asyncio.TimeoutError):
exit()


def start():
"""Entrypoint."""
parser = argparse.ArgumentParser(
description="Detect bluetti devices by bluetooth name"
)
parser.parse_args()
parser.add_argument(
"-r", "--regex", type=str, help="Custom regex to match device name"
)
parser.add_argument(
"-s",
"--scan-time",
type=int,
default=5,
help="How long to scan for devices (seconds)",
)
args = parser.parse_args()

logging.basicConfig(level=logging.WARNING)

asyncio.run(scan_async())
asyncio.run(scan_async(args.regex, args.scan_time))


if __name__ == "__main__":
start()