Skip to content

Commit ca2c4d0

Browse files
authored
Update yeelight.py
1 parent c38ec5b commit ca2c4d0

File tree

1 file changed

+49
-32
lines changed

1 file changed

+49
-32
lines changed

BridgeEmulator/protocols/yeelight.py

+49-32
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def discover(bridge_config, new_lights):
3434
properties["ip"] = line.split(":")[2][2:]
3535
elif line[:4] == "name":
3636
properties["name"] = line[6:]
37+
elif line[:5] == "model":
38+
properties["model"] = line.split(": ",1)[1]
3739
device_exist = False
3840
for light in bridge_config["lights_address"].keys():
3941
if bridge_config["lights_address"][light]["protocol"] == "yeelight" and bridge_config["lights_address"][light]["id"] == properties["id"]:
@@ -42,10 +44,13 @@ def discover(bridge_config, new_lights):
4244
logging.debug("light id " + properties["id"] + " already exist, updating ip...")
4345
break
4446
if (not device_exist):
45-
light_name = "YeeLight id " + properties["id"][-8:] if properties["name"] == "" else properties["name"]
47+
#light_name = "YeeLight id " + properties["id"][-8:] if properties["name"] == "" else properties["name"]
48+
light_name = "Yeelight " + properties["model"] + " " + properties["ip"][-3:] if properties["name"] == "" else properties["name"] #just for me :)
4649
logging.debug("Add YeeLight: " + properties["id"])
4750
modelid = "LWB010"
48-
if properties["rgb"]:
51+
if properties["model"] == "desklamp":
52+
modelid = "LTW001"
53+
elif properties["rgb"]:
4954
modelid = "LCT015"
5055
elif properties["ct"]:
5156
modelid = "LTW001"
@@ -87,6 +92,8 @@ def set_light(ip, light, data):
8792
elif key == "bri":
8893
payload["set_bright"] = [int(value / 2.55) + 1, "smooth", transitiontime]
8994
elif key == "ct":
95+
if ip[:-3] == "201" or ip[:-3] == "202": #quick and dirty only for predefined IPs
96+
if value > 369: value = 369 #define valid CT
9097
payload["set_ct_abx"] = [int(1000000 / value), "smooth", transitiontime]
9198
elif key == "hue":
9299
payload["set_hsv"] = [int(value / 182), int(light["state"]["sat"] / 2.54), "smooth", transitiontime]
@@ -118,40 +125,50 @@ def get_light_state(ip, light):
118125
else:
119126
state['on'] = False
120127
state["bri"] = int(int(light_data[1]) * 2.54)
121-
msg_mode=json.dumps({"id": 1, "method": "get_prop", "params":["color_mode"]}) + "\r\n"
122-
tcp_socket.send(msg_mode.encode())
123-
data = tcp_socket.recv(16 * 1024)
124-
if json.loads(data[:-2].decode("utf8"))["result"][0] == "1": #rgb mode
125-
msg_rgb=json.dumps({"id": 1, "method": "get_prop", "params":["rgb"]}) + "\r\n"
126-
tcp_socket.send(msg_rgb.encode())
127-
data = tcp_socket.recv(16 * 1024)
128-
hue_data = json.loads(data[:-2].decode("utf8"))["result"]
129-
hex_rgb = "%6x" % int(json.loads(data[:-2].decode("utf8"))["result"][0])
130-
r = hex_rgb[:2]
131-
if r == " ":
132-
r = "00"
133-
g = hex_rgb[3:4]
134-
if g == " ":
135-
g = "00"
136-
b = hex_rgb[-2:]
137-
if b == " ":
138-
b = "00"
139-
state["xy"] = convert_rgb_xy(int(r,16), int(g,16), int(b,16))
140-
state["colormode"] = "xy"
141-
elif json.loads(data[:-2].decode("utf8"))["result"][0] == "2": #ct mode
128+
if ip[:-3] == "201" or ip[:-3] == "202": #don't use color_mode on desklamp (IP based exeption)
129+
logging.info("sonderlocke")
142130
msg_ct=json.dumps({"id": 1, "method": "get_prop", "params":["ct"]}) + "\r\n"
143131
tcp_socket.send(msg_ct.encode())
144132
data = tcp_socket.recv(16 * 1024)
145-
state["ct"] = int(1000000 / int(json.loads(data[:-2].decode("utf8"))["result"][0]))
133+
tempval = int(1000000 / int(json.loads(data[:-2].decode("utf8"))["result"][0]))
134+
if tempval > 369: tempval = 369
135+
state["ct"] = tempval
146136
state["colormode"] = "ct"
147-
148-
elif json.loads(data[:-2].decode("utf8"))["result"][0] == "3": #ct mode
149-
msg_hsv=json.dumps({"id": 1, "method": "get_prop", "params":["hue","sat"]}) + "\r\n"
150-
tcp_socket.send(msg_hsv.encode())
137+
else:
138+
msg_mode=json.dumps({"id": 1, "method": "get_prop", "params":["color_mode"]}) + "\r\n"
139+
tcp_socket.send(msg_mode.encode())
151140
data = tcp_socket.recv(16 * 1024)
152-
hue_data = json.loads(data[:-2].decode("utf8"))["result"]
153-
state["hue"] = int(int(hue_data[0]) * 182)
154-
state["sat"] = int(int(hue_data[1]) * 2.54)
155-
state["colormode"] = "hs"
141+
if json.loads(data[:-2].decode("utf8"))["result"][0] == "1": #rgb mode
142+
msg_rgb=json.dumps({"id": 1, "method": "get_prop", "params":["rgb"]}) + "\r\n"
143+
tcp_socket.send(msg_rgb.encode())
144+
data = tcp_socket.recv(16 * 1024)
145+
hue_data = json.loads(data[:-2].decode("utf8"))["result"]
146+
hex_rgb = "%6x" % int(json.loads(data[:-2].decode("utf8"))["result"][0])
147+
r = hex_rgb[:2]
148+
if r == " ":
149+
r = "00"
150+
g = hex_rgb[3:4]
151+
if g == " ":
152+
g = "00"
153+
b = hex_rgb[-2:]
154+
if b == " ":
155+
b = "00"
156+
state["xy"] = convert_rgb_xy(int(r,16), int(g,16), int(b,16))
157+
state["colormode"] = "xy"
158+
elif json.loads(data[:-2].decode("utf8"))["result"][0] == "2": #ct mode
159+
msg_ct=json.dumps({"id": 1, "method": "get_prop", "params":["ct"]}) + "\r\n"
160+
tcp_socket.send(msg_ct.encode())
161+
data = tcp_socket.recv(16 * 1024)
162+
state["ct"] = int(1000000 / int(json.loads(data[:-2].decode("utf8"))["result"][0]))
163+
state["colormode"] = "ct"
164+
165+
elif json.loads(data[:-2].decode("utf8"))["result"][0] == "3": #ct mode
166+
msg_hsv=json.dumps({"id": 1, "method": "get_prop", "params":["hue","sat"]}) + "\r\n"
167+
tcp_socket.send(msg_hsv.encode())
168+
data = tcp_socket.recv(16 * 1024)
169+
hue_data = json.loads(data[:-2].decode("utf8"))["result"]
170+
state["hue"] = int(int(hue_data[0]) * 182)
171+
state["sat"] = int(int(hue_data[1]) * 2.54)
172+
state["colormode"] = "hs"
156173
tcp_socket.close()
157174
return state

0 commit comments

Comments
 (0)