-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight_utils.py
74 lines (62 loc) · 1.86 KB
/
light_utils.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
import requests as rq
import json
import light_specific_vars
def check_response(response: rq.Response):
if (response.status_code != 200) and (response.status_code != 207):
print(f'There is something wrong with the API call! Status code: {response.status_code}')
def powered_on():
header = light_specific_vars.header
light_url = light_specific_vars.light_url
r = rq.get(url=light_url, headers=header, verify=False)
check_response(r)
response_json = r.json()
light_on = response_json['data'][0]['on']['on']
if light_on:
return True
else:
return False
def change_power():
header = light_specific_vars.header
light_url = light_specific_vars.light_url
payload_on = json.dumps({
"on": {
"on": True
}
})
payload_off = json.dumps({
"on": {
"on": False
}
})
if powered_on():
r = rq.put(light_url, headers=header, data=payload_off, verify=False)
check_response(r)
else:
r = rq.put(light_url, headers=header, data=payload_on, verify=False)
check_response(r)
def change_brightness(level):
if level > 100:
level = 100
elif level < 0:
level = 0
header = light_specific_vars.header
light_url = light_specific_vars.light_url
payload = json.dumps({
"dimming": {
"brightness": level
}
})
r = rq.put(url=light_url, headers=header, data=payload, verify=False)
check_response(r)
def change_color(x, y):
r = rq.put(light_specific_vars.light_url, headers=light_specific_vars.header,
json={
"color": {
"xy": {
"x": x,
"y": y
}
}
}, verify=False)
# print(it)
check_response(r)