@@ -153,7 +153,7 @@ def __init__(self, verbose=False):
153153
154154
155155
156- device_list_before = [] # dict ([(f, None) for f in os.listdir (self.input_path)])
156+ device_list_before = []
157157 if self .DEBUG :
158158 print (" device_list_before: " , device_list_before )
159159 print ("self.auto_detect_new_devices: " , self .auto_detect_new_devices )
@@ -168,6 +168,7 @@ def __init__(self, verbose=False):
168168 # The addon is now ready
169169 self .ready = True
170170
171+ self .first_run = True
171172 while self .running :
172173
173174 #device_list_now = dict ([(f, None) for f in os.listdir (self.input_path)])
@@ -188,6 +189,14 @@ def __init__(self, verbose=False):
188189 print ("devices added: " , ", " .join (added ))
189190 print ("devices removed: " , ", " .join (removed ))
190191
192+ if self .first_run == False :
193+ if added :
194+ self .send_pairing_prompt ("A button input device connected" )
195+
196+ elif removed :
197+ self .send_pairing_prompt ("A button input device disconnected" )
198+
199+ self .first_run = False
191200
192201 #time.sleep(1)
193202 #self.inputs = [evdev.InputDevice(path) for path in evdev.list_devices()]
@@ -208,16 +217,18 @@ def __init__(self, verbose=False):
208217 for device_path in added :
209218 if self .DEBUG :
210219 print ("" )
211- print (str (event_index ) + ". " )
220+ print ("ADDED DEVICE: " + str (event_index ) + ". " )
212221
213222
214223
215224 try :
216225
217226 device = evdev .InputDevice (device_path )
218227
219- if not str (device .path ) in self .input_data .keys ():
220- print ("adding device to self.input_data: " , str (device .path ))
228+
229+ if device and str (device .path ) not in self .input_data .keys ():
230+ if self .DEBUG :
231+ print ("adding device to self.input_data: " , str (device .path ))
221232 self .input_data [ str (device .path ) ] = {}
222233
223234 if self .DEBUG :
@@ -267,7 +278,9 @@ def __init__(self, verbose=False):
267278 #asyncio.ensure_future(self.print_events(device))
268279 event_loop = asyncio .get_event_loop ()
269280 asyncio .run_coroutine_threadsafe (self .print_events (device ), event_loop )
270- print ("added new coroutine to asyncio loop" )
281+ if self .DEBUG :
282+ print ("added new coroutine to asyncio loop" )
283+
271284 except Exception as ex :
272285 if self .DEBUG :
273286 print ("caught ERROR looping over input events: " + str (ex ))
@@ -307,7 +320,8 @@ def __init__(self, verbose=False):
307320 time .sleep (1 )
308321
309322 if self .auto_detect_new_devices == False :
310- print ("not auto-detecting changes" )
323+ if self .DEBUG :
324+ print ("not auto-detecting changes" )
311325 break
312326
313327
@@ -416,33 +430,40 @@ def generate_things(self):
416430
417431
418432 def run_asyncio_forever (self ):
419- print ("in run_asyncio_forever" )
433+ if self .DEBUG :
434+ print ("in run_asyncio_forever" )
420435 while self .running :
421436 time .sleep (0.5 )
422437
423438
424439 if self .running :
425440 if self .asyncio_loop == None :
426- print ("run_asyncio_forever: creating asyncio loop" )
441+ if self .DEBUG :
442+ print ("run_asyncio_forever: creating asyncio loop" )
427443 self .asyncio_loop = asyncio .new_event_loop ()
428444 asyncio .set_event_loop (self .asyncio_loop )
429445
430446 if self .asyncio_loop :
431- print ("starting asyncio loop" )
447+ if self .DEBUG :
448+ print ("starting asyncio loop" )
432449 #self.asyncio_loop.run_forever()
433450 try :
434451 self .asyncio_loop .run_forever ()
435452 except Exception as ex :
436- print ("un_asyncio_forever: caught error in run_forever: " , ex )
453+ if self .DEBUG :
454+ print ("un_asyncio_forever: caught error in run_forever: " , ex )
437455 finally :
438- print ("run_asyncio_forever: finally: asyncio loop exited" )
456+ if self .DEBUG :
457+ print ("run_asyncio_forever: finally: asyncio loop exited" )
439458 self .asyncio_loop .run_until_complete (loop .shutdown_asyncgens ())
440459 self .asyncio_loop .close ()
441460
442461 #run_until_complete(task)
443- print ("run_asyncio_forever: Asyncio loop stopped!" )
462+ if self .DEBUG :
463+ print ("run_asyncio_forever: Asyncio loop stopped!" )
444464 else :
445- print ("run_asyncio_forever: not starting asyncio loop!" )
465+ if self .DEBUG :
466+ print ("run_asyncio_forever: not starting asyncio loop!" )
446467
447468 #time.sleep(0.5)
448469
@@ -457,12 +478,14 @@ def run_asyncio_forever(self):
457478
458479
459480 def scan_devices (self ):
460- print ("\n 0_0_0_0_\n in scan_devices" )
481+ if self .DEBUG :
482+ print ("\n 0_0_0_0_\n in scan_devices" )
461483 #scan_devices_response = run_command('ls -l /dev/input/event*')
462484 #print("raw scan_devices_response: " + str(scan_devices_response))
463485
464486
465- print ("BLOCKED" )
487+ if self .DEBUG :
488+ print ("scan_devices is BLOCKED" )
466489 return
467490
468491
@@ -488,10 +511,12 @@ def scan_devices(self):
488511 except Exception as ex :
489512 print ("scan_devices: failed to stop asyncio tasks first: " , ex )
490513
491- print ('scan_devices: stopping asyncio loop' )
514+ if self .DEBUG :
515+ print ('scan_devices: stopping asyncio loop' )
492516 try :
493517 self .asyncio_loop .stop ()
494- print ("scan_devices: closed old loop" )
518+ if self .DEBUG :
519+ print ("scan_devices: closed old loop" )
495520 except Exception as ex :
496521 if self .DEBUG :
497522 print ("scan_devices: failed to stop loop: " + str (ex ))
@@ -506,7 +531,8 @@ def scan_devices(self):
506531
507532
508533 if self .asyncio_loop == None :
509- print ("scan_devices: creating asyncio loop" )
534+ if self .DEBUG :
535+ print ("scan_devices: creating asyncio loop" )
510536 self .asyncio_loop = asyncio .new_event_loop ()
511537 asyncio .set_event_loop (self .asyncio_loop )
512538
@@ -632,7 +658,17 @@ async def print_events(self,device):
632658 print ("ERROR, device path not in self.input_data: " + str (device .path ))
633659
634660 #if event.type == ecodes.EV_KEY:
635- # print("detected a key press")
661+ #
662+
663+
664+
665+ if 'nice_name' not in self .input_data [device .path ]:
666+ if self .DEBUG :
667+ print ("ERROR, device has no NiceName" )
668+ return
669+
670+ if self .DEBUG :
671+ print ("detected a key press" )
636672
637673
638674 if event_category .startswith ('synchronization event' ):
@@ -800,8 +836,9 @@ async def print_events(self,device):
800836 #print("button short_code: " + str(short_code))
801837 #print("button short_code_detail: " + str(short_code_detail))
802838
803- if 'children' in self .input_data [device .path ]['capabilities' ]['EV_KEY' ] and short_code in self .input_data [device .path ]['capabilities' ]['EV_KEY' ]['children' ]:
804- #print("found button short_code in capabilities: ", short_code)
839+ if 'children' in self .input_data [device .path ]['capabilities' ]['EV_KEY' ] and short_code in self .input_data [device .path ]['capabilities' ]['EV_KEY' ]['children' ] and 'nice_name' in self .input_data [device .path ]:
840+ if self .DEBUG :
841+ print ("found button short_code in capabilities: " , short_code )
805842
806843 self .input_data [device .path ]['capabilities' ]['EV_KEY' ]['children' ][short_code ]['last_time' ] = int (event .sec )
807844
@@ -819,6 +856,8 @@ async def print_events(self,device):
819856 #print("button short_code: ", short_code)
820857
821858 if thingy :
859+ if self .DEBUG :
860+ print ("Found the matching thing for the button press, based on device NiceName: " + str (self .input_data [device .path ]['nice_name' ]));
822861 propy = thingy .find_property (short_code )
823862 if propy :
824863 #print("found button short_code property too")
@@ -828,7 +867,9 @@ async def print_events(self,device):
828867 else :
829868 #print("FAILED TO SET BUTTON VALUE")
830869 pass
831-
870+ else :
871+ if self .DEBUG :
872+ print ("button press, but no thing found for device NiceName: " + str (self .input_data [device .path ]['nice_name' ]));
832873
833874 #try:
834875 # self.devices[ str(self.input_data[device.path]['nice_name']) ].get_props()[short_code].update(bool(event.value)
@@ -849,7 +890,8 @@ async def print_events(self,device):
849890 try :
850891 if 'No such device' in str (ex ) and str (device .path ) in self .input_data .keys ():
851892
852- print ("device disconnected: " , str (self .input_data [device .path ]['nice_name' ]))
893+ if self .DEBUG :
894+ print ("device disconnected: " , str (self .input_data [device .path ]['nice_name' ]))
853895 thingy = self .get_device (str (self .input_data [device .path ]['nice_name' ]))
854896 #print("button thingy: ", thingy)
855897
@@ -858,19 +900,22 @@ async def print_events(self,device):
858900 #print("button short_code: ", short_code)
859901
860902 if thingy :
861- print ("setting thing to disconnected" )
903+ if self .DEBUG :
904+ print ("setting thing to disconnected" )
862905 thingy .connected = False
863906 thingy .connected_notify (True )
864907
865908 del self .input_data [ str (device .path ) ]
866909
867910 except Exception as ex :
868- print ("caught error trying to remove device from self.input_data dict: " , ex )
911+ if self .DEBUG :
912+ print ("caught error trying to remove device from self.input_data dict: " , ex )
869913
870914 try :
871915 device .close ()
872916 except Exception as ex :
873- print ("caught error trying to close evdev device: " , ex )
917+ if self .DEBUG :
918+ print ("caught error trying to close evdev device: " , ex )
874919
875920
876921 """
0 commit comments