-
Notifications
You must be signed in to change notification settings - Fork 1
/
wemo.py
70 lines (56 loc) · 1.98 KB
/
wemo.py
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
69
70
from miranda import upnp, msearch
conn = upnp(False,False,None,[])
class WeMo(object):
hostName = None
controlURL = None
def __init__(self, hostInfo={}, controlURL=None):
if hostInfo:
self.hostName = hostInfo['name']
self.controlURL = '%s%s%s' % (hostInfo['proto'], hostInfo['name'], hostInfo['deviceList']['controllee']['services']['basicevent']['controlURL'])
elif controlURL:
self.hostName = controlURL.split('/')[2] # TODO: urlparse
self.controlURL = controlURL
def __str__(self):
return self.friendlyName
def send(self, action, args={}):
resp = conn.sendSOAP(self.hostName, 'urn:Belkin:service:basicevent:1', self.controlURL, action, args)
if conn.extractSingleTag(resp, "UPnPError"):
raise Exception(resp)
return resp
@property
def friendlyName(self):
return conn.extractSingleTag(self.send('GetFriendlyName'), 'FriendlyName')
@friendlyName.setter
def friendlyName(self, value):
self.send('ChangeFriendlyName', {'FriendlyName': (value, 'String')})
@property
def binaryState(self):
return conn.extractSingleTag(self.send('GetBinaryState'), 'BinaryState') in ['1']
@binaryState.setter
def binaryState(self, value):
self.send('SetBinaryState', {'BinaryState': (int(value), 'Boolean')})
@property
def iconURL(self):
return conn.extractSingleTag(self.send('GetIconURL'), 'URL')
@property
def logFileURL(self):
return conn.extractSingleTag(self.send('GetLogFileURL'), 'LOGURL')
def search():
conn.TIMEOUT = 2
conn.UNIQ = True
msearch(0, 0, conn)
wemos = []
for index, hostInfo in conn.ENUM_HOSTS.iteritems():
if hostInfo['dataComplete'] == False:
xmlHeaders, xmlData = conn.getXML(hostInfo['xmlFile'])
conn.getHostInfo(xmlData, xmlHeaders, index)
try:
if conn.ENUM_HOSTS[index]['deviceList']['controllee']['modelName'] == 'Socket':
wemos.append(WeMo(hostInfo))
except Exception, e:
pass
return wemos
if __name__ == "__main__":
wemos = search()
for wemo in wemos:
print "[*]" if wemo.binaryState else "[ ]", wemo.friendlyName