-
Notifications
You must be signed in to change notification settings - Fork 1
/
nukiproxy.py
103 lines (80 loc) · 3.07 KB
/
nukiproxy.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Ben Jones <ben.jones12()gmail.com>'
__copyright__ = 'Copyright 2016 Ben Jones'
# wget http://bottlepy.org/bottle.py
# ... or ... pip install bottle
from bottle import get, post, put, request, run, response, abort
import os
import sys
import ConfigParser
import json
import time
import re
import requests
# Script name (without extension) used for config/logfile names
APPNAME = os.path.splitext(os.path.basename(__file__))[0]
INIFILE = os.getenv('INIFILE', APPNAME + '.ini')
# Read the config file
config = ConfigParser.RawConfigParser()
config.read(INIFILE)
# Use ConfigParser to pick out the settings
DEBUG = config.getboolean("global", "DEBUG")
NUKIPROXY_HOST = config.get("nukiproxy", "NUKIPROXY_HOST")
NUKIPROXY_PORT = config.getint("nukiproxy", "NUKIPROXY_PORT")
NUKIPROXY_ENDPOINT = config.get("nukiproxy", "NUKIPROXY_ENDPOINT")
OPENHAB_HOST = config.get("openhab", "OPENHAB_HOST")
OPENHAB_PORT = config.getint("openhab", "OPENHAB_PORT")
OPENHAB_USER = config.get("openhab", "OPENHAB_USER")
OPENHAB_PASSWORD = config.get("openhab", "OPENHAB_PASSWORD")
OPENHAB_ITEM_LOCKED = config.get("openhab", "OPENHAB_ITEM_LOCKED")
OPENHAB_ITEM_STATE = config.get("openhab", "OPENHAB_ITEM_STATE")
OPENHAB_ITEM_STATENAME = config.get("openhab", "OPENHAB_ITEM_STATENAME")
OPENHAB_ITEM_BATTERYCRITICAL = config.get("openhab", "OPENHAB_ITEM_BATTERYCRITICAL")
def update_openhab(item, value):
url = 'http://%s:%s@%s:%d/rest/items/%s/state' % (OPENHAB_USER, OPENHAB_PASSWORD, OPENHAB_HOST, OPENHAB_PORT, item)
headers = { 'Content-Type': 'text/plain' }
requests.put(url, headers=headers, data=value)
@get('/')
def monitor():
response.status = 200
return
@post(NUKIPROXY_ENDPOINT)
def proxy():
'''Nuki bridge callback - called whenever the Nuki lock changes state
Sends a JSON POST to the configured callback URL in the form'
{"nukiId": 11, "state": 1, "stateName": "locked", "batteryCritical": false}
'''
try:
try:
data = request.json
except:
raise ValueError
if data is None:
raise ValueError
nukiId = data['nukiId']
state = data['state']
stateName = data['stateName']
batteryCritical = data['batteryCritical']
if state == 1:
update_openhab(OPENHAB_ITEM_LOCKED, 'CLOSED')
else:
update_openhab(OPENHAB_ITEM_LOCKED, 'OPEN')
update_openhab(OPENHAB_ITEM_STATE, str(state))
update_openhab(OPENHAB_ITEM_STATENAME, stateName)
if batteryCritical:
update_openhab(OPENHAB_ITEM_BATTERYCRITICAL, 'ON')
else:
update_openhab(OPENHAB_ITEM_BATTERYCRITICAL, 'OFF')
except ValueError:
response.status = 400
return
response.state = 200
return
if __name__ == '__main__':
try:
run(host=NUKIPROXY_HOST, port=NUKIPROXY_PORT, debug=DEBUG)
except KeyboardInterrupt:
sys.exit(0)
except:
raise