-
Notifications
You must be signed in to change notification settings - Fork 3
/
wiz.py
125 lines (106 loc) · 3.61 KB
/
wiz.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import json
import webcolors
from constants import *
from message import Config, Params, Request
from scenes import SCENES
from udp import UDP
class Light:
def __init__(self, ip: str):
# TODO: get ip from network auto
self.ip = ip
self.udp = UDP(ip)
self.config = self.get_config()
def send(self, message):
result = self.udp.call(message)
if message.params:
self.update_config(message.params)
return result
def on(self):
params = Params(state=True)
message = self.message(params)
self.send(message)
def off(self):
params = Params(state=False)
message = self.message(params)
self.send(message)
def switch(self):
if self.config.state:
self.off()
else:
self.on()
def color(self, **kwargs):
params = Params()
if HEXCODE in kwargs:
params['r'] = int(kwargs[HEXCODE][:2], 16)
params['g'] = int(kwargs[HEXCODE][2:4], 16)
params['b'] = int(kwargs[HEXCODE][4:], 16)
elif COLOR in kwargs:
params['r'], params['g'], params['b'] = webcolors.name_to_rgb(kwargs[COLOR])
else:
if RED in kwargs:
params['r'] = kwargs[RED]
if BLUE in kwargs:
params['b'] = kwargs[BLUE]
if GREEN in kwargs:
params['g'] = kwargs[GREEN]
message = self.message(params)
self.send(message)
def brightness(self, value: int):
params = Params(dimming=value)
message = self.message(params)
self.send(message)
def temp(self, value: int):
params = Params(temp=value)
message = self.message(params)
self.send(message)
def speed(self, value: int):
params = Params(speed=value)
message = self.message(params)
self.send(message)
def scene(self, scene_id=None, scene=None):
if scene:
if scene in SCENES:
scene_id = SCENES[scene]
if not scene_id:
scene_id = input(self.get_prompt())
params = Params(sceneId=scene_id)
message = self.message(params)
self.send(message)
def get_config(self):
message = Request(GET_PILOT)
result_json = self.send(message).decode('UTF-8')
result = json.loads(result_json)['result']
config = Config(state=result['state'], scene=result['sceneId'], brightness=result['dimming'])
if 'r' in result:
config.red = result['r']
config.green = result['g']
config.blue = result['b']
return config
def update_config(self, params):
for key, value in params.items():
if key == 'sceneId':
self.config.scene = params['sceneId']
if key == 'r':
self.config.red = params['r']
elif key == 'g':
self.config.green = params['g']
elif key == 'b':
self.config.blue = params['b']
elif key == 'dimming':
self.config.brightness = params['dimming']
elif key == 'state':
self.config.state = params['state']
@staticmethod
def message(params: Params):
return Request(SET_PILOT, params)
@staticmethod
def get_prompt():
prompt = 'Select one of the following scenes:\n'
for scene, scene_id in SCENES.items():
prompt += '{:>3} : {}\n'.format(scene_id, scene)
return prompt
if __name__ == '__main__':
light = Light(IP)
light.scene()
light.color(red=255)
light.switch()