forked from ropez/wemo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wemo.py
77 lines (63 loc) · 2.22 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
71
72
73
74
75
76
77
from miranda import upnp, msearch
conn = upnp(False, False, None, [])
conn.TIMEOUT = 2 # wait for 2 seconds
msearch(4, [0, 'Belkin', 'service', 'basicevent'], conn)
#msearch(4, [0, 'Belkin', 'device', 'controllee'], conn)
SWITCHES = []
# populate all the host info, for every uPNP device on the network
for index in conn.ENUM_HOSTS:
hostInfo = conn.ENUM_HOSTS[index]
if hostInfo['dataComplete'] == False:
xmlHeaders, xmlData = conn.getXML(hostInfo['xmlFile'])
conn.getHostInfo(xmlData,xmlHeaders,index)
for index in conn.ENUM_HOSTS:
try:
if conn.ENUM_HOSTS[index]['deviceList']['controllee']['modelName'] == 'Socket':
SWITCHES = [index]
except KeyError:
pass
def _send(action, args=None):
if not args:
args = {}
host_info = conn.ENUM_HOSTS[SWITCHES[0]]
device_name = 'controllee'
service_name = 'basicevent'
controlURL = host_info['proto'] + host_info['name']
controlURL2 = host_info['deviceList'][device_name]['services'][service_name]['controlURL']
if not controlURL.endswith('/') and not controlURL2.startswith('/'):
controlURL += '/'
controlURL += controlURL2
resp = conn.sendSOAP(
host_info['name'],
'urn:Belkin:service:basicevent:1',
controlURL,
action,
args
)
return resp
def get():
"""
Gets the value of the first switch that it finds
"""
resp = _send('GetBinaryState')
tagValue = conn.extractSingleTag(resp, 'BinaryState')
return True if tagValue == '1' else False
def on():
"""
Turns on the first switch that it finds.
BinaryState is set to 'Error' in the case that it was already on.
"""
resp = _send('SetBinaryState', {'BinaryState': (1, 'Boolean')})
tagValue = conn.extractSingleTag(resp, 'BinaryState')
return True if tagValue in ['1', 'Error'] else False
def off():
"""
Turns off the first switch that it finds.
BinaryState is set to 'Error' in the case that it was already off.
"""
resp = _send('SetBinaryState', {'BinaryState': (0, 'Boolean')})
tagValue = conn.extractSingleTag(resp, 'BinaryState')
return True if tagValue in ['0', 'Error'] else False
def toggle():
isOn = get()
off() if isOn else on()