-
Notifications
You must be signed in to change notification settings - Fork 15
/
nukiWebApiHandler.groovy
194 lines (178 loc) · 6.27 KB
/
nukiWebApiHandler.groovy
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import groovy.json.JsonSlurper
metadata {
// Automatically generated. Make future change here.
definition (name: "Simulated Lock", namespace: "Volski", author: "Volski") {
capability "Actuator"
capability "Sensor"
capability "Health Check"
capability "Lock"
capability "Battery"
capability "Refresh"
command "lockNgo"
}
preferences {
input("api_token","string",title:"Api Web Token",description:"Enter your web API token",required:true ,displayDuringSetup: true)
input("nukiId","string",title:"nukiId",description:"Enter your nukiId",required:true ,displayDuringSetup: true)
}
tiles {
multiAttributeTile(name:"lock", type: "generic", width: 6, height: 4){
tileAttribute ("lock", key: "PRIMARY_CONTROL") {
attributeState "locked", label:'locked', action:"lock.unlock", icon:"st.locks.lock.locked", backgroundColor:"#FF0000"
attributeState "unlocked", label:'unlocked', action:"lock.lock", icon:"st.locks.lock.unlocked", backgroundColor:"#79B821"
attributeState "locking", label:'locking', icon:"st.locks.lock.locked", backgroundColor:"#00A0DC"
attributeState "unlocking", label:'unlocking', icon:"st.locks.lock.unlocked", backgroundColor:"#00A0DC"
attributeState "waiting", label: "Waiting", icon:"st.locks.lock.locked", backgroundColor:"#00000F"
attributeState "lockngo",label: "LockNgo - UNLOCKED", icon:"st.locks.lock.locked", backgroundColor:"#78C821"
}
tileAttribute ("device.battery", key: "SECONDARY_CONTROL") {
attributeState "battery", label: 'battery ${currentValue}%', unit: "%"
}
}
standardTile("lockNgo", "lockNgo", inactiveLabel: false, decoration: "flat", width: 3, height: 2) {
state "default", label:'lockNgo', action:"lockNgo", icon: "st.locks.lock.locked"
}
standardTile("refresh", "device.refresh", inactiveLabel: false, decoration: "flat", width: 3, height: 2) {
state "default", action:"refresh", icon:"st.secondary.refresh"
}
}
}
def installed() {
log.trace "installed()"
initialize()
runEvery1Minute(refresh)
}
def updated() {
log.info('called shade updated()')
runEvery1Minute(refresh)
}
def initialize() {
log.trace "initialize()"
setBatteryLevel(50)
}
def refresh() {
sendEvent( name: "lock" ,value: "waiting")
log.trace "Refreshing!!"
def params = [
uri: "https://api.nuki.io/smartlock/${nukiId}",
requestContentType: "application/json",
headers: ['Authorization': "Bearer ${api_token}"]
]
try{
httpGet(params)
{resp ->
log.debug "resp data: ${resp.data}"
log.debug "Battery is Critical: ${resp.data.state.batteryCritical}"
if(resp.data.state.batteryCritical==false)
{
sendEvent(name:"battery", value:100)
}
else
{
sendEvent(name:"battery", value:0)
}
if(1==resp.data.state.state)
{
sendEvent(name: "lock", value: "locked")
}
else if (2==resp.data.state.state)
{
sendEvent(name: "lock", value: "unlocking")
runIn(5, refresh)
}
else if (3==resp.data.state.state)
{
sendEvent(name: "lock", value: "unlocked")
}
else if (4==resp.data.state.state)
{
sendEvent(name: "lock", value: "locking")
runIn(5, refresh)
}
else if (6==resp.data.state.state)
{
sendEvent(name: "lock", value: "lockngo")
runIn(5, refresh)
}
else
{
sendEvent(name: "lock", value: "waiting")
runIn(2, refresh)
}
}
}
catch(e)
{
log.debug "Error ${e}"
}
}
/*
In http resp status code ->
204 Ok
400 Bad Parameter
401 Not authorized
*/
def lock() {
log.trace "---LOCK COMMAND---"
sendEvent(name: "lock", value: "locking")
def object = new JsonSlurper().parseText('{ "action": "2"}')
def options = [
uri: "https://api.nuki.io/smartlock/${nukiId}/action",
requestContentType: "application/json",
headers: [
'User-Agent': 'none',
'Accept': 'application/json',
'Authorization': "Bearer ${api_token}"
],
body: object
]
httpPost(options)
{resp -> log.debug "resp status: ${resp.status}"
if ( resp.status == 204 ) {
runIn(7, refresh)
}}
}
def unlock() {
log.trace "---UNLOCK COMMAND---"
sendEvent(name: "lock", value: "unlocking")
def object = new JsonSlurper().parseText('{ "action": "1"}')
def options = [
uri: "https://api.nuki.io/smartlock/${nukiId}/action",
requestContentType: "application/json",
headers: [
'User-Agent': 'none',
'Accept': 'application/json',
'Authorization': "Bearer ${api_token}"
],
body: object
]
httpPost(options)
{resp -> log.debug "resp status: ${resp.status}"
if ( resp.status == 204 ) {
runIn(7, refresh)
}}
}
def lockNgo(){
log.trace "lockNgo"
sendEvent(name: "lock", value: "lockngo")
def object = new JsonSlurper().parseText('{ "action": "4"}')
def options = [
uri: "https://api.nuki.io/smartlock/${nukiId}/action",
requestContentType: "application/json",
headers: [
'User-Agent': 'none',
'Accept': 'application/json',
'Authorization': "Bearer ${api_token}"
],
body: object
]
httpPost(options)
{resp -> log.debug "resp status: ${resp.status}"
if ( resp.status == 204 ) {
runIn(30, refresh)
}}
}
def setBatteryLevelLock(Number lvl) {
log.trace "setBatteryLevel(level)"
sendEvent(name: "battery", value: lvl)
sendEvent( name: "lock" ,value: "locked")
}