File: run.py

package info (click to toggle)
led-ble 1.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 428 kB
  • sloc: python: 717; makefile: 15
file content (55 lines) | stat: -rw-r--r-- 1,599 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import asyncio
import logging

from bleak import BleakScanner
from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData

from led_ble import LEDBLE, LEDBLEState

_LOGGER = logging.getLogger(__name__)

ADDRESS = "D0291B39-3A1B-7FF2-787B-4E743FED5B25"
ADDRESS = "D0291B39-3A1B-7FF2-787B-4E743FED5B25"


async def run() -> None:
    scanner = BleakScanner()
    future: asyncio.Future[BLEDevice] = asyncio.Future()

    def on_detected(device: BLEDevice, adv: AdvertisementData) -> None:
        if future.done():
            return
        _LOGGER.info("Detected: %s", device)
        if device.address.lower() == ADDRESS.lower():
            _LOGGER.info("Found device: %s", device.address)
            future.set_result(device)

    scanner.register_detection_callback(on_detected)
    await scanner.start()

    def on_state_changed(state: LEDBLEState) -> None:
        _LOGGER.info("State changed: %s", state)

    device = await future
    led = LEDBLE(device)
    cancel_callback = led.register_callback(on_state_changed)
    await led.update()
    await led.turn_on()
    await led.set_rgb((255, 0, 0), 255)
    await asyncio.sleep(1)
    await led.set_rgb((0, 255, 0), 128)
    await asyncio.sleep(1)
    await led.set_rgb((0, 0, 255), 255)
    await asyncio.sleep(1)
    await led.set_rgbw((255, 255, 255, 128), 255)
    await asyncio.sleep(1)
    await led.turn_off()
    await led.update()
    cancel_callback()
    await scanner.stop()


logging.basicConfig(level=logging.INFO)
logging.getLogger("led_ble").setLevel(logging.DEBUG)
asyncio.run(run())