-
Notifications
You must be signed in to change notification settings - Fork 1
/
Auto_Off_d.groovy
309 lines (264 loc) · 10.7 KB
/
Auto_Off_d.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
/**
* Hubitat Import URL: https://raw.githubusercontent.com/HubitatCommunity/Auto_Off/main/Auto_Off_d.groovy
*/
/**
* Auto_Off Child
*
* Copyright 2020 C Steele, Mattias Fornander
*
* 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.
*
*/
public static String version() { return "v1.0.3" }
import groovy.time.*
// Set app Metadata for the Hub
definition(
name: "Auto_Off dim",
namespace: "csteele",
author: "Mattias Fornander, CSteele",
description: "Automatically turn off/on devices after set amount of time on/off",
category: "Automation",
importUrl: "https://raw.githubusercontent.com/HubitatCommunity/Auto_Off/main/Auto_Off_d.groovy",
parent: "csteele:Auto_Off",
iconUrl: "",
iconX2Url: "",
iconX3Url: ""
)
preferences {
page (name: "mainPage")
}
/**
* Called after app is initially installed.
*/
def installed() {
initialize()
app.clearSetting("debugOutput") // app.updateSetting() only updates, won't create.
app.clearSetting("descTextEnable")
}
/**
* Called after any of the configuration settings are changed.
*/
def updated() {
if (descTextEnable) log.info "Updated with settings: ${settings}"
state.delay = Math.floor(autoTime * 60).toInteger()
String curPref = ("$autoTime${devices}${master}$dRate$invert").toString().bytes.encodeBase64()
if (curPref != state.prevPref) {
state.prevPref = curPref
unsubscribe()
unschedule()
if (descTextEnable) log.warn "This Auto_Off Child was reset."
}
initialize()
}
/**
* Internal helper function with shared code for installed() and updated().
*/
private initialize() {
if (state.offList == null) state.offList = [:]
state.topLevel = [0, 99]
subscribe(devices, "switch", switchHandler)
updateMyLabel("1")
}
/**
* Main configuration function declares the UI shown.
*/
def mainPage() {
updateMyLabel("2")
dynamicPage(name: "mainPage", install: true, uninstall: true) {
section("<h2>${app.label ?: app.name}</h2>"){
paragraph '<i>Automatically turn off/on devices after set amount of time on/off.</i>'
input name: "autoTime", type: "number", range: "1..1440", title: "Time until auto-off (minutes) 24hrs max", required: true
input name: "devices", type: "capability.switchLevel", title: "Devices", required: true, multiple: true
input name: "dRate", type: "number", range: "1..100", title: "Dimming rate (in Seconds)", required: false, defaultValue: 1
input name: "invert", type: "bool", title: "Invert logic (make app Auto On)", defaultValue: false
input name: "master", type: "capability.switch", title: "Master Switch", multiple: false
}
section (title: "<b>Name/Rename</b>") {
label title: "This child app's Name (optional)", required: false, submitOnChange: true
if (!app.label) {
app.updateLabel(app.name)
atomicState.appDisplayName = app.name
}
if (app.label.contains('<span ')) {
if (atomicState?.appDisplayName != null) {
app.updateLabel(atomicState.appDisplayName)
} else {
String myLabel = app.label.substring(0, app.label.indexOf('<span '))
atomicState.appDisplayName = myLabel
app.updateLabel(myLabel)
}
}
}
display()
}
}
/**
* Handler called when any of our devices turn on, or off.
*
* We use the device id of the switch turning on as key since the evt.device
* object seems to be a proxy object that changes with each callback. The first
* implementation used the evt.device as key but that would create multiple
* entries in the map for the same switch. Using the device id instead ensures
* that a user that turn on and off and on the same switch, will only have one
* entry since the id stays the same and new off times replace old off times.
*/
def switchHandler(evt) {
def oNow = now() // grab time object: one exact same time then compare and display.
// Add the watched device if turning on, or off if inverted mode
if ((evt.value == "on") ^ (invert == true)) {
state.delay = state?.delay ?: Math.floor(autoTime * 60).toInteger()
runIn(state.delay, scheduleHandler, [overwrite: false])
atomicState.cycleEnd = oNow + autoTime * 60 * 1000
state.offList[evt.device.id] = oNow + autoTime * 60 * 1000
} else {
state.offList.remove(evt.device.id)
if (!state.offList) unschedule(scheduleHandler)
}
updateMyLabel("3")
if (debugOutput) log.debug "switchHandler delay: $state.delay, evt.device:${evt.device}, evt.value:${evt.value}, invert:$invert, state:${state} "
}
/**
* Handler called every minute to see if any devices should be turned off, or on.
*
* THe first pass used an optimized schedule that looked for the next switch to
* turn off and would schedule a callback for exactly that time and then
* reschedule the next off item, if any. However, it seemed error-prone and
* cumbersome since errors can happen that may interrupt the rescheduling.
* Calling a tiny function with a quick check seemed ok to do every minute
* so that's v1.0 for now.
*/
def scheduleHandler() {
def oNow = now() // grab time object: one exact same time then compare and display.
// Find all map entries with an off-time that is earlier than now
def actionList = state.offList.findAll { it.value < oNow }
// Find all devices that match the off-entries from above
def deviceList = devices.findAll { device -> actionList.any { it.key == device.id } }
if (debugOutput) log.debug "scheduleHandler now:${oNow} offList:${state.offList} actionList:${actionList} deviceList:${deviceList}"
// Call setLevel & off() on all relevant devices and remove them from offList
if (!master || master.latestValue("switch") == "on") {
invert ? deviceList*.setLevel(state.topLevel[1], dRate) : deviceList*.setLevel(state.topLevel[0], dRate)
} else {
if (debugOutput) log.debug "Skipping actions because MasterSwitch '${master?.displayName}' is Off"
}
state.offList -= actionList
updateMyLabel("4")
}
def setDebug(dbg, inf) {
app.updateSetting("debugOutput",[value:dbg, type:"bool"])
app.updateSetting("descTextEnable",[value:inf, type:"bool"])
if (descTextEnable) log.info "Set by Parent: debugOutput: $debugOutput, descTextEnable: $descTextEnable"
}
def display()
{
section {
paragraph "\n<hr style='background-color:#1A77C9; height: 1px; border: 0;'></hr>"
paragraph "<div style='color:#1A77C9;text-align:center;font-weight:small;font-size:9px'>Developed by: C Steele<br/>Version Status: $state.Status<br>Current Version: ${version()} - ${thisCopyright}</div>"
}
}
def updateMyLabel(c) {
String flag = '<span '
// Display state / status as part of the label...
String myLabel = atomicState.appDisplayName
if ((myLabel == null) || !app.label.startsWith(myLabel)) {
myLabel = app.label ?: app.name
if (!myLabel.contains(flag)) atomicState.appDisplayName = myLabel
}
if (myLabel.contains(flag)) {
// strip off any connection status tag :: retain the original display name
myLabel = myLabel.substring(0, myLabel.indexOf(flag))
atomicState.appDisplayName = myLabel
}
// log.debug "uml: $c, $state.offList, $atomicState.cycleEnd"
atomicState.cycleEnd = state.offList ? state.offList.max({it.value}).value : 0
if (!master || master.latestValue("switch") == "off") {
String k = invert ? "off" : "on"
if (atomicState.cycleEnd && (devices.findAll{it.latestValue("switch") == k }.size)) {
myLabel = myLabel + "<span style=\"color:Green\"> Active until " + fixDateTimeString(atomicState.cycleEnd) + "</span>"
} else {
myLabel = myLabel + " <span style=\"color:Green\">Idle</span>"
atomicState.cycleEnd = 0
}
} else {
myLabel = myLabel + " <span style=\"color:Crimson\">[-]</span>"
atomicState.cycleEnd = 0
}
if (app.label != myLabel) app.updateLabel(myLabel) // log.debug "label: $myLabel"
}
String fixDateTimeString( eventDate) {
def today = new Date(now()).clearTime()
def target = new Date(eventDate).clearTime()
String resultStr = ''
String myDate = ''
String myTime = ''
boolean showTime = true
if (target == today) {
myDate = 'today'
} else if (target == today-1) {
myDate = 'yesterday'
} else if (target == today+1) {
myDate = 'tomorrow'
} else if (dateStr == '2035-01-01' ) { // to Infinity
myDate = 'a long time from now'
showTime = false
} else {
myDate = 'on '+target.format('MM-dd')
}
if (showTime) {
myTime = new Date(eventDate).format('h:mma').toLowerCase()
}
if (myDate || myTime) {
resultStr = myTime ? "${myDate} at ${myTime}" : "${myDate}"
}
if (debugOutput) log.debug "banner: ${resultStr}"
return resultStr
}
// Parent does the version2 JSON fetch and distributes it to each Child.
def updateCheck(respUD)
{
state.InternalName = "Auto_Off_d"
state.Status = "Unknown"
state.Copyright = "${thisCopyright} -- ${version()}"
if (respUD.application.(state.InternalName) == null) {
if (descTextEnable) log.info "This Application is not version tracked yet."
return
}
def newVer = padVer(respUD.application.(state.InternalName)?.ver)
def currentVer = padVer(version())
state.UpdateInfo = (respUD.application.(state.InternalName)?.updated)
// log.debug "updateCheck: ${respUD.application.(state.InternalName).ver}, $state.UpdateInfo, ${respUD.author}"
switch(newVer) {
case { it == "NLS"}:
state.Status = "<b>** This Application is no longer supported by ${respUD.author} **</b>"
log.warn "** This Application is no longer supported by ${respUD.author} **"
break
case { it > currentVer}:
state.Status = "<b>New Version Available (Version: ${respUD.application.(state.InternalName).ver})</b>"
log.warn "** There is a newer version of this Application available (Version: ${respUD.application.(state.InternalName).ver}) **"
log.warn "** $state.UpdateInfo **"
break
case { it < currentVer}:
state.Status = "<b>You are using a Test version of this Application (Expecting: ${respUD.application.(state.InternalName).ver})</b>"
break
default:
state.Status = "Current"
if (descTextEnable) log.info "You are using the current version of this Application"
break
}
}
/*
padVer
Version progression of 1.4.9 to 1.4.10 would mis-compare unless each column is padded into two-digits first.
*/
def padVer(ver) {
def pad = ""
ver.replaceAll( "[vV]", "" ).split( /\./ ).each { pad += it.padLeft( 2, '0' ) }
return pad
}
def getThisCopyright(){"© 2020 C Steele "}