-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloxone.py
More file actions
123 lines (103 loc) · 3.87 KB
/
loxone.py
File metadata and controls
123 lines (103 loc) · 3.87 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
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
119
120
121
122
123
#!/usr/bin/python -p
# import libs for Loxone connection
import requests
from requests.auth import HTTPBasicAuth
import urllib2
from base64 import b64encode
import urllib3
import json
# Initialize logging
import logging
lg = logging.getLogger(__name__)
def get_req(url, user, password):
try:
myResponse = requests.get(
url, auth=HTTPBasicAuth(user, password), verify=True, timeout=(2.0, 7.0)
)
except (requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout ) as e:
lg.error("Connection to Miniserver failed with: {}".format(e))
return None
if (myResponse.ok):
return myResponse.json()
else:
lg.error("Connection to Miniserver failed \
with http status code: {}".format(myResponse.status_code))
return None
def get_url2(url, user, password):
try:
request = urllib2.Request(url)
request.add_header(
'Authorization', 'Basic ' + b64encode(user + ':' + password)
)
handler = urllib2.urlopen(request, timeout=7)
except urllib2.HTTPError as e:
lg.error("Connection to Miniserver failed with: {}".format(e))
return None
if (handler.code == 200):
return json.loads(handler.read())
else:
lg.error("Connection to Miniserver failed \
with http status code: {}".format(handler.code))
return None
def get_url3(url, user, password):
try:
http = urllib3.PoolManager()
headers = urllib3.util.make_headers(basic_auth=user + ':' + password)
response = http.request('GET', url, headers=headers, timeout=Timeout(connect=2.0, read=7.0))
except urllib3.exceptions.NewConnectionError as e:
lg.error("Connection to Miniserver failed with: {}".format(e))
return None
if (response.status == 200):
return json.loads(response.data)
else:
lg.error("Connection to Miniserver failed \
with http status code: {}".format(response.status))
return None
def strip_units(value):
while value:
try:
float(value)
break
except ValueError:
value = value[:-1]
return value
def loxclient(
host, user, password, action='state', obj=None, strip=False, lib='requests'
):
# Set Loxone URL
if obj is None:
url = "http://{0}/jdev/sps/{1}".format(host, action)
elif '/' in obj:
url = "http://{0}/jdev/{1}".format(host, obj)
elif obj in ['sys', 'cfg', 'lan', 'bus', 'task0']:
url = "http://{0}/jdev/{1}/{2}".format(host, obj, action)
else:
url = "http://{0}/jdev/sps/io/{1}/{2}".format(host, obj, action)
lg.debug("Loxone URL is: {}".format(url))
# Get Json data from Miniserver
#obj = 'Dummy_Object' if obj is None else obj
#mock_data = str('{"LL": { "control": "dev/sps/io/' + obj + '/state", "value": "531.68", "Code": "200"}}')
#jData = json.loads(mock_data)
get = {'requests': get_req, 'urllib2': get_url2, 'urllib3': get_url3}
jData = get[lib](url, user, password)
if jData is None:
# Getting the data failed, bailing out
return None
lg.debug("The response json content is: {}".format(jData))
# Check return code inside the json.
# If it's not 200, response probably doesn't contain what we asked for.
if jData['LL']['Code'] != '200':
lg.error(
"Miniserver returned error code: {}".format(jData['LL']['Code'])
)
return None
# Get the value from json response and optionally strip it from units.
value = jData['LL']['value']
if strip:
value = float(strip_units(value))
lg.debug("The requested value is: {}".format(value))
else:
# Do not crash on non-ASCII chars (i.e. Czech).
# Convert it into UTF8 string for logger first.
lg.debug("The requested value is: {}".format(value.encode("utf-8")))
return value