-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
executable file
·96 lines (77 loc) · 2.98 KB
/
gui.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
91
92
93
94
95
96
import PySimpleGUI as sg
from time import localtime, strftime
from json import dumps, loads
from WatchHue import WatchHue
w = WatchHue()
# GUI
layout = [[sg.Button('Load'), sg.Button('Save'), sg.Button('Hue Auth'), sg.Button('Refresh Token'), sg.Button('Bridge Auth'), sg.Button('Exit')],
[sg.Text('URI:'), sg.Input(key='_URI_', do_not_clear=True, default_text='/lights/')],
[sg.Text('Body:'), sg.Input(default_text='{"hue": 50000, "on": true,"bri": 200}', do_not_clear=True, key='_BODY_', size=(35, 3))],
[sg.Button('Get'), sg.Button('Put'), sg.Button('Post')],
[sg.Output(size=(80, 10))],
[sg.Button('Find Sensors'), sg.Button('Check Sensors')]]
window = sg.Window('Window Title').Layout(layout)
# Initialisation
while True: # Event Loop
event, values = window.Read()
print(event, values)
if event is None or event == 'Exit':
break
if event == 'Load':
w.loadConfig()
sg.EasyPrint('Access token: ' + w.access_token, do_not_reroute_stdout=True)
sg.EasyPrint('Refresh token: ' + w.refresh_token)
sg.EasyPrint('Refresh token expires: ' + strftime("%x %X", localtime(w.refresh_expires)))
sg.EasyPrint('Bridge username:' + w.ids['username'])
if event == 'Save':
w.saveConfig()
if event == 'Refresh Token':
w.refreshTokens()
sg.EasyPrint('New tokens:')
sg.EasyPrint('Access token: ' + w.access_token)
sg.EasyPrint('Refresh token: ' + w.refresh_token)
sg.EasyPrint('Refresh token expires: ' + strftime("%x %X", localtime(w.refresh_expires)))
if event == 'Hue Auth':
w.startAuth()
text = sg.PopupGetText('Code', 'xxxxxxxx')
w.setCode(text)
w.requestTokens()
if event == 'Bridge Auth':
w.authWatchHue()
sg.EasyPrint(w.ids['username'])
if event == 'Get':
q = w.getHue(uri=window.FindElement('_URI_').Get())
sg.EasyPrint("Get:")
sg.EasyPrint(q.headers)
print(dumps(q.json(), indent=2))
window.Refresh()
if event == 'Put':
try:
body = window.FindElement('_BODY_').Get()
j = loads(body)
except:
print('Invalid json in body!')
continue
u = window.FindElement('_URI_').Get()
q = w.putHue(uri=u, json=j)
sg.EasyPrint("Post:")
sg.EasyPrint(q.headers)
print(dumps(q.json(), indent=2))
window.Refresh()
if event == 'Post':
try:
j = loads(window.FindElement('_BODY_').Get())
except:
print('Invalid json in body!')
continue
u = window.FindElement('_URI_').Get()
q = w.postHue(uri=u, json=j)
sg.EasyPrint("Post:")
sg.EasyPrint(q.headers)
print(dumps(q.json(), indent=2))
window.Refresh()
if event == 'Find Sensors':
w.get_pres_sensors_https()
if event == 'Check Sensors':
w.get_pres_sensor_state_https()
window.Close()