-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
executable file
·90 lines (71 loc) · 2.95 KB
/
example.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
89
90
#!/usr/bin/env python3
import argparse
import logging
import asyncio
from aiohuesyncbox import HueSyncBox, InvalidState
async def main(args):
registration_info = None
if args.token:
box = HueSyncBox(args.host, args.id, access_token=args.token)
if not await box.is_registered():
await box.close()
print("Token is not valid")
return
else:
print("No token provided, starting registration process with huesyncbox.")
# This is basically the "Registration" example from the readme except for the unregister step which is at the end
box = HueSyncBox(args.host, args.id)
print(
"Press the button on the box for a few seconds until the light blinks green."
)
while not registration_info:
await asyncio.sleep(1)
try:
registration_info = await box.register(
"Your application", "Your device"
)
except InvalidState:
# Indicates the button was not pressed
pass
# Save registration_info somewhere and use the 'access_token' when instantiating HueSyncBox next time
print(registration_info)
# This part is the "Basic usage" example in the readme
await box.initialize()
print(box.device.name)
print(box.execution.sync_active)
print(box.execution.mode)
print(box.execution.hdmi_source)
# Turn the box on (assuming it was off), start syncing on input 3
await box.execution.set_state(sync_active=True, mode="video", hdmi_source="input3")
# Call update() to update with latest status of the box
await box.execution.update()
print(box.execution.sync_active)
print(box.execution.mode)
print(box.execution.hdmi_source)
# Cleanup in case the registration was done this run
if registration_info and not args.skipunregister:
# Unregister by registration ID. HueSyncBox needs to have a valid accessToken to execute this request
await box.unregister(registration_info["registration_id"])
await box.close()
if __name__ == "__main__":
## Commandlineoptions
parser = argparse.ArgumentParser(
description="Example application for aiohuesyncbox."
)
parser.add_argument("host", help="Hostname or IP Address of the syncbox")
parser.add_argument("id", help="ID of the syncbox")
parser.add_argument("--token", help="Token for the hue syncbox")
parser.add_argument(
"--skipunregister",
action="store_true",
help="Skip the unregistration so you can reuse the obtained token. Only unregisters when token was _not_ provided with --token",
)
parser.add_argument(
"--loglevel",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
default="INFO",
help="Define loglevel, default is INFO.",
)
args = parser.parse_args()
logging.basicConfig(level=args.loglevel)
asyncio.run(main(args))