@@ -34,6 +34,8 @@ def discover(bridge_config, new_lights):
34
34
properties ["ip" ] = line .split (":" )[2 ][2 :]
35
35
elif line [:4 ] == "name" :
36
36
properties ["name" ] = line [6 :]
37
+ elif line [:5 ] == "model" :
38
+ properties ["model" ] = line .split (": " ,1 )[1 ]
37
39
device_exist = False
38
40
for light in bridge_config ["lights_address" ].keys ():
39
41
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):
42
44
logging .debug ("light id " + properties ["id" ] + " already exist, updating ip..." )
43
45
break
44
46
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 :)
46
49
logging .debug ("Add YeeLight: " + properties ["id" ])
47
50
modelid = "LWB010"
48
- if properties ["rgb" ]:
51
+ if properties ["model" ] == "desklamp" :
52
+ modelid = "LTW001"
53
+ elif properties ["rgb" ]:
49
54
modelid = "LCT015"
50
55
elif properties ["ct" ]:
51
56
modelid = "LTW001"
@@ -87,6 +92,8 @@ def set_light(ip, light, data):
87
92
elif key == "bri" :
88
93
payload ["set_bright" ] = [int (value / 2.55 ) + 1 , "smooth" , transitiontime ]
89
94
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
90
97
payload ["set_ct_abx" ] = [int (1000000 / value ), "smooth" , transitiontime ]
91
98
elif key == "hue" :
92
99
payload ["set_hsv" ] = [int (value / 182 ), int (light ["state" ]["sat" ] / 2.54 ), "smooth" , transitiontime ]
@@ -118,40 +125,50 @@ def get_light_state(ip, light):
118
125
else :
119
126
state ['on' ] = False
120
127
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" )
142
130
msg_ct = json .dumps ({"id" : 1 , "method" : "get_prop" , "params" :["ct" ]}) + "\r \n "
143
131
tcp_socket .send (msg_ct .encode ())
144
132
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
146
136
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 ())
151
140
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"
156
173
tcp_socket .close ()
157
174
return state
0 commit comments