@@ -161,14 +161,14 @@ def rules_processor(scheduler=False):
161161 for action in bridge_config ["rules" ][rule ]["actions" ]:
162162 Thread (target = sendRequest , args = ["/api/" + bridge_config ["rules" ][rule ]["owner" ] + action ["address" ], action ["method" ], json .dumps (action ["body" ])]).start ()
163163
164- def sendRequest (url , method , data ):
164+ def sendRequest (url , method , data , time_out = 3 ):
165165 if not url .startswith ( 'http://' ):
166166 url = "http://127.0.0.1" + url
167167 opener = urllib2 .build_opener (urllib2 .HTTPHandler )
168168 request = urllib2 .Request (url , data = data )
169169 request .add_header ("Content-Type" ,'application/json' )
170170 request .get_method = lambda : method
171- response = opener .open (request , timeout = 3 ).read ()
171+ response = opener .open (request , timeout = time_out ).read ()
172172 return response
173173
174174def convert_xy (x , y , bri ): #needed for milight hub that don't work with xy values
@@ -246,7 +246,7 @@ def sendLightRequest(light, data):
246246 else :
247247 url += "&" + key + "=" + str (value )
248248 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"
250250 method = 'PUT'
251251 payload = data
252252 elif lights_address [light ]["protocol" ] == "milight" : #MiLight bulb
@@ -358,9 +358,21 @@ def scan_for_lights(): #scan for ESP8266 lights and strips
358358 except Exception , e :
359359 print (ip + " is unknow device " + str (e ))
360360
361- def syncWithTradfri (): #update Hue Bridge lights states from Ikea Tradfri gateway
361+ def syncWithLights (): #update Hue Bridge lights states
362362 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" :
364376 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 ])
365377 bridge_config ["lights" ][light ]["state" ]["on" ] = bool (light_stats ["3311" ][0 ]["5850" ])
366378 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
371383 elif light_stats ["3311" ][0 ]["5706" ] == "efd275" :
372384 bridge_config ["lights" ][light ]["state" ]["ct" ] = 470
373385
374-
375386def description ():
376387 return """<root xmlns=\" urn:schemas-upnp-org:device-1-0\" >
377388<specVersion>
@@ -502,7 +513,11 @@ def update_all_lights():
502513 payload = {}
503514 payload ["on" ] = bridge_config ["lights" ][light ]["state" ]["on" ]
504515 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" ]
506521 Thread (target = sendLightRequest , args = [light , payload ]).start ()
507522 sleep (0.5 )
508523 print ("update status for light " + light )
@@ -518,7 +533,7 @@ def do_GET(self):
518533 if self .path == '/description.xml' :
519534 self .wfile .write (description ())
520535 elif self .path == '/favicon.ico' :
521- self .send_response ( 404 )
536+ self .wfile . write ( "file not found" )
522537 elif self .path .startswith ("/tradfri" ): #setup Tradfri gateway
523538 get_parameters = parse_qs (urlparse (self .path ).query )
524539 if "code" in get_parameters :
@@ -623,7 +638,7 @@ def do_GET(self):
623638 self .wfile .write (json .dumps (bridge_config ))
624639 elif len (url_pices ) == 4 : #print specified object config
625640 if url_pices [3 ] == "lights" : #add changes from IKEA Tradfri gateway to bridge
626- syncWithTradfri ()
641+ syncWithLights ()
627642 self .wfile .write (json .dumps (bridge_config [url_pices [3 ]]))
628643 elif len (url_pices ) == 5 :
629644 if url_pices [4 ] == "new" : #return new lights and sensors only
@@ -728,7 +743,13 @@ def do_PUT(self):
728743 del bridge_config ["scenes" ][url_pices [4 ]]["lightstates" ][light ]["ct" ]
729744 elif "hue" in bridge_config ["scenes" ][url_pices [4 ]]["lightstates" ][light ]:
730745 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+
732753 if url_pices [3 ] == "sensors" :
733754 for key , value in put_dictionary .iteritems ():
734755 bridge_config [url_pices [3 ]][url_pices [4 ]][key ].update (value )
@@ -744,8 +765,8 @@ def do_PUT(self):
744765 bridge_config ["lights" ][light ]["state" ]["colormode" ] = "xy"
745766 elif "ct" in bridge_config ["scenes" ][put_dictionary ["scene" ]]["lightstates" ][light ]:
746767 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 "
749770 Thread (target = sendLightRequest , args = [light , bridge_config ["scenes" ][put_dictionary ["scene" ]]["lightstates" ][light ]]).start ()
750771 update_group_stats (light )
751772 elif "bri_inc" in put_dictionary :
@@ -779,8 +800,10 @@ def do_PUT(self):
779800 elif url_pices [3 ] == "lights" : #state is applied to a light
780801 Thread (target = sendLightRequest , args = [url_pices [4 ], put_dictionary ]).start ()
781802 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
783804 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"
784807 update_group_stats (url_pices [4 ])
785808 if not url_pices [4 ] == "0" : #group 0 is virtual, must not be saved in bridge configuration
786809 try :
0 commit comments