-
Notifications
You must be signed in to change notification settings - Fork 0
/
yeelight_lib.py
146 lines (106 loc) · 3.14 KB
/
yeelight_lib.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
# !/usr/bin/python3
import socket
import json
import sys
class Bulb:
def __init__(self,ip,port,transitionType,transitionTime):
#Get the parameters
self.ipAddress = (ip,port);
self.transitionType = transitionType
self.transitionTime = transitionTime
#Configure the client
self.bulb = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.bulb.settimeout(2)
#Setup variable
self.isConnected = 0
def connect(self):
toReturn = 0
try:
self.bulb.connect(self.ipAddress)
self.isConnected = 1
except:
print("Connection Error")
toReturn = 1
self.isConnected = 0
return toReturn
def disconnect(self):
self.bulb.close()
self.isConnected = 0
def createCommand(self,id,method,params):
#Concatenate id and method
toReturn = "{\"id\":"+id+",\"method\":\""+method+"\",\"params\":["
#Add the params
for param in params:
# If this is an str
if(isinstance(param,str)):
toReturn += "\""
toReturn += param
toReturn += "\""
else: #if this is an int
toReturn += str(param)
toReturn += ","
#Finish the command
toReturn = toReturn[:-1]
toReturn += "]}\r\n"
return toReturn
def sendCommandToBulb(self,id,method,params):
#Check the bulb is connected
if(self.isConnected == 0):
return "fail"
#Create the command
toSend = self.createCommand(id,method,params)
toSend = str.encode(toSend)
#Send the command
self.bulb.sendall(toSend)
#Listen the response
maxSize = 50000;
chunks = ""
bytes_recd = 0
needStop = 0
chunk = b''
while not(needStop):
chunk = self.bulb.recv(1)
chunks += (str(chunk,'utf-8'))
bytes_recd = bytes_recd+len(chunk)
if (chunk == b''):
raise RuntimeError("Connection Error")
if ('\n' in chunks):
#Ignore notification messages
if not("props" in chunks):
needStop = 1
else:
chunk = b''
chunks = ""
bytes_recd = 0
# Return the command result
#print(chunks)
result = json.loads(chunks)["result"]
return result
def getCurrentState(self):
result = self.sendCommandToBulb("1","get_prop",["power","bright","rgb","ct"])
return result
def turnOff(self):
toReturn = 1
if(self.sendCommandToBulb("1","set_power",["off",self.transitionType,self.transitionTime])[0] == "ok"):
toReturn = 0
return toReturn
def turnOn(self):
toReturn = 1
if(self.sendCommandToBulb("1","set_power",["on",self.transitionType,self.transitionTime])[0] == "ok"):
toReturn = 0
return toReturn
def adjustBrightness(self,brightness):
self.sendCommandToBulb("1","set_bright",[int(brightness),self.transitionType,self.transitionTime])
def adjustTemperature(self,temperature):
self.sendCommandToBulb("1","set_ct_abx",[int(temperature),self.transitionType,self.transitionTime])
def setColor(self,r,g,b):
colorToSend = (65536*r) + (256*g) + b
self.setColorInt(colorToSend)
def setColorInt(self,colorToSend):
self.sendCommandToBulb("1","set_rgb",[colorToSend,self.transitionType,self.transitionTime])
def getCurrentIp(self):
return self.ipAddress[0]
def getCurrentTransitionType(self):
return self.transitionType
def getCurrentTransitionTime(self):
return self.transitionTime