-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmyopenhab.py
123 lines (96 loc) · 2.97 KB
/
myopenhab.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import sys
import json
import requests
import time
import urlparse
import sys
def batteryLevel( level ):
"This returns a string value for a battery level"
l = 'empty'
try:
v = float(level)
except:
v = 0
if (v < 5):
l = 'empty'
elif (v < 20):
l = 'low'
elif (v < 90):
l = 'medium'
elif (v < 100):
l = 'high'
else:
l = 'full'
return l
def mapValues(k, map):
v = "unkown"
if(str(k) in map):
v = map[str(k)]
return v
def getJSONValue(obj, path):
pathOK = True
subObj = obj
v = ""
try:
for k in path:
if(isinstance(k, basestring)):
if(str(k) in subObj):
subObj = subObj[k]
v = subObj
else:
pathOK = False
break
else:
subObj = subObj[k]
v = subObj
except:
pathOK = False
if (pathOK):
return v
else:
return None
class openhab(object):
"""
A wrapper for the OpenHab REST API.
"""
def __init__(self):
self.openhab_ip = "127.0.0.1:8080"
def sendCommand(self, item, state):
"""
Update State from Item
"""
try:
if (isinstance(state, basestring)):
s = state.encode('utf-8')
elif (state is None):
s = "NULL"
else:
s = str(state)
r = requests.put('http://' + str(self.openhab_ip) + '/rest/items/' + item + '/state', s)
if(r.status_code == 202):
print "Successfully posted state to OpenHab: \033[0;37m" + str(item) + " \033[0m= \033[0;36m" + s + "\033[0m"
elif(r.status_code == 400):
print "Error posting state to OpenHab: \033[0;31m" + str(item) + " (HTTP Response " + str(r.json()['error']['message']) + ")\033[0m"
else:
print "Error posting state to OpenHab: \033[0;31m" + str(item) + " (HTTP Response " + str(r.status_code) + ")\033[0m"
except:
print "OpenHab: \033[0;31mError posting state:" + str(sys.exc_info()[1]) + "\033[0m"
def getState(self, item):
"""
Get State for Item
"""
try:
r = requests.get('http://' + str(self.openhab_ip) + '/rest/items/' + item + '/state')
if(r.status_code == 200):
print "Successfully got state from OpenHab: \033[0;37m" + str(item) + "\033[0m"
state = str(r.content)
if (state == ""):
state = "NULL"
return state
elif(r.status_code == 400):
print "Error posting state to OpenHab: \033[0;31m" + str(item) + " (HTTP Response " + str(r.json()['error']['message']) + ")\033[0m"
else:
print "Error getting state from OpenHab: \033[0;31m" + str(item) + " (HTTP Response " + str(r.status_code) + ")\033[0m"
return ""
except:
print "OpenHab: \033[0;31mError getting state:" + str(sys.exc_info()[1]) + "\033[0m"