-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyWeMoGUIDeviceManager.py
More file actions
68 lines (55 loc) · 2.51 KB
/
PyWeMoGUIDeviceManager.py
File metadata and controls
68 lines (55 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import logging
import threading
from queue import Queue
import pywemo
class PyWeMoGUIDeviceManager:
def __init__(self):
self.logger = logging.getLogger(__name__)
self.current_devices = []
def discover_devices(self, queue: Queue):
'''
This function will discover devices on the local network, using a thread to not block the caller.
To recieve data, **you must check a passed queue** for new data. Otherwise, this function will appear to do nothing.
:param queue: Pass a fresh Queue object, it will have the pywemo devices array put into it once discovery is complete
:type queue: Queue
'''
def _device_discovery(queue: Queue): #called in discover but is not discover itself
self.logger.debug("Device discovery thread started, now discovering devices")
self.current_devices = pywemo.discover_devices()
queue.put(self.current_devices)
self.logger.debug("Clearing device list before running discovery to avoid stale objects")
self.current_devices.clear() # clear devices to avoid stale objects
scanThread = threading.Thread(target=_device_discovery, args=(queue,)) #setup thread
scanThread.daemon=True
scanThread.start() # start actual discovery in thread
#def list_devices(self):
# #return [device.name for device in self.current_devices] #### this was never used
# pass
def sort_list_by_name(self):
self.current_devices.sort(key=lambda device: device.name)
def get_device_by_name(self, name):
'''
Returns the device with the specified name
:param index: The name of the device in the table to return
'''
for device in self.current_devices:
if device.name == name:
return device
return None
def get_device_by_ip(self, ip):
'''
Returns the device with the specified IP
:param index: The IP of the device in the table to return
'''
for device in self.current_devices:
if device.host == ip:
return device
return None
def get_device_by_array_index(self, index: int): # Probably more performant than by name or ip
'''
Returns the device with the specified index
:param index: The index of the device in the table to return
'''
if 0 <= index < len(self.current_devices):
return self.current_devices[index]
return None