-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.py
88 lines (69 loc) · 2.2 KB
/
cli.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Manual testing CLI."""
# ruff: noqa: T201
import argparse
import asyncio
import logging
import aioconsole
from bleak import BleakScanner
from idasen_ha import Desk
logging.basicConfig(format="%(asctime)s [%(name)s %(levelname)s]: %(message)s")
logger = logging.getLogger("idasen_ha")
logger.setLevel(logging.DEBUG)
logging.getLogger("idasen").setLevel(logging.DEBUG)
logging.getLogger("bleak").setLevel(logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--address", help="Desk's bluetooth address")
args = parser.parse_args()
async def getBLEDevice(address: str):
"""Get BLE Device from address."""
return await BleakScanner.find_device_by_address(
address
) # pyright: ignore[reportGeneralTypeIssues]
def print_menu():
"""Print the menu."""
print("\n")
print(30 * "-", "MENU", 30 * "-")
print("c - Connect")
print("d - Disconnect")
print("k - Move Up")
print("j - Move Down")
print("0 - Stop Moving")
print("q - Quit")
print("h - Print this menu")
print(67 * "-")
async def start():
"""Start the CLI."""
if args.address is None:
logger.error("Desk address argument missing")
return
def update_callback(height: int | None):
pass
ble_device = await getBLEDevice(args.address)
if ble_device is None:
logger.error("Desk not found")
return
desk = Desk(update_callback)
loop = True
while loop:
print_menu()
choice = await aioconsole.ainput("Enter your choice: ")
if choice == "h":
print_menu()
elif choice == "c":
try:
await desk.connect(ble_device, True)
except Exception as ex:
logger.exception(ex)
elif choice == "d":
await desk.disconnect()
elif choice == "k":
asyncio.create_task(desk.move_up()) # noqa: RUF006
elif choice == "j":
asyncio.create_task(desk.move_down()) # noqa: RUF006
elif choice == "0":
await desk.stop()
elif choice == "q":
loop = False
else:
print("Wrong option selection. Enter any key to try again..")
asyncio.run(start())