-
Notifications
You must be signed in to change notification settings - Fork 1
/
wizcli.py
166 lines (139 loc) · 6.02 KB
/
wizcli.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import socket
import sys
import click
import time
import json
import pyfiglet
import os
from contextlib import closing
from scenes import get_scenes
from termcolor import colored,cprint
os.system('color')
class Wiz:
def __init__(self,ip,port):
self.ip = ip
self.port = port
self.color = {}
self.dimming = 100
def turn_on(self):
message = '{"id":1,"method":"setState","params":{"state":true}}'
udp_client(self.ip,self.port,message)
def turn_off(self):
message = '{"id":1,"method":"setState","params":{"state":false}}'
udp_client(self.ip,self.port,message)
def change_color(self):
self.color = self.ask_color()
message = '{"id":1,"method":"setPilot","params":{"r":0,"g":0,"b":0,"dimming":100}}'
json_message = json.loads(message)
json_message["params"]["r"] = self.color["r"]
json_message["params"]["g"] = self.color["g"]
json_message["params"]["b"] = self.color["b"]
json_message["params"]["dimming"] = self.dimming
udp_client(self.ip,self.port,json.dumps(json_message))
def change_dimming(self,value):
self.dimming = value;
message = '{"id":1,"method":"setPilot","params":{"dimming":100}}'
json_message = json.loads(message)
json_message['params']['dimming'] = self.dimming;
udp_client(self.ip,self.port,json.dumps(json_message))
return True
def ask_color(self):
red = click.prompt(colored("Please enter value of red","red"),type=int,default=0,show_default=True)
green = click.prompt(colored("Please enter value of green","green"),type=int,default=0,show_default=True)
blue = click.prompt(colored("Please enter value of blue","blue"),type=int,default=255,show_default=True)
if red <= 255 and green <= 255 and blue <= 255:
new_color = {"r":red,"g":green,"b":blue}
else:
print("[-] value must be lower than 255")
print("[-] setting to default color (blue)")
new_color = {"r":0,"g":0,"b":255}
return new_color
def set_scene(self):
scenes = get_scenes()
for i,v in scenes.items():
print(colored(f"{i} : {v}","magenta"))
user_prompt = click.prompt(colored("choose an scene:","yellow"),type=int,default=1,show_default=True)
message = '{"id":1,"method":"setPilot","params":{"sceneId":0}}'
json_message = json.loads(message)
json_message['params']['sceneId'] = user_prompt
udp_client(self.ip,self.port,json.dumps(json_message))
def udp_client(ip,port,message,return_response=False):
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
print(colored("Do Ctrl+C to exit the program !!","blue"))
while True:
try:
s.sendto(message.encode('utf-8'),(ip,port))
print(colored(f"\n [-] Data sent to client : {message},\n","green"))
s.settimeout(5)
data,address = s.recvfrom(4096)
if return_response:
s.close()
return data
else:
print(colored(f"\n [-] Data recieved from Client : {data.decode('utf-8')},\n","green"))
s.close()
except socket.timeout:
s.close()
print(colored("[-] Looks like given ip is not wiz-light , try looking ip address in your wiz mobile app !","red"))
exit()
break
def error_check(port,ipaddr):
port_open = False
valid_ip = False
overflow_error = False
wiz_light = False
try:
socket.inet_aton(ipaddr)
valid_ip = True
except socket.error:
valid_ip = False
with closing(socket.socket(socket.AF_INET,socket.SOCK_DGRAM)) as sock:
if port < 65535 and valid_ip:
if sock.connect_ex((ipaddr,port)) == 0:
port_open = True
if port > 65535:
overflow_error = True
if overflow_error == True:
print(colored("[-] port must be below 65535","red"))
if valid_ip == False:
print(colored('[-] ip address is not valid / checking port is available didnt take place ..',"red"))
if valid_ip == True and port == False:
print(colored('[-] given port is not open',"red"))
if overflow_error == False and valid_ip == True and port_open == True:
return True
else:
return False;
def gettui():
print(colored("Coming soon...","blue"))
def welcome():
header = pyfiglet.figlet_format("wiz-cli")
print(colored(header,"red",attrs=["bold"]))
@click.command()
@click.option("-p","--port",default=38899,type=int,help="port number default(38899)")
@click.option("-ip","--ipaddr",required=True,type=str,prompt="please enter the ip of your wiz light (you can get it from your wiz mobile app)",help="ip address of wiz light")
@click.option("-m","--message",default="hello world (default message)",type=str,help="request to send (recommended for advanced users)")
@click.option('--tui',is_flag=True,help="Terminal User Interface (coming soon)")
@click.option('-cc','--changecolor',is_flag=True,help="option to change color")
@click.option('-off','--turnoff',is_flag=True,help="option to turn off light")
@click.option('-on','--turnon',is_flag=True,help="option to turn on light")
@click.option('-dim','--dimming',type=int,help="adjust the dimming of wiz light")
@click.option('-ss','--setscene',is_flag=True,help="set a new scene for your wiz light")
def main(port,ipaddr,message,tui,changecolor,turnoff,turnon,dimming,setscene):
welcome()
if(error_check(port,ipaddr)):
wiz_light = Wiz(ipaddr,port)
if turnon:
wiz_light.turn_on()
if changecolor:
wiz_light.change_color()
if turnoff:
wiz_light.turn_off()
if dimming != None:
wiz_light.change_dimming(dimming)
if setscene:
wiz_light.set_scene()
if tui:
print("tui comming soon")
else:
print(colored("some error","red"))
main()