-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
40 lines (35 loc) · 1.48 KB
/
api.py
File metadata and controls
40 lines (35 loc) · 1.48 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
import requests
import os
# these are AWS environment variables - set them for your OpenHAB installation
hostname = os.environ['hostname']
port = os.environ['port']
user = os.environ['user']
password = os.environ['password']
# sends commands to OpenHAB for an item via its restful interface
# returns True for successful outcome, False for error.
# name is the item name, value is the payload data.
def postCommand(name, value):
try:
resp = requests.post("http://" + hostname + ":" + str(port) + "/rest/items/" + name,
headers={'Content-Type': 'text/plain', 'Content-Length': str(len(value))}, data=value,
auth=(user, password))
except requests.exceptions.RequestException as e:
print("HTTP error occured: " + str(e))
return False
if resp.status_code not in (200, 201): # bad response
print("got bad HTTP response code:" + resp.status_code)
return False
print("Successful request made: " + name + " " + value) # All OK
return True
# Obtains an "item" from the OpenHAB restful interface.
def getItem(name):
try:
resp = requests.get("http://" + hostname + ":" + str(port) + "/rest/items/" + name, auth=(user, password))
if resp.status_code not in (200, 201):
print("got bad HTTP response code:" + resp.status_code)
return False
json = resp.json()
except Exception as e:
print("Error: " + str(e))
return False
return json