@@ -161,14 +161,14 @@ def rules_processor(scheduler=False):
161
161
for action in bridge_config ["rules" ][rule ]["actions" ]:
162
162
Thread (target = sendRequest , args = ["/api/" + bridge_config ["rules" ][rule ]["owner" ] + action ["address" ], action ["method" ], json .dumps (action ["body" ])]).start ()
163
163
164
- def sendRequest (url , method , data ):
164
+ def sendRequest (url , method , data , time_out = 3 ):
165
165
if not url .startswith ( 'http://' ):
166
166
url = "http://127.0.0.1" + url
167
167
opener = urllib2 .build_opener (urllib2 .HTTPHandler )
168
168
request = urllib2 .Request (url , data = data )
169
169
request .add_header ("Content-Type" ,'application/json' )
170
170
request .get_method = lambda : method
171
- response = opener .open (request , timeout = 3 ).read ()
171
+ response = opener .open (request , timeout = time_out ).read ()
172
172
return response
173
173
174
174
def convert_xy (x , y , bri ): #needed for milight hub that don't work with xy values
@@ -246,7 +246,7 @@ def sendLightRequest(light, data):
246
246
else :
247
247
url += "&" + key + "=" + str (value )
248
248
elif lights_address [light ]["protocol" ] == "hue" : #Original Hue light
249
- url = "http://" + lights_address [light ]["ip" ] + "/api/" + lights_address [light ]["username" ] + "/lights/" + lights_address [light ]["light_id" ] + "/state" ;
249
+ url = "http://" + lights_address [light ]["ip" ] + "/api/" + lights_address [light ]["username" ] + "/lights/" + lights_address [light ]["light_id" ] + "/state"
250
250
method = 'PUT'
251
251
payload = data
252
252
elif lights_address [light ]["protocol" ] == "milight" : #MiLight bulb
@@ -358,9 +358,21 @@ def scan_for_lights(): #scan for ESP8266 lights and strips
358
358
except Exception , e :
359
359
print (ip + " is unknow device " + str (e ))
360
360
361
- def syncWithTradfri (): #update Hue Bridge lights states from Ikea Tradfri gateway
361
+ def syncWithLights (): #update Hue Bridge lights states
362
362
for light in lights_address :
363
- if lights_address [light ]["protocol" ] == "ikea_tradfri" :
363
+ if lights_address [light ]["protocol" ] == "native" :
364
+ try :
365
+ light_data = json .loads (sendRequest ("http://" + lights_address [light ]["ip" ] + "/get?light=" + str (lights_address [light ]["light_nr" ]), "GET" , "{}" , 0.5 ))
366
+ except :
367
+ bridge_config ["lights" ][light ]["state" ]["reachable" ] = False
368
+ print ("request error" )
369
+ else :
370
+ bridge_config ["lights" ][light ]["state" ]["reachable" ] = True
371
+ bridge_config ["lights" ][light ]["state" ].update (light_data )
372
+ elif lights_address [light ]["protocol" ] == "hue" :
373
+ light_data = json .loads (sendRequest ("http://" + lights_address [light ]["ip" ] + "/api/" + lights_address [light ]["username" ] + "/lights/" + lights_address [light ]["light_id" ] + "/state" ), "GET" , "{}" , 1 ))
374
+ bridge_config ["lights" ][light ]["state" ].update (light_data )
375
+ elif lights_address [light ]["protocol" ] == "ikea_tradfri" :
364
376
light_stats = json .loads (check_output ("./coap-client-linux -m get -u \" Client_identity\" -k \" " + lights_address [light ]["security_code" ] + "\" \" coaps://" + lights_address [light ]["ip" ] + ":5684/15001/" + str (lights_address [light ]["device_id" ]) + "\" " , shell = True ).split ("\n " )[3 ])
365
377
bridge_config ["lights" ][light ]["state" ]["on" ] = bool (light_stats ["3311" ][0 ]["5850" ])
366
378
bridge_config ["lights" ][light ]["state" ]["bri" ] = light_stats ["3311" ][0 ]["5851" ]
@@ -371,7 +383,6 @@ def syncWithTradfri(): #update Hue Bridge lights states from Ikea Tradfri gatewa
371
383
elif light_stats ["3311" ][0 ]["5706" ] == "efd275" :
372
384
bridge_config ["lights" ][light ]["state" ]["ct" ] = 470
373
385
374
-
375
386
def description ():
376
387
return """<root xmlns=\" urn:schemas-upnp-org:device-1-0\" >
377
388
<specVersion>
@@ -502,7 +513,11 @@ def update_all_lights():
502
513
payload = {}
503
514
payload ["on" ] = bridge_config ["lights" ][light ]["state" ]["on" ]
504
515
payload ["bri" ] = bridge_config ["lights" ][light ]["state" ]["bri" ]
505
- payload [bridge_config ["lights" ][light ]["state" ]["colormode" ]] = bridge_config ["lights" ][light ]["state" ][bridge_config ["lights" ][light ]["state" ]["colormode" ]]
516
+ if bridge_config ["lights" ][light ]["state" ]["colormode" ] in ["xy" , "ct" ]:
517
+ payload [bridge_config ["lights" ][light ]["state" ]["colormode" ]] = bridge_config ["lights" ][light ]["state" ][bridge_config ["lights" ][light ]["state" ]["colormode" ]]
518
+ elif bridge_config ["lights" ][light ]["state" ]["colormode" ] == "hs" :
519
+ payload ["hue" ] = bridge_config ["lights" ][light ]["state" ]["hue" ]
520
+ payload ["sat" ] = bridge_config ["lights" ][light ]["state" ]["sat" ]
506
521
Thread (target = sendLightRequest , args = [light , payload ]).start ()
507
522
sleep (0.5 )
508
523
print ("update status for light " + light )
@@ -518,7 +533,7 @@ def do_GET(self):
518
533
if self .path == '/description.xml' :
519
534
self .wfile .write (description ())
520
535
elif self .path == '/favicon.ico' :
521
- self .send_response ( 404 )
536
+ self .wfile . write ( "file not found" )
522
537
elif self .path .startswith ("/tradfri" ): #setup Tradfri gateway
523
538
get_parameters = parse_qs (urlparse (self .path ).query )
524
539
if "code" in get_parameters :
@@ -623,7 +638,7 @@ def do_GET(self):
623
638
self .wfile .write (json .dumps (bridge_config ))
624
639
elif len (url_pices ) == 4 : #print specified object config
625
640
if url_pices [3 ] == "lights" : #add changes from IKEA Tradfri gateway to bridge
626
- syncWithTradfri ()
641
+ syncWithLights ()
627
642
self .wfile .write (json .dumps (bridge_config [url_pices [3 ]]))
628
643
elif len (url_pices ) == 5 :
629
644
if url_pices [4 ] == "new" : #return new lights and sensors only
@@ -728,7 +743,13 @@ def do_PUT(self):
728
743
del bridge_config ["scenes" ][url_pices [4 ]]["lightstates" ][light ]["ct" ]
729
744
elif "hue" in bridge_config ["scenes" ][url_pices [4 ]]["lightstates" ][light ]:
730
745
del bridge_config ["scenes" ][url_pices [4 ]]["lightstates" ][light ]["hue" ]
731
- bridge_config ["scenes" ][url_pices [4 ]]["lightstates" ][light ][bridge_config ["lights" ][light ]["state" ]["colormode" ]] = bridge_config ["lights" ][light ]["state" ][bridge_config ["lights" ][light ]["state" ]["colormode" ]]
746
+ del bridge_config ["scenes" ][url_pices [4 ]]["lightstates" ][light ]["sat" ]
747
+ if bridge_config ["lights" ][light ]["state" ]["colormode" ] in ["ct" , "xy" ]:
748
+ bridge_config ["scenes" ][url_pices [4 ]]["lightstates" ][light ][bridge_config ["lights" ][light ]["state" ]["colormode" ]] = bridge_config ["lights" ][light ]["state" ][bridge_config ["lights" ][light ]["state" ]["colormode" ]]
749
+ elif bridge_config ["lights" ][light ]["state" ]["colormode" ] == "hs" :
750
+ bridge_config ["scenes" ][url_pices [4 ]]["lightstates" ][light ]["hue" ] = bridge_config ["lights" ][light ]["state" ]["hue" ]
751
+ bridge_config ["scenes" ][url_pices [4 ]]["lightstates" ][light ]["sat" ] = bridge_config ["lights" ][light ]["state" ]["sat" ]
752
+
732
753
if url_pices [3 ] == "sensors" :
733
754
for key , value in put_dictionary .iteritems ():
734
755
bridge_config [url_pices [3 ]][url_pices [4 ]][key ].update (value )
@@ -744,8 +765,8 @@ def do_PUT(self):
744
765
bridge_config ["lights" ][light ]["state" ]["colormode" ] = "xy"
745
766
elif "ct" in bridge_config ["scenes" ][put_dictionary ["scene" ]]["lightstates" ][light ]:
746
767
bridge_config ["lights" ][light ]["state" ]["colormode" ] = "ct"
747
- elif "hue" in bridge_config ["scenes" ][put_dictionary ["scene" ]]["lightstates" ][light ]:
748
- bridge_config ["lights" ][light ]["state" ]["colormode" ] = "hue "
768
+ elif "hue" or "sat" in bridge_config ["scenes" ][put_dictionary ["scene" ]]["lightstates" ][light ]:
769
+ bridge_config ["lights" ][light ]["state" ]["colormode" ] = "hs "
749
770
Thread (target = sendLightRequest , args = [light , bridge_config ["scenes" ][put_dictionary ["scene" ]]["lightstates" ][light ]]).start ()
750
771
update_group_stats (light )
751
772
elif "bri_inc" in put_dictionary :
@@ -779,8 +800,10 @@ def do_PUT(self):
779
800
elif url_pices [3 ] == "lights" : #state is applied to a light
780
801
Thread (target = sendLightRequest , args = [url_pices [4 ], put_dictionary ]).start ()
781
802
for key in put_dictionary .iterkeys ():
782
- if key in ["ct" , "xy" , "hue" ]: #colormode must be set by bridge
803
+ if key in ["ct" , "xy" ]: #colormode must be set by bridge
783
804
bridge_config ["lights" ][url_pices [4 ]]["state" ]["colormode" ] = key
805
+ elif key in ["hue" , "sat" ]:
806
+ bridge_config ["lights" ][url_pices [4 ]]["state" ]["colormode" ] = "hs"
784
807
update_group_stats (url_pices [4 ])
785
808
if not url_pices [4 ] == "0" : #group 0 is virtual, must not be saved in bridge configuration
786
809
try :
0 commit comments