Skip to content

Commit

Permalink
v0.2.22: fix device scan
Browse files Browse the repository at this point in the history
  • Loading branch information
dudanov committed Aug 10, 2024
1 parent dff5f04 commit 1b88b86
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/simple_with_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import asyncio
import logging

from pyftms import get_client_from_address, FtmsEvents, FitnessMachine
from pyftms import FitnessMachine, FtmsEvents, get_client_from_address

ADDRESS = "29:84:5A:22:A4:11"

Expand Down
42 changes: 22 additions & 20 deletions pyftms/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from bleak import BleakScanner
from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData
from bleak.exc import BleakDeviceNotFoundError

from .backends import (
ControlEvent,
Expand Down Expand Up @@ -50,6 +51,7 @@ def get_client(
- `adv_or_type` - Service [advertisement data](https://bleak.readthedocs.io/en/latest/backends/index.html#bleak.backends.scanner.AdvertisementData) or `MachineType`.
- `timeout` - Control operation timeout. Defaults to 2.0s.
- `on_ftms_event` - Callback for receiving fitness machine events.
- `on_disconnect` - Disconnetion callback.
Return:
- `FitnessMachine` instance.
Expand Down Expand Up @@ -88,34 +90,34 @@ async def get_client_from_address(
- `scan_timeout` - Scanning timeout. Defaults to 10.0s.
- `timeout` - Control operation timeout. Defaults to 2.0s.
- `on_ftms_event` - Callback for receiving fitness machine events.
- `on_disconnect` - Disconnetion callback.
Return:
- `FitnessMachine` instance.
- `FitnessMachine` instance if device found successfully.
"""

future: asyncio.Future[tuple[BLEDevice, AdvertisementData]] = asyncio.Future()

def _on_device(dev: BLEDevice, adv: AdvertisementData) -> None:
if not future.done() and dev.address.lower() == address.lower():
future.set_result((dev, adv))

scanner = BleakScanner(_on_device, [FITNESS_MACHINE_SERVICE_UUID])
async with BleakScanner() as scanner:
try:
async with asyncio.timeout(scan_timeout):
async for dev, adv in scanner.advertisement_data():
if dev.address.lower() != address.lower():
continue

await scanner.start()
data = adv.service_data.get(FITNESS_MACHINE_SERVICE_UUID)

try:
dev, adv = await asyncio.wait_for(future, scan_timeout)
if data is not None and len(data) == 3:
return get_client(
dev,
adv,
timeout=timeout,
on_ftms_event=on_ftms_event,
on_disconnect=on_disconnect,
)

return get_client(
dev,
adv,
timeout=timeout,
on_ftms_event=on_ftms_event,
on_disconnect=on_disconnect,
)
except asyncio.TimeoutError:
pass

finally:
await scanner.stop()
raise BleakDeviceNotFoundError(address)


__all__ = [
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pyftms"
version = "0.2.21"
version = "0.2.22"
description = "PyFTMS - Python Fitness Machine Service client library."
authors = ["Sergey V. DUDANOV <[email protected]>"]
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from pyftms.serializer import BaseModel, get_serializer, ModelSerializer
from pyftms.models import TreadmillData
from pyftms.serializer import BaseModel, ModelSerializer, get_serializer


@pytest.mark.parametrize(
Expand Down

0 comments on commit 1b88b86

Please sign in to comment.