/
time-debounced-off-switch.goovy
183 lines (149 loc) · 5.39 KB
/
time-debounced-off-switch.goovy
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
definition(
name: "Time-Debounced off switches",
namespace: "thefabio",
author: "The Fabio",
description: "Configure switches to be turned off after a number of seconds, resetting the counter of seconds per switch every time the switch is turned on",
category: "Convenience",
iconUrl: "",
iconX2Url: "",
iconX3Url: "",
importUrl: "https://raw.githubusercontent.com/TheFabio/hubitat/master/src/time-debounced-off-switch.goovy",
singleInstance: true
)
preferences {
page(name: "mainPage")
page(name: "deviceSpecificPage")
}
def mainPage() {
dynamicPage(name:"mainPage", uninstall:true, install:true) {
section () {
paragraph "Select the switches to which the time-Debounced Switch-Off applies to"
paragraph "Every time a selected switch is turned off, the Switch-Off event is removed"
paragraph "Every time a selected switch is turned on, the Switch-Off event is removed and re-scheduled according to the configured delay"
input "selectedSwitches",
"capability.switch",
title: "Select Switches",
multiple: true,
hideWhenEmpty: false,
required: false
input name: "defaultDelay",
type: "number",
title: "Default Switch-off delay (in seconds)",
defaultValue: 900 // 15 minutes
required: true
}
section(){
input name: "debugMode",
type: "bool",
title: "Write debug logs",
defaultValue: false
}
section(){
href("deviceSpecificPage", title: "Device specific Switch-off delays")
}
}
}
def deviceSpecificPage(){
dynamicPage(name:"deviceSpecificPage", uninstall:true, install:true, title:"Device specific Switch-off delays") {
section(){
href("mainPage", title: "Back to Main")
}
section () {
paragraph "When set to 0, uses the Default Switch-off delay, in seconds (${defaultDelay})"
selectedSwitches.sort{a,b -> a.label <=> b.label}.each {
input name: "${it.deviceNetworkId}.delay",
type: "number",
title: it.label,
defaultValue: 0
}
}
}
}
import groovy.json.JsonSlurper
// default methods - begin
def installed() { // This method is called when the app is first installed.
setupScheduler()
}
def updated() { // This method is called when the app settings are updated
subscribeSelectedDevices()
}
// default methods - end
def setupScheduler() {
unschedule()
schedule("0/2 * * ? * *", "switchOffLoop") // every 2 seconds
}
def deviceSwitchState(targetDevice) {
if (targetDevice == null || targetDevice.currentState('switch') == null) return null;
return targetDevice.currentState('switch').value
}
def switchOffLoop() {
deviceIdsToRemove = []
if (state.switchOffsScheduled == null || state.switchOffsScheduled.length == 0) return;
state.switchOffsScheduled.each { scheduleItem ->
refData = new Date().format("yyyy-MM-dd HH:mm:ss")
if (scheduleItem.switchOffAt < refData.toString()) {
// time to run it
deviceIdsToRemove << scheduleItem.deviceId
targetDevice = selectedSwitches.find{selected -> selected.deviceNetworkId == scheduleItem.deviceId}
if (targetDevice != null && deviceSwitchState(targetDevice) == "on") targetDevice.off();
writeLog("switchOffLoop -- device ${targetDevice.deviceNetworkId}, switched off")
log.info("Switched Off - ${targetDevice.label}")
}
}
deviceIdsToRemove.each { cancelSwitchOff(it) }
}
def cancelSwitchOffRemovedDevices() {
deviceIdsToRemove = []
if (state.switchOffsScheduled == null || state.switchOffsScheduled.length == 0) return;
selectedDeviceIds = []
selectedSwitches.each {
selectedDeviceIds << it.deviceNetworkId
}
state.switchOffsScheduled.each { scheduleItem ->
if (selectedDeviceIds.find{selectedId -> scheduleItem.deviceId == selectedId } == null){
deviceIdsToRemove << scheduleItem.deviceId
}
}
deviceIdsToRemove.each { cancelSwitchOff(it) }
}
def subscribeSelectedDevices() {
unsubscribe()
subscribe(selectedSwitches, "switch", switchHandler)
cancelSwitchOffRemovedDevices()
}
def switchHandler(evt) {
if (evt.value == 'on') {
upsertSwitchOff(evt.device.deviceNetworkId)
}
if (evt.value == 'off') {
cancelSwitchOff(evt.device.deviceNetworkId)
}
}
def deviceSwitchOffDelay(deviceId) {
deviceDelay = settings["${deviceId}.delay"] == null ? 0 : settings["${deviceId}.delay"] as int
defaultDevicesDelay = settings["defaultDelay"] == null ? 900 : settings["defaultDelay"] as int
if (deviceDelay < 0) deviceDelay = 0
if (defaultDevicesDelay < 0) defaultDevicesDelay = 900
return deviceDelay > 0 ? deviceDelay : defaultDevicesDelay
}
def cancelSwitchOff(deviceId) {
newSwitchOffsScheduled = state.switchOffsScheduled == null ?
[] :
state.switchOffsScheduled
state.switchOffsScheduled = newSwitchOffsScheduled.findAll { it.deviceId != deviceId }
}
def upsertSwitchOff(deviceId) {
deviceDelay = deviceSwitchOffDelay(deviceId)
cancelSwitchOff(deviceId)
use (groovy.time.TimeCategory) {
switchOffAt = new Date() + deviceDelay.seconds
deviceSchedule = "{\"deviceId\": \"${deviceId}\", \"switchOffAt\": \"${switchOffAt.format("yyyy-MM-dd HH:mm:ss")}\"}"
state.switchOffsScheduled.add(new JsonSlurper().parseText(deviceSchedule))
}
}
def writeLog(logLine){
if (!debugMode){
return
}
log.debug logLine
}