Skip to content

Commit 15f7410

Browse files
committed
Add Wiz config parsing logic
1 parent 0d0968d commit 15f7410

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
GREEN = 'green'
1313
BLUE = 'blue'
1414

15+
NETWORK_NAME = 'wiz_a4f78f'
1516
IP = '192.168.31.170'

message.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ class Params:
66
def __init__(self, **kwargs):
77
self.params = kwargs
88

9+
def __getitem__(self, key):
10+
return self.params[key]
11+
912
def __setitem__(self, key, value):
1013
self.params[key] = value
1114

@@ -16,6 +19,7 @@ def __str__(self):
1619
class Request:
1720
def __init__(self, method: str, params: Params = None):
1821
self.method = method
22+
self.params = None
1923
if params:
2024
self.params = params.params
2125

@@ -26,6 +30,16 @@ def __str__(self):
2630
return json.dumps(self.__dict__)
2731

2832

33+
class Config:
34+
def __init__(self, state=True, scene=11, red=None, green=None, blue=None, brightness=100):
35+
self.state = state
36+
self.scene = scene
37+
self.red = red
38+
self.green = green
39+
self.blue = blue
40+
self.brightness = brightness
41+
42+
2943
if __name__ == '__main__':
3044
message = Request(GET_PILOT, Params(state=True))
3145
print(bytes(message).decode('utf-8'))

wiz.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1+
import json
2+
13
import webcolors
24

3-
from udp import UDP
45
from constants import *
5-
from message import Params, Request
6+
from message import Config, Params, Request
7+
from udp import UDP
68

79

810
class Light:
911
def __init__(self, ip: str):
1012
# TODO: get ip from network auto
1113
self.ip = ip
1214
self.udp = UDP(ip)
15+
self.config = self.get_config()
1316

1417
def send(self, message):
15-
self.udp.call(message)
18+
result = self.udp.call(message)
19+
if message.params:
20+
self.update_config(message.params)
21+
return result
1622

1723
def on(self):
1824
params = Params(state=True)
@@ -22,9 +28,14 @@ def on(self):
2228
def off(self):
2329
params = Params(state=False)
2430
message = self.message(params)
25-
# print(message)
2631
self.send(message)
2732

33+
def switch(self):
34+
if self.config.state:
35+
self.off()
36+
else:
37+
self.on()
38+
2839
def color(self, **kwargs):
2940
params = Params()
3041
if HEXCODE in kwargs:
@@ -41,7 +52,6 @@ def color(self, **kwargs):
4152
if GREEN in kwargs:
4253
params['g'] = kwargs[GREEN]
4354
message = self.message(params)
44-
print(params, message)
4555
self.send(message)
4656

4757
def brightness(self, value: int):
@@ -61,7 +71,27 @@ def speed(self, value: int):
6171

6272
def get_config(self):
6373
message = Request(GET_PILOT)
64-
return self.udp.call(message)
74+
result_json = self.send(message).decode('UTF-8')
75+
result = json.loads(result_json)['result']
76+
config = Config(state=result['state'], scene=result['sceneId'], brightness=result['dimming'])
77+
if 'r' in result:
78+
config.red = result['r']
79+
config.green = result['g']
80+
config.blue = result['b']
81+
return config
82+
83+
def update_config(self, params):
84+
for key, value in params.items():
85+
if key == 'r':
86+
self.config.red = params['r']
87+
elif key == 'g':
88+
self.config.green = params['g']
89+
elif key == 'b':
90+
self.config.blue = params['b']
91+
elif key == 'dimming':
92+
self.config.brightness = params['dimming']
93+
elif key == 'state':
94+
self.config.state = params['state']
6595

6696
@staticmethod
6797
def message(params: Params):
@@ -71,3 +101,4 @@ def message(params: Params):
71101
if __name__ == '__main__':
72102
light = Light(IP)
73103
light.color(red=255)
104+
light.switch()

0 commit comments

Comments
 (0)