File: associate.py

package info (click to toggle)
py-dormakaba-dkey 1.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208 kB
  • sloc: python: 1,733; makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,366 bytes parent folder | download | duplicates (2)
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
"""Example which associates with a lock."""

from __future__ import annotations

import asyncio
import logging
import sys

from bleak import AdvertisementData, BleakScanner, BLEDevice
from bleak.exc import BleakError

from py_dormakaba_dkey import DKEYLock

ADDRESS = "F0:94:0A:BD:3D:0A"

_LOGGER = logging.getLogger(__name__)


async def main(address: str) -> None:
    """Associate with a lock."""

    found_lock_evt = asyncio.Event()
    lock_device = None

    def callback(device: BLEDevice, advertising_data: AdvertisementData):
        nonlocal lock_device
        if device.address == address:
            lock_device = device
            found_lock_evt.set()

    async with BleakScanner(
        detection_callback=callback,
    ):
        await found_lock_evt.wait()

    if not lock_device:
        raise BleakError(f"A device with address {address} could not be found.")

    lock = DKEYLock(lock_device)

    activation_code = "m8ll-41s4"
    associationdata = await lock.associate(activation_code)
    _LOGGER.info(
        "Association data: %s",
        associationdata.to_json() if associationdata else "<None>",
    )


if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    logging.getLogger("bleak.backends.bluezdbus.manager").setLevel(logging.WARNING)
    asyncio.run(main(sys.argv[1] if len(sys.argv) == 2 else ADDRESS))