-
Notifications
You must be signed in to change notification settings - Fork 0
/
kl.py
executable file
·43 lines (35 loc) · 1.15 KB
/
kl.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
#!/usr/bin/python3
import leglight
import pathlib
import json
# Discover's delay can be a bit annoying, so I store the result
# in a file and read from it. I update the file with discover if the
# IP address fails
script_location = pathlib.Path(__file__).parent.resolve()
def write_leglight_to_file():
allLights = leglight.discover(2)
my_light = allLights[0]
light_vars = vars(my_light)
with open(f'{script_location}/light.json', 'w') as file:
file.write(json.dumps(light_vars, indent=2))
def read_from_file():
with open(f'{script_location}/light.json', 'r') as file:
light_vars = json.load(file)
return light_vars
def get_light(vars):
return leglight.LegLight(vars['address'], vars['port'])
try:
file_light_vars = read_from_file()
file_light = get_light(file_light_vars)
except:
print("No light found. Searching for light")
write_leglight_to_file()
print("Trying again")
file_light_vars = read_from_file()
file_light = get_light(file_light_vars)
my_light_vars = vars(file_light)
my_light = get_light(my_light_vars)
if my_light_vars['isOn'] == 1:
my_light.off()
else:
my_light.on()