Skip to content

Commit e23c35b

Browse files
Add files via upload
1 parent e354587 commit e23c35b

File tree

1 file changed

+72
-15
lines changed

1 file changed

+72
-15
lines changed

pkg/buttoninput.py

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,18 @@ def __init__(self, verbose=False):
124124

125125
self.save_persistent_data()
126126

127-
self.scan_devices()
127+
128128

129129
self.generate_things()
130130

131-
self.asyncio_loop = asyncio.get_event_loop()
131+
#self.asyncio_loop = asyncio.get_event_loop()
132+
133+
#self.asyncio_loop = asyncio.new_event_loop()
134+
#asyncio.set_event_loop(self.asyncio_loop)
135+
136+
self.scan_devices()
132137

133-
self.asyncio_loop.run_forever()
138+
#self.asyncio_loop.run_forever()
134139

135140
# The addon is now ready
136141
self.ready = True
@@ -230,6 +235,25 @@ def scan_devices(self):
230235
self.inputs = []
231236
self.input_data = {}
232237

238+
try:
239+
if self.asyncio_loop:
240+
241+
for task in asyncio.Task.all_tasks():
242+
task.cancel()
243+
244+
self.asyncio_loop.stop()
245+
print("scan_devices: closed old loop")
246+
247+
except Exception as ex:
248+
if self.DEBUG:
249+
print("caught error cancelling asyncio tasks: " + str(ex))
250+
251+
self.asyncio_loop = asyncio.new_event_loop()
252+
asyncio.set_event_loop(self.asyncio_loop)
253+
254+
255+
256+
233257
self.inputs = [evdev.InputDevice(path) for path in evdev.list_devices()]
234258

235259
if self.DEBUG:
@@ -296,9 +320,28 @@ def scan_devices(self):
296320
except Exception as ex:
297321
if self.DEBUG:
298322
print("caught ERROR looping over input events: " + str(ex))
323+
324+
"""
325+
if 'There is no current event loop in thread' in str(ex):
326+
if self.DEBUG:
327+
print("atempting to create new event loop")
328+
329+
try:
330+
self.asyncio_loop = asyncio.new_event_loop()
331+
asyncio.set_event_loop(self.asyncio_loop)
332+
333+
except Exception as ex:
334+
if self.DEBUG:
335+
print("double error: failed to create new event loop after catching errot that there is no loop: " + str(ex))
336+
"""
337+
338+
299339

300340
event_index += 1
301341

342+
if len(self.inputs):
343+
self.asyncio_loop.run_forever()
344+
302345
return
303346

304347

@@ -383,7 +426,11 @@ async def print_events(self,device):
383426
thingy = self.get_device(str(self.input_data[device.path]['nice_name']))
384427
#print("thingy: ", thingy)
385428

386-
if thingy:
429+
if not thingy:
430+
431+
#print("No thing")
432+
pass
433+
elif thingy != None:
387434

388435
#properties_dict = thingy.get_property_descriptions()
389436
#print("in this properties_dict: ", properties_dict.keys())
@@ -414,7 +461,8 @@ async def print_events(self,device):
414461

415462

416463
elif event_category.startswith('key event') and 'EV_KEY' in self.input_data[device.path]['capabilities']:
417-
#print("event_category RAW: ", event_category)
464+
if self.DEBUG:
465+
print("event_category RAW: ", event_category)
418466

419467
key_code = event_category
420468
short_code_detail = ''
@@ -496,16 +544,17 @@ async def print_events(self,device):
496544
#properties_dict = thingy.get_property_descriptions()
497545
#print("button properties_dict: ", properties_dict)
498546
#print("button short_code: ", short_code)
499-
500-
propy = thingy.find_property(short_code)
501-
if propy:
502-
#print("found button short_code property too")
503-
if propy.set_cached_value_and_notify(bool(event.value)):
504-
#print("OK, button value set")
505-
pass
506-
else:
507-
#print("FAILED TO SET BUTTON VALUE")
508-
pass
547+
548+
if thingy:
549+
propy = thingy.find_property(short_code)
550+
if propy:
551+
#print("found button short_code property too")
552+
if propy.set_cached_value_and_notify(bool(event.value)):
553+
#print("OK, button value set")
554+
pass
555+
else:
556+
#print("FAILED TO SET BUTTON VALUE")
557+
pass
509558

510559

511560
#try:
@@ -544,6 +593,14 @@ def unload(self):
544593
if self.DEBUG:
545594
print("Error setting status on thing: " + str(ex))
546595

596+
try:
597+
for task in asyncio.Task.all_tasks():
598+
task.cancel()
599+
except Exception as ex:
600+
if self.DEBUG:
601+
print("caught error cancelling asyncio tasks: " + str(ex))
602+
603+
547604
try:
548605
self.asyncio_loop.close()
549606
except Exception as ex:

0 commit comments

Comments
 (0)