forked from mhatrey/TotalConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TotalConnect.groovy
286 lines (263 loc) · 12.1 KB
/
TotalConnect.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
/**
* TotalConnect
*
* Copyright 2014 Yogesh Mhatre
*
* 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.
*
*/
/*
Version: v0.3.1
Changes [November 12th, 2015]
- Added ability to select Total Connect Location for users with multiple Locations. For sigle location users, it will default to your location
- Implemented logic to successfully message. Though there is room for improvement
Version: v0.3.
Changes [August 30th, 2015]
- Logic to check if the Arm/DisArm signal actually implemented or not.
- User dont have to input LocationID & DeviceID. Its been capatured from the response now.
*/
definition(
name: "TotalConnect v0.3.1",
namespace: "Security",
author: "Yogesh Mhatre",
description: "Total Connect App to lock/unlock your home based on your location and mode",
category: "My Apps",
iconUrl: "https://s3.amazonaws.com/yogi/TotalConnect/150.png",
iconX2Url: "https://s3.amazonaws.com/yogi/TotalConnect/300.png")
preferences {
page(name: "credentials", title: "Total Connect Login", nextPage: "locationList", uninstall: false) {
section ("Give your Total Connect credentials. Recommended to make another user for SmartThings") {
input("userName", "text", title: "Username", description: "Your username for TotalConnect")
input("password", "password", title: "Password", description: "Your Password for TotalConnect")
input("applicationId", "text", title: "Application ID - It is '14588' currently", description: "Application ID")
input("applicationVersion", "text", title: "Application Version", description: "Application Version")
}
}
page(name: "locationList", title: "Select the Total Connection Location for this App", nextPage: "success", content:"locationList")
page(name: "success")
}
// Start of Page Functions
private locationList(params=[:]){
def locations = locationFound()
def options = locations.keySet() ?: []
return dynamicPage(name:"locationList", title:"Pulling up the Location List!", install: true, uninstall: true) {
section("Select from the following Locations for Total Connect.") {
input "selectedLocation", "enum", required:true, title:"Select the Location", multiple:false, options:options
}
}
}
Map locationFound() {
log.debug "Executed location function during Setup"
def token
def paramsLogin = [
uri: "https://rs.alarmnet.com/TC21API/TC2.asmx/AuthenticateUserLogin",
body: [userName: settings.userName , password: settings.password, ApplicationID: settings.applicationId, ApplicationVersion: settings.applicationVersion]
]
httpPost(paramsLogin) { responseLogin ->
token = responseLogin.data.SessionID
}
log.debug "Smart Things has logged In to get Locations. SessionID: ${token}"
def locationId
def locationName
def locationMap = [:]
def getSessionParams = [
uri: "https://rs.alarmnet.com/tc21api/tc2.asmx/GetSessionDetails",
body: [ SessionID: token, ApplicationID: settings.applicationId, ApplicationVersion: settings.applicationVersion]
]
httpPost(getSessionParams) { responseSession ->
responseSession.data.Locations.LocationInfoBasic.each
{
LocationInfoBasic ->
locationName = LocationInfoBasic.LocationName
locationId = LocationInfoBasic.LocationID
locationMap["${locationName}"] = "${locationId}"
}
}
log.debug "This is map during Settings " + locationMap
def paramsLogout = [
uri: "https://rs.alarmnet.com/TC21API/TC2.asmx/Logout",
body: [SessionID: token]
]
httpPost(paramsLogout) { responseLogout ->
log.debug "Smart Things has successfully logged out during settings"
}
return locationMap
}
def success() {
dynamicPage(name: "success") {
section {
image "https://s3.amazonaws.com/yogi/TotalConnect/success.jpg"
}
}
}
// End of Page Functions
// SmartThings defaults
def installed() {
//log.debug "Installed with settings: ${settings}"
subscribe(location, checkMode)
}
def updated() {
//log.debug "Updated with settings: ${settings}"
unsubscribe()
subscribe(location, checkMode)
}
// Logic for Triggers based on mode change of SmartThings
def checkMode(evt) {
if (evt.value == "Away") {
log.debug "Mode is set to Away, Performing ArmAway"
armAway()
}
else if (evt.value == "Night") {
log.debug "Mode is set to Night, Performing ArmStay"
armStay()
}
else if (evt.value == "Home") {
log.debug "Mode is set to Home, Performing Disarm"
disarm()
}
}
// Login Function. Returns SessionID for rest of the functions
def login(token) {
log.debug "Executed login"
def paramsLogin = [
uri: "https://rs.alarmnet.com/TC21API/TC2.asmx/AuthenticateUserLogin",
body: [userName: settings.userName , password: settings.password, ApplicationID: settings.applicationId, ApplicationVersion: settings.applicationVersion]
]
httpPost(paramsLogin) { responseLogin ->
token = responseLogin.data.SessionID
}
log.debug "Smart Things has logged In. SessionID: ${token}"
return token
} // Returns token
// Logout Function. Called after every mutational command. Ensures the current user is always logged Out.
def logout(token) {
log.debug "During logout - ${token}"
def paramsLogout = [
uri: "https://rs.alarmnet.com/TC21API/TC2.asmx/Logout",
body: [SessionID: token]
]
httpPost(paramsLogout) { responseLogout ->
log.debug "Smart Things has successfully logged out"
}
}
// Gets Panel Metadata. Takes token & location ID as an argument
Map panelMetaData(token, locationId) {
def alarmCode
def lastSequenceNumber
def lastUpdatedTimestampTicks
def partitionId
def getPanelMetaDataAndFullStatus = [
uri: "https://rs.alarmnet.com/TC21API/TC2.asmx/GetPanelMetaDataAndFullStatus",
body: [ SessionID: token, LocationID: locationId, LastSequenceNumber: 0, LastUpdatedTimestampTicks: 0, PartitionID: 1]
]
httpPost(getPanelMetaDataAndFullStatus) { response ->
lastUpdatedTimestampTicks = response.data.PanelMetadataAndStatus.'@LastUpdatedTimestampTicks'
lastSequenceNumber = response.data.PanelMetadataAndStatus.'@ConfigurationSequenceNumber'
partitionId = response.data.PanelMetadataAndStatus.Partitions.PartitionInfo.PartitionID
alarmCode = response.data.PanelMetadataAndStatus.Partitions.PartitionInfo.ArmingState
}
//log.debug "AlarmCode is " + alarmCode
return [alarmCode: alarmCode, lastSequenceNumber: lastSequenceNumber, lastUpdatedTimestampTicks: lastUpdatedTimestampTicks]
} //Should return alarmCode, lastSequenceNumber & lastUpdateTimestampTicks
// Get LocationID & DeviceID map
Map getSessionDetails(token) {
def locationId
def deviceId
def locationName
Map locationMap = [:]
Map deviceMap = [:]
def getSessionParams = [
uri: "https://rs.alarmnet.com/tc21api/tc2.asmx/GetSessionDetails",
body: [ SessionID: token, ApplicationID: settings.applicationId, ApplicationVersion: settings.applicationVersion]
]
httpPost(getSessionParams) { responseSession ->
responseSession.data.Locations.LocationInfoBasic.each
{
LocationInfoBasic ->
locationName = LocationInfoBasic.LocationName
locationId = LocationInfoBasic.LocationID
deviceId = LocationInfoBasic.DeviceList.DeviceInfoBasic.DeviceID
locationMap["${locationName}"] = "${locationId}"
deviceMap["${locationName}"] = "${deviceId}"
}
}
// log.debug "Location map is " + locationMap + "& Devie ID map is " + deviceMap
return [locationMap: locationMap, deviceMap: deviceMap]
} // Should return Map of Locations
// Arm Function. Performs arming function
def armAway() {
def token = login(token)
def details = getSessionDetails(token) // Gets Map of Location
// log.debug "This was Selected " + settings.selectedLocation
def locationName = settings.selectedLocation
def locationId = details.locationMap[locationName]
// log.debug "ArmAway Function. Location ID is " + locationId
def deviceId = details.deviceMap[locationName]
// log.debug "ArmAway Function. Device ID is " + deviceId
def paramsArm = [
uri: "https://rs.alarmnet.com/TC21API/TC2.asmx/ArmSecuritySystem",
body: [SessionID: token, LocationID: locationId, DeviceID: deviceId, ArmType: 0, UserCode: '-1']
]
httpPost(paramsArm) // Arming Function in away mode
def metaData = panelMetaData(token, locationId) // Get AlarmCode
while( metaData.alarmCode != 10201 ){
pause(3000) // 3 Seconds Pause to relieve number of retried on while loop
metaData = panelMetaData(token, locationId)
}
//log.debug "Home is now Armed successfully"
sendPush("Home is now Armed successfully")
logout(token)
}
def armStay() {
def token = login(token)
def details = getSessionDetails(token) // Gets Map of Location
// log.debug "This was Selected " + settings.selectedLocation
def locationName = settings.selectedLocation
def locationId = details.locationMap[locationName]
// log.debug "ArmStay Function. Location ID is " + locationId
def deviceId = details.deviceMap[locationName]
// log.debug "ArmStay Function. Device ID is " + deviceId
def paramsArm = [
uri: "https://rs.alarmnet.com/TC21API/TC2.asmx/ArmSecuritySystem",
body: [SessionID: token, LocationID: locationId, DeviceID: deviceId, ArmType: 1, UserCode: '-1']
]
httpPost(paramsArm) // Arming function in stay mode
def metaData = panelMetaData(token, locationId) // Gets AlarmCode
while( metaData.alarmCode != 10203 ){
pause(3000) // 3 Seconds Pause to relieve number of retried on while loop
metaData = panelMetaData(token, locationId)
}
//log.debug "Home is now Armed for Night successfully"
sendPush("Home is armed in Night mode successfully")
logout(token)
}
def disarm() {
def token = login(token)
def details = getSessionDetails(token) // Get Location & Device ID
// log.debug "This was Selected " + settings.selectedLocation
def locationName = settings.selectedLocation
def deviceId = details.deviceMap[locationName]
// log.debug "DisArm Function. Device ID is " + deviceId
def locationId = details.locationMap[locationName]
// log.debug "DisArm Function. Location ID is " + locationId
def paramsDisarm = [
uri: "https://rs.alarmnet.com/TC21API/TC2.asmx/DisarmSecuritySystem",
body: [SessionID: token, LocationID: locationId, DeviceID: deviceId, UserCode: '-1']
]
httpPost(paramsDisarm)
def metaData = panelMetaData(token, locationId) // Gets AlarmCode
while( metaData.alarmCode != 10200 ){
pause(3000) // 3 Seconds Pause to relieve number of retried on while loop
metaData = panelMetaData(token, locationId)
}
// log.debug "Home is now Disarmed successfully"
sendPush("Home is now Disarmed successfully")
logout(token)
}