forked from EricJilot/SmartThings.BatteryMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BatteryMonitor.smartapp.groovy
334 lines (286 loc) · 9.59 KB
/
BatteryMonitor.smartapp.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
def smartAppNameFull() {
return "BatteryMonitor SmartApp for SmartThings"
}
def smartAppNameShort() {
return "BatteryMonitor"
}
def smartAppVersion() {
return "Version 0.0.5"
}
def smartAppAuthor() {
return "Author Brandon Gordon"
}
def smartAppCopyright() {
return "Copyright (c) 2014 Brandon Gordon"
}
def smartAppSource() {
return "https://github.com/notoriousbdg/SmartThings.BatteryMonitor"
}
def smartAppDescription() {
return "This SmartApp helps you monitor the status of your SmartThings devices with batteries."
}
def smartAppRevision () {
return '2014-11-14 v0.0.1\n' +
' * Initial release\n\n' +
'2014-11-15 v0.0.2\n' +
' * Moved status to main page\n' +
' * Removed status page\n' +
' * Improved formatting of status page\n' +
' * Added low, medium, high thresholds\n' +
' * Handle battery status strings of OK and Low\n\n' +
'2014-11-15 v0.0.3\n' +
' * Added push notifications\n\n' +
'2014-11-20 v0.0.4\n' +
' * Added error handling for batteries that return strings\n\n' +
'2014-12-26 v0.0.5\n' +
' * Move app metadata to a new about page\n' +
' * Changed notifications to only send at specified time daily\n'
}
def smartAppLicense() {
return '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:' +
'\n\n' +
'http://www.apache.org/licenses/LICENSE-2.0' +
'\n\n' +
'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.'
}
definition(
name: "BatteryMonitor",
namespace: "notoriousbdg",
author: "Brandon Gordon",
description: "SmartApp to monitor battery levels.",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]")
preferences {
page name:"pageStatus"
page name:"pageConfigure"
page name:"pageAbout"
}
// Show About Page
def pageAbout() {
def pageProperties = [
name: "pageAbout",
title: smartAppNameFull(),
nextPage: "pageConfigure",
uninstall: true
]
return dynamicPage(pageProperties) {
section() {
paragraph smartAppVersion() + "\n" +
smartAppAuthor() + "\n" +
smartAppCopyright()
}
section() {
paragraph smartAppDescription()
}
section() {
href(
name: "sourceCode",
title: "Source Code (Tap to view)",
required: false,
external: true,
style: "external",
url: smartAppSource(),
description: smartAppSource()
)
}
section() {
paragraph title: "Revision History",
smartAppRevision()
}
section() {
paragraph title: "License",
smartAppLicense()
}
}
}
// Show Status page
def pageStatus() {
def pageProperties = [
name: "pageStatus",
title: smartAppNameShort() + " Status",
nextPage: null,
install: true,
uninstall: true
]
if (settings.devices == null) {
return pageAbout()
}
def listLevel0 = ""
def listLevel1 = ""
def listLevel2 = ""
def listLevel3 = ""
def listLevel4 = ""
if (settings.level1 == null) { settings.level1 = 33 }
if (settings.level3 == null) { settings.level3 = 67 }
if (settings.pushMessage) { settings.pushMessage = true }
return dynamicPage(pageProperties) {
settings.devices.each() {
try {
if (it.currentBattery == null) {
listLevel0 += "$it.displayName\n"
} else if (it.currentBattery >= 0 && it.currentBattery < settings.level1.toInteger()) {
listLevel1 += "$it.currentBattery $it.displayName\n"
} else if (it.currentBattery >= settings.level1.toInteger() && it.currentBattery <= settings.level3.toInteger()) {
listLevel2 += "$it.currentBattery $it.displayName\n"
} else if (it.currentBattery > settings.level3.toInteger() && it.currentBattery < 100) {
listLevel3 += "$it.currentBattery $it.displayName\n"
} else if (it.currentBattery == 100) {
listLevel4 += "$it.displayName\n"
} else {
listLevel0 += "$it.currentBattery $it.displayName\n"
}
} catch (e) {
log.trace "Caught error checking battery status."
log.trace e
listLevel0 += "$it.displayName\n"
}
}
if (listLevel0) {
section("Batteries with errors or no status") {
paragraph listLevel0.trim()
}
}
if (listLevel1) {
section("Batteries with low charge (less than $settings.level1)") {
paragraph listLevel1.trim()
}
}
if (listLevel2) {
section("Batteries with medium charge (between $settings.level1 and $settings.level3)") {
paragraph listLevel2.trim()
}
}
if (listLevel3) {
section("Batteries with high charge (more than $settings.level3)") {
paragraph listLevel3.trim()
}
}
if (listLevel4) {
section("Batteries with full charge") {
paragraph listLevel4.trim()
}
}
section("Menu") {
href "pageStatus", title:"Refresh", description:""
href "pageConfigure", title:"Configure", description:""
href "pageAbout", title:"About", description: ""
}
}
}
// Show Configure Page
def pageConfigure() {
def helpPage =
"Select devices with batteries that you wish to monitor."
def inputBattery = [
name: "devices",
type: "capability.battery",
title: "Which devices with batteries?",
multiple: true,
required: true
]
def inputLevel1 = [
name: "level1",
type: "number",
title: "Low battery threshold?",
defaultValue: "20",
required: true
]
def inputLevel3 = [
name: "level3",
type: "number",
title: "Medium battery threshold?",
defaultValue: "70",
required: true
]
def inputTime = [
name: "time",
type: "time",
title: "Notify at what time daily?",
required: true
]
def inputPush = [
name: "pushMessage",
type: "bool",
title: "Send push notifications?",
defaultValue: true
]
def inputSMS = [
name: "phoneNumber",
type: "phone",
title: "Send SMS notifications to?",
required: false
]
def pageProperties = [
name: "pageConfigure",
title: smartAppNameShort() + " Configuration",
nextPage: "pageStatus",
uninstall: true
]
return dynamicPage(pageProperties) {
section("About") {
paragraph helpPage
}
section("Devices") {
input inputBattery
}
section("Settings") {
input inputLevel1
input inputLevel3
}
section("Notification") {
input inputTime
input inputPush
input inputSMS
}
section([title:"Options", mobileOnly:true]) {
label title:"Assign a name", required:false
}
}
}
def installed() {
log.debug "Initialized with settings: ${settings}"
initialize()
}
def updated() {
unschedule()
unsubscribe()
initialize()
}
def initialize() {
schedule(settings.time, updateStatus)
}
def send(msg) {
log.debug msg
if (settings.pushMessage) {
sendPush(msg)
} else {
sendNotificationEvent(msg)
}
if (settings.phoneNumber != null) {
sendSms(phoneNumber, msg)
}
}
def updateStatus() {
settings.devices.each() {
try {
if (it.currentBattery == null) {
send("${it.displayName} battery is not reporting.")
} else if (it.currentBattery > 100) {
send("${it.displayName} battery is ${it.currentBattery}, which is over 100.")
} else if (it.currentBattery < settings.level1) {
send("${it.displayName} battery is ${it.currentBattery} (threshold ${settings.level1}.)")
}
} catch (e) {
log.trace "Caught error checking battery status."
log.trace e
send("${it.displayName} battery reported a non-integer level.")
}
}
}