@@ -279,6 +279,26 @@ def exception_handler(loop, context):
279279 # mapping from mac address to widget
280280 self .device_widgets = {}
281281
282+ async def process_devices ():
283+ # queue to process discovered devices
284+ #
285+ # this must be called from within the loop:
286+ # https://stackoverflow.com/a/53724990/2796349
287+ self .device_queue = asyncio .Queue ()
288+
289+
290+ while True :
291+ new_item = await self .device_queue .get ()
292+ logging .info ("Discovered device: {}" .format (repr (new_item )))
293+ try :
294+ await self .add_device (new_item )
295+ except Exception as exc :
296+ logging .error (exc )
297+
298+ self .event_loop .create_task (process_devices ())
299+
300+ # TODO make sure that self.device_queue exists before exiting this function
301+
282302 self .refresh_button = tkinter .Button (
283303 self , text = "Refresh" , command = self .start_refresh
284304 )
@@ -313,7 +333,7 @@ async def _do_refresh(self):
313333 self .refresh_button ["state" ] = tkinter .DISABLED
314334 self .refresh_button ["text" ] = "Refreshing..."
315335 await self .clear_devices ()
316- await kasa .Discover .discover (on_discovered = self .add_device )
336+ await kasa .Discover .discover (on_discovered = self .device_queue . put )
317337 self .refresh_button ["state" ] = tkinter .NORMAL
318338 self .refresh_button ["text" ] = "Refresh"
319339
0 commit comments