@@ -489,7 +489,7 @@ def syncWithLights(): #update Hue Bridge lights states
489
489
else :
490
490
bridge_config ["lights" ][light ]["state" ]["reachable" ] = True
491
491
bridge_config ["lights" ][light ]["state" ].update (light_data )
492
- elif bridge_config ["lights_address" ][light ]["protocol" ] in [ "hue" , "deconz" ] :
492
+ elif bridge_config ["lights_address" ][light ]["protocol" ] == "hue" :
493
493
light_data = json .loads (sendRequest ("http://" + bridge_config ["lights_address" ][light ]["ip" ] + "/api/" + bridge_config ["lights_address" ][light ]["username" ] + "/lights/" + bridge_config ["lights_address" ][light ]["light_id" ], "GET" , "{}" , 1 ))
494
494
bridge_config ["lights" ][light ]["state" ].update (light_data ["state" ])
495
495
elif bridge_config ["lights_address" ][light ]["protocol" ] == "ikea_tradfri" :
@@ -525,6 +525,7 @@ def opened(self):
525
525
526
526
def closed (self , code , reason = None ):
527
527
print (("deconz websocket disconnected" , code , reason ))
528
+ del bridge_config ["deconz" ]["websocketport" ]
528
529
529
530
def received_message (self , m ):
530
531
print (m )
@@ -540,8 +541,17 @@ def received_message(self, m):
540
541
if "buttonevent" in message ["state" ]:
541
542
if message ["state" ]["buttonevent" ] in [2001 , 3001 , 4001 , 5001 ]:
542
543
Thread (target = longPressButton , args = [bridge_sensor_id , message ["state" ]["buttonevent" ]]).start ()
544
+ if "presence" in message ["state" ] and message ["state" ]["presence" ] and "virtual_light" in bridge_config ["alarm_config" ] and bridge_config ["lights" ][bridge_config ["alarm_config" ]["virtual_light" ]]["state" ]["on" ]:
545
+ sendEmail (bridge_config ["sensors" ][bridge_sensor_id ]["name" ])
546
+ bridge_config ["alarm_config" ]["virtual_light" ]
543
547
elif "config" in message :
544
548
bridge_config ["sensors" ][bridge_sensor_id ]["config" ].update (message ["config" ])
549
+ elif message ["r" ] == "lights" :
550
+ bridge_light_id = bridge_config ["deconz" ]["lights" ][message ["id" ]]["bridgeid" ]
551
+ if "state" in message :
552
+ bridge_config ["lights" ][bridge_light_id ]["state" ].update (message ["state" ])
553
+ updateGroupStats (bridge_light_id )
554
+
545
555
546
556
except Exception as e :
547
557
print ("unable to process the request" + str (e ))
@@ -554,34 +564,32 @@ def received_message(self, m):
554
564
ws .close ()
555
565
556
566
def scanDeconz ():
557
- if bridge_config ["deconz" ]["enabled" ]:
558
- if "port" in bridge_config ["deconz" ]:
559
- port = bridge_config ["deconz" ]["port" ]
560
- else :
561
- port = 8080
562
-
567
+ if not bridge_config ["deconz" ]["enabled" ]:
563
568
if "username" not in bridge_config ["deconz" ]:
564
569
try :
565
- registration = json .loads (sendRequest ("http://127.0.0.1:" + str (port ) + "/api" , "POST" , "{\" username\" : \" 283145a4e198cc6535\" , \" devicetype\" :\" Hue Emulator\" }" ))
570
+ registration = json .loads (sendRequest ("http://127.0.0.1:" + str (bridge_config [ "deconz" ][ " port" ] ) + "/api" , "POST" , "{\" username\" : \" 283145a4e198cc6535\" , \" devicetype\" :\" Hue Emulator\" }" ))
566
571
except :
567
572
print ("registration fail, is the link button pressed?" )
568
573
return
569
574
if "success" in registration [0 ]:
570
575
bridge_config ["deconz" ]["username" ] = registration [0 ]["success" ]["username" ]
571
- deconz_config = json .loads (sendRequest ("http://127.0.0.1:" + str (port ) + "/api/" + bridge_config ["deconz" ]["username" ] + "/config" , "GET" , "{}" ))
576
+ bridge_config ["deconz" ]["enabled" ] = True
577
+ if "username" in bridge_config ["deconz" ]:
578
+ deconz_config = json .loads (sendRequest ("http://127.0.0.1:" + str (bridge_config ["deconz" ]["port" ]) + "/api/" + bridge_config ["deconz" ]["username" ] + "/config" , "GET" , "{}" ))
572
579
bridge_config ["deconz" ]["websocketport" ] = deconz_config ["websocketport" ]
573
580
registered_deconz_lights = []
574
581
for light in bridge_config ["lights_address" ]:
575
582
if bridge_config ["lights_address" ][light ]["protocol" ] == "deconz" :
576
583
registered_deconz_lights .append ( bridge_config ["lights_address" ][light ]["light_id" ] )
577
- deconz_lights = json .loads (sendRequest ("http://127.0.0.1:" + str (port ) + "/api/" + bridge_config ["deconz" ]["username" ] + "/lights" , "GET" , "{}" ))
584
+ deconz_lights = json .loads (sendRequest ("http://127.0.0.1:" + str (bridge_config [ "deconz" ][ " port" ] ) + "/api/" + bridge_config ["deconz" ]["username" ] + "/lights" , "GET" , "{}" ))
578
585
for light in deconz_lights :
579
586
if light not in registered_deconz_lights :
580
587
new_light_id = nextFreeId ("lights" )
581
588
print ("register new light " + new_light_id )
582
589
bridge_config ["lights" ][new_light_id ] = deconz_lights [light ]
583
- bridge_config ["lights_address" ][new_light_id ] = {"username" : bridge_config ["deconz" ]["username" ], "light_id" : light , "ip" : "127.0.0.1:" + str (port ), "protocol" : "deconz" }
584
- deconz_sensors = json .loads (sendRequest ("http://127.0.0.1:" + str (port ) + "/api/" + bridge_config ["deconz" ]["username" ] + "/sensors" , "GET" , "{}" ))
590
+ bridge_config ["lights_address" ][new_light_id ] = {"username" : bridge_config ["deconz" ]["username" ], "light_id" : light , "ip" : "127.0.0.1:" + str (bridge_config ["deconz" ]["port" ]), "protocol" : "deconz" }
591
+ bridge_config ["deconz" ]["lights" ][light ] = {"bridgeid" : new_light_id }
592
+ deconz_sensors = json .loads (sendRequest ("http://127.0.0.1:" + str (bridge_config ["deconz" ]["port" ]) + "/api/" + bridge_config ["deconz" ]["username" ] + "/sensors" , "GET" , "{}" ))
585
593
for sensor in deconz_sensors :
586
594
if sensor not in bridge_config ["deconz" ]["sensors" ]:
587
595
new_sensor_id = nextFreeId ("sensors" )
@@ -593,6 +601,9 @@ def scanDeconz():
593
601
print ("register TRADFRI remote control as Philips Motion Sensor" )
594
602
newMotionSensorId = addHueMotionSensor ()
595
603
bridge_config ["deconz" ]["sensors" ][sensor ] = {"bridgeid" : newMotionSensorId }
604
+ if "websocketport" in bridge_config ["deconz" ]:
605
+ print ("Starting deconz websocket" )
606
+ Thread (target = websocketClient ).start ()
596
607
597
608
598
609
@@ -676,7 +687,7 @@ def webformIndex():
676
687
content += "<label for=\" " + deconzSensor + "\" >" + bridge_config ["sensors" ][bridge_config ["deconz" ]["sensors" ][deconzSensor ]["bridgeid" ]]["name" ] + "</label>\n "
677
688
content += "<select id=\" " + deconzSensor + "\" name=\" " + bridge_config ["deconz" ]["sensors" ][deconzSensor ]["bridgeid" ] + "\" >\n "
678
689
for group in bridge_config ["groups" ].iterkeys ():
679
- if bridge_config ["deconz" ]["sensors" ][deconzSensor ]["room" ] == group :
690
+ if "room" in bridge_config [ "deconz" ][ "sensors" ][ deconzSensor ] and bridge_config ["deconz" ]["sensors" ][deconzSensor ]["room" ] == group :
680
691
content += "<option value=\" " + group + "\" selected>" + bridge_config ["groups" ][group ]["name" ] + "</option>\n "
681
692
else :
682
693
content += "<option value=\" " + group + "\" >" + bridge_config ["groups" ][group ]["name" ] + "</option>\n "
@@ -840,7 +851,6 @@ def do_GET(self):
840
851
self .wfile .write (webform_hue ())
841
852
elif self .path .startswith ("/deconz" ): #setup imported deconz sensors
842
853
get_parameters = parse_qs (urlparse (self .path ).query )
843
- pprint (get_parameters )
844
854
#clean all rules related to deconz Switches
845
855
sensorsResourcelinks = []
846
856
if get_parameters :
@@ -1148,15 +1158,14 @@ def run(server_class=HTTPServer, handler_class=S):
1148
1158
if __name__ == "__main__" :
1149
1159
if bridge_config ["deconz" ]["enabled" ]:
1150
1160
scanDeconz ()
1151
- Thread (target = websocketClient ).start ()
1152
1161
try :
1153
1162
updateAllLights ()
1154
1163
Thread (target = ssdpSearch ).start ()
1155
1164
Thread (target = ssdpBroadcast ).start ()
1156
1165
Thread (target = schedulerProcessor ).start ()
1157
1166
run ()
1158
- except :
1159
- print ("server stopped" )
1167
+ except Exception as e :
1168
+ print ("server stopped " + str ( e ) )
1160
1169
finally :
1161
1170
run_service = False
1162
1171
saveConfig ()
0 commit comments