-
Notifications
You must be signed in to change notification settings - Fork 62
/
SHMBased.groovy
459 lines (416 loc) · 17.4 KB
/
SHMBased.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
* TotalConnect
*
* Copyright 2017 Yogesh Mhatre, Oendaril, Jeremy Stroebel
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
/*
Edition: Smarthings Smart Home Monitor
Version: v0.3.4
Changes
- Few bugs smashed. UnAuthenticated DisArm functionality was broken (copy/paste error), now fixed.
- Notifications were broken. Now fixed. Instead of polling on TotalConnect API, We are waiting for 70 seconds before notifying.
*/
include 'asynchttp_v1'
definition(
name: "TotalConnect vSHM",
namespace: "Security",
author: "Yogesh Mhatre",
description: "Total Connect App to lock/unlock your home based on SmartThings Smart Home Monitor",
category: "My Apps",
iconUrl: "https://s3.amazonaws.com/yogi/TotalConnect/150.png",
iconX2Url: "https://s3.amazonaws.com/yogi/TotalConnect/300.png")
preferences {
page(name: "authenticate", content: "authenticate")
page(name: "selectLocation", content: "selectLocation")
page(name: "confirmation", content: "confirmation")
}
// First Page for authentication
def authenticate() {
dynamicPage(name: "authenticate", title: "Total Connect Login", nextPage: "selectLocation",uninstall: true, install: false){
section("Give your TotalConnect credentials") {
paragraph "It is recommended to make another total connect user for SmartThings. This user should have a passcode SET"
input("userName", "text", title: "Username", description: "Your username for TotalConnect")
input("password", "password", title: "Password", description: "Your Password for TotalConnect", submitOnChange:true)
}
section("Backend TotalConnect Values - DO NOT CHANGE", hideable: true, hidden: true) {
paragraph "These are required for login. They typically do not change"
input("applicationId", "text", title: "Application ID currently - 14588", description: "Application ID", defaultValue: "14588")
input("applicationVersion", "text", title: "Application Version currently - 3.24.1", description: "Application Version", defaultValue: "3.24.1")
}
}
}
// Second Page that pulls Location Map to select a Location for multiLocation users of TotalConnect
def selectLocation() {
atomicState.applicationId = settings.applicationId
atomicState.applicationVersion = settings.applicationVersion
//log.debug "During authentication page applicationId is $atomicState.applicationId & applicationVersion is $atomicState.applicationVersion"
getLocations()
while (atomicState.locationMap == null)
{
pause(1000)
} // This while loop is "bit disturbing, but needed. Until atomicState.locationMap does not have any value, we are making the app pause for a seconds. Typically in my observance, the while loop is only executed thrice or less"
//log.debug "During selectLocation Page, LocationMap is $atomicState.locationMap"
def locations = atomicState.locationMap
def optionList = locations.keySet() as List
//log.debug "OptionList are " + optionList
dynamicPage(name: "selectLocation", title: "Select the Location of your Total Connect Alarm",nextPage: "confirmation", uninstall: true, install: false) {
section("Select from the following Locations for Total Connect.") {
input(name: "selectLocation", type: "enum", required: true, title: "Select the Location", options:optionList, multiple: false, submitOnChange:true)
}
}
}
// Third page to store LocationID & DeviceID for SmartApp use
def confirmation(){
//log.debug "Option selected is $settings.selectLocation"
def selectedLocation = settings.selectLocation
log.debug "During Confirmation Page, LocationMap: $atomicState.locationMap, DeviceMap: $atomicState.deviceMap"
def locations = atomicState.locationMap
def devices = atomicState.deviceMap
def deviceId = devices["${selectedLocation}"]
def locationId = locations["${selectedLocation}"]
log.debug "DeviceId is $deviceId & LocationId is $locationId"
dynamicPage(name: "confirmation", title: "Selected LocationID & DeviceID", uninstall: true, install: true) {
section("DO NOT CHANGE - These values are fetched from your selected Location.") {
paragraph "If your DeviceID is larger than 6 digits, please use the first 6 digits and remove the rest."
input(name: "locationId", type: "text", required: true, title: "LocationID", defaultValue: locationId)
input(name: "deviceId", type: "text", required: true, title: "DeviceID", defaultValue: deviceId)
}
}
}
// SmartThings defaults
def installed() {
log.debug "Installed with settings: Username: $settings.userName, ApplicationId: $settings.applicationId & ApplicationVersion: $settings.applicationVersion, DeviceId: $settings.deviceId & LocationId: $settings.locationId"
subscribe(location, "alarmSystemStatus", alarmStatusHandler)
}
def updated() {
log.debug "Updated with settings: Username: $settings.userName, ApplicationId: $settings.applicationId & ApplicationVersion: $settings.applicationVersion, DeviceId: $settings.deviceId & LocationId: $settings.locationId"
unsubscribe()
subscribe(location, "alarmSystemStatus", alarmStatusHandler)
}
// Logic for Triggers based on mode change of SmartThings
def alarmStatusHandler(evt) {
if (evt.value == "away") {
log.debug "Smart Home Monitor is set to Away, Performing ArmAway"
armAway()
}
else if (evt.value == "stay") {
log.debug "Smart Home Monitor is set to Night, Performing ArmStay"
armStay()
}
else if (evt.value == "off") {
log.debug "Smart Home Monitor is set to Home, Performing Disarm"
disarm()
}
}
// disarm Function
def disarm() {
log.debug "disarm is executed"
if(isTokenValid())
disarmAuthenticated()
else {
log.debug "disarm is executed with login"
login(disarmAuthenticated)
}
}
def disarmAuthenticated() {
tcCommandAsync("DisarmSecuritySystem", [SessionID: state.token, LocationID: settings.locationId, DeviceID: settings.deviceId, UserCode: '-1'], 0, "disarm")
}
// armStay Function
def armStay() {
if(isTokenValid())
armStayAuthenticated()
else {
login(armStayAuthenticated)
}
}
def armStayAuthenticated() {
tcCommandAsync("ArmSecuritySystem", [SessionID: state.token, LocationID: settings.locationId, DeviceID: settings.deviceId, ArmType: 1, UserCode: '-1'], 0, "armStay")
}
// armAway Function
def armAway() {
if(isTokenValid())
armAwayAuthenticated()
else {
login(armAwayAuthenticated)
}
}
def armAwayAuthenticated() {
tcCommandAsync("ArmSecuritySystem", [SessionID: state.token, LocationID: settings.locationId, DeviceID: settings.deviceId, ArmType: 0, UserCode: '-1'], 0 , "armAway")
}
// Login Function.
def login(callback) {
//log.debug "Executed login"
tcCommandAsync("AuthenticateUserLogin", [userName: settings.userName , password: settings.password, ApplicationID: settings.applicationId, ApplicationVersion: settings.applicationVersion], 0, callback)
}
def loginResponse(token, callback) {
//log.debug "loginResponse with callback: ${callback} is Executed"
if(token != null) {
//log.debug "new token is ${token}"
state.token = "${token}"
state.tokenRefresh = now()
}
switch(callback) {
case "refresh":
refresh()
break
case "refreshAuthenticated":
refreshAuthenticated()
break
case "getSessionDetails":
getSessionDetails()
break
case "armAway":
armAway()
break
case "armAwayAuthenticated":
armAwayAuthenticated()
break
case "armStay":
armStay()
break
case "armStayAuthenticated":
armStayAuthenticated()
break
case "getPanelMetadata":
getPanelMetadata()
break
case "disarm":
disarm()
break
case "disarmAuthenticated":
disarmAuthenticated()
break
default:
return
break
}
}
def logout() {
tcCommandAsync("Logout", [SessionID: state.token], 0, "logout")
} //Takes token as argument
// Asyn APIs
def tcCommandAsync(path, body, retry, callback) {
//log.debug "tcCommandAsync was Executed"
String stringBody = ""
body.each { k, v ->
if(!(stringBody == "")) {
stringBody += "&" }
stringBody += "${k}=${v}"
}//convert Map to String
//log.debug "stringBody: ${stringBody}"
def params = [
uri: "https://rs.alarmnet.com/TC21API/TC2.asmx/",
path: path,
body: stringBody,
requestContentType: "application/x-www-form-urlencoded",
contentType: "application/xml"
]
def handler
switch(path) {
case "GetPanelMetaDataAndFullStatusEx":
handler = "panel"
break
case "GetZonesListInStateEx":
handler = "zone"
break
case "AuthenticateUserLogin":
handler = "login"
break
case "GetSessionDetails":
handler = "details"
break
case "ArmSecuritySystem":
handler = "refresh"
break
case "DisarmSecuritySystem":
handler = "refresh"
break
default:
handler = "none"
break
}//define handler based on method called
def data = [
path: path,
body: stringBody,
handler: handler,
callback: callback,
retry: retry
] //Data for Async Command. Params to retry, handler to handle, and retry count if needed
try {
asynchttp_v1.post('asyncResponse', params, data)
//log.debug "Sent asynchhttp_v1.post(responseHandler, ${params}, ${data})"
} catch (e) {
log.error "Something unexpected went wrong in tcCommandAsync: ${e}"
}//try / catch for asynchttpPost
}//async post command
def asyncResponse(response, data) {
//log.debug "asyncresponse was Executed"
if (response.hasError()) {
log.debug "error response data: ${response.errorData}"
try {
// exception thrown if xml cannot be parsed from response
log.debug "error response xml: ${response.errorXml}"
} catch (e) {
log.warn "error parsing xml: ${e}"
}
try {
// exception thrown if json cannot be parsed from response
log.debug "error response json: ${response.errorJson}"
} catch (e) {
log.warn "error parsing json: ${e}"
}
}
response = response.getXml()
//log.debug "data: ${data}"
//log.debug "response received: ${response}"
try {
def handler = data.get('handler')
def callback = data.get('callback')
if(handler == "login") {
if(response.ResultCode == "0") {
loginResponse(response.SessionID, callback)
}
else {
log.error "Command Type: ${data} failed with ResultCode: ${resultCode} and ResultData: ${resultData}"
}
}
else {
//validate response
def resultCode = response.ResultCode
def resultData = response.ResultData
switch(resultCode) {
case "0": //Successful Command
case "4500": //Successful Command for Arm Action
state.tokenRefresh = now() //we ran a successful command, that will keep the token alive
//log.debug "Handler: ${data.get('handler')}"
switch(handler) {
case "details":
//details handler would be executed only when you need LocationID & DeviceID, that is during initial Setup & Updates.
def locationId
def deviceId
def locationName
Map locationMap = [:]
Map deviceMap = [:]
response.Locations.LocationInfoBasic.each
{
LocationInfoBasic ->
locationName = LocationInfoBasic.LocationName
locationId = LocationInfoBasic.LocationID
deviceId = LocationInfoBasic.DeviceList.DeviceInfoBasic.DeviceID
locationMap["${locationName}"] = "${locationId}"
deviceMap["${locationName}"] = "${deviceId}"
}
atomicState.locationMap = locationMap
atomicState.deviceMap = deviceMap
log.debug "During 'details' handler, LocationMap is $atomicState.locationMap & DeviceMap is $atomicState.deviceMap"
break
case "panel":
updateAlarmStatus(getAlarmStatus(response))
break
case "refresh":
runIn(70,refresh)
break
default:
return
break
}//switch(data)
break
case "-102":
//this means the Session ID is invalid, needs to login and try again
log.error "Command Type: ${data} failed with ResultCode: ${resultCode} and ResultData: ${resultData}"
log.debug "Attempting to refresh token and try again for method ${callback}"
state.token = null
if(state.loginRetry == null || state.loginRetry == 0) {
state.loginRetry = 1;
state.token = null
login(callback)
}
else {
state.loginRetry = 0;
}
break
case "4101": //We are unable to connect to the security panel. Please try again later or contact support
case "4108": //Panel not connected with Virtual Keypad. Check Power/Communication failure
case "-4002": //The specified location is not valid
case "-4108": //Cannot establish a connection at this time. Please contact your Security Professional if the problem persists.
default: //Other Errors
log.error "Command Type: ${data} failed with ResultCode: ${resultCode} and ResultData: ${resultData}"
break
}//switch
}
} catch (SocketTimeoutException e) {
//identify a timeout and retry?
log.error "Timeout Error: $e"
} catch (e) {
log.error "Something unexpected went wrong in asyncResponse: $e"
}//try / catch for httpPost
}//asyncResponse
// Other necessary functions.
def isTokenValid() {
def isValid = true
if(state.token == null) {
isValid = false
}
else {
Long timeSinceRefresh = now() - (state.tokenRefresh != null ? state.tokenRefresh : 0)
//return false if time since refresh is over 4 minutes (likely timeout)
if(timeSinceRefresh > 240000) {
state.token = null
isValid = false
}
}
return isValid
} // This is a logical check only, assuming known timeout values and clearing token on loggout. This method does no testing of the actua
def getSessionDetails() {
tcCommandAsync("GetSessionDetails", [SessionID: state.token, ApplicationID: settings.applicationId, ApplicationVersion: settings.applicationVersion], 0, "getSessionDetails") //This updates panel status
}
def getLocations() {
login(getSessionDetails)
}
def getPanelMetadata() {
tcCommandAsync("GetPanelMetaDataAndFullStatusEx", [SessionID: state.token, LocationID: settings.locationId, LastSequenceNumber: 0, LastUpdatedTimestampTicks: 0, PartitionID: 1], 0, "getPanelMetadata") //This updates panel status
}
def refresh() {
if(isTokenValid()) {
refreshAuthenticated()
}
else {
login(refreshAuthenticated)
}
}
def refreshAuthenticated() {
getPanelMetadata() // Gets AlarmCode
}
def updateAlarmStatus(alarmCode) {
if(state.alarmCode != alarmCode) {
if (alarmCode == "10200") {
log.debug "Status is: Disarmed"
sendNotification("TotalConnect has Disarmed your ${settings.selectLocation} successfully", [method: "push"])
} else if (alarmCode == "10203") {
log.debug "Status is: Armed Stay"
sendNotification("TotalConnect has Armed Stay your ${settings.selectLocation} successfully", [method: "push"])
} else if (alarmCode =="10201") {
log.debug "Status is: Armed Away"
sendNotification("TotalConnect has Armed Away your ${settings.selectLocation} successfully", [method: "push"])
}
}
//logout(token)
state.alarmCode = alarmCode
}
// Gets Panel Metadata.
def getAlarmStatus(response) {
String alarmCode
alarmCode = response.PanelMetadataAndStatus.Partitions.PartitionInfo.ArmingState
state.alarmStatusRefresh = now()
return alarmCode
} //returns alarmCode