This repository has been archived by the owner on Nov 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
AlarmResetter.groovy
141 lines (120 loc) · 2.94 KB
/
AlarmResetter.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
// Note: 20 sec (current cloud setting) timeout, watch the pause().
definition(
name: "Alarm Resetter",
namespace: "Operations",
author: "justinlhudson",
description: "Resets Alarming (incase someone comes invited without unlocking)",
category: "Safety & Security",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]")
preferences {
section("Alarming...") {
input "alarms", "capability.alarm", title:"Reset alarms", multiple:true, required:false
input "switches_reset", "capability.switch", title: "Reset switches", required: false, multiple: true
input "switches_off", "capability.switch", title: "Off switches", required: false, multiple: true
input "reset", "number", title:"Reset (seconds)", defaultValue:15
}
}
def installed() {
initialize()
}
def updated() {
unsubscribe()
initialize()
}
def resetHandler(evt)
{
if (state.lock == false) {
updated()
}
}
def appTouch(evt)
{
clear()
}
def clear(evt) {
log.debug "clear"
alarms_off()
switches_off()
state.lock = false
log.debug "unlocked"
notify("Alarm(s) Reset...")
}
def alarmHandler(evt)
{
log.debug "${evt.value}"
if( state.lock == false) {
state.lock = true
log.debug "locked"
notify("Alarm(s) Active!")
// Throwing un handled exception? but yet still runs...
// try a few then cross fingers actually worked. WT...?!?!
def x = 3 * 4
x.times { n ->
try {
runIn(settings.reset, clear, [overwrite: true])
return
} catch (all) {
pause(250)
// EAT IT!
//log.error "Something went horribly wrong!\n${all}"
}
}
}
}
private def alarms_off() {
log.debug "alarms_off"
def x = 3
x.times { n ->
try {
settings.alarms.each {
//if ( it != null && it.latestValue("alarm") != "off") {
it.off()
//}
}
}
catch (all) {
log.error "Something went horribly wrong!\n${all}"
}
if( n > 0) {
pause(1500)
}
}
}
private def switches_off() {
log.debug "switches_off"
def x = 3
x.times { n ->
try {
settings.switches_reset.each {
//if ( it != null && it.currentSwitch != "off") {
it.off()
//}
}
}
catch (all) {
log.error "Something went horribly wrong!\n${all}"
}
if( n > 0) {
pause(1500)
}
}
}
private def notify(message) {
try {
sendNotificationEvent(message)
} catch (all) {
log.error "Something went horribly wrong!\n${all}"
}
}
private def initialize() {
state.lock = false
state.cycle = 0
subscribe(switches_off, "switch", clear)
subscribe(switches_reset, "switch.on", alarmHandler)
subscribe(alarms, "alarm", alarmHandler)
subscribe(app, appTouch)
// HACK: keep alive
subscribe(location, "sunset", resetHandler)
subscribe(location, "sunrise", resetHandler)
}