forked from justinlhudson/SmartThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModeSwitchActivator.groovy
82 lines (71 loc) · 1.68 KB
/
ModeSwitchActivator.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
definition(
name: "Mode Switch Activator",
namespace: "Operations",
author: "justinlhudson",
description: "Switches on when mode active set and off otherwise.",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]")
preferences {
section("Settings") {
input "activeMode", "mode", title:"Active Mode", multiple:false, required:true
input "switches", "capability.switch", title: "Switch", required: false, multiple: true
}
}
def installed() {
initialize()
}
def updated() {
unsubscribe()
unschedule()
initialize()
// update on reset/restart
modeHandler([value: location.mode])
}
def modeHandler(evt)
{
if (evt.value == settings.activeMode) {
switches_on()
}
else {
switches_off()
}
}
def resetHandler(evt)
{
updated()
}
private def initialize() {
subscribe(location, "mode", modeHandler)
// HACK: keep alive
subscribe(location, "sunset", resetHandler)
subscribe(location, "sunrise", resetHandler)
}
private def switches_off() {
log.debug "switches_off"
def x = 6
x.times { n ->
settings.switches.each {
if ( it != null && it.currentSwitch != "off") {
it.off()
}
}
if( n > 0) {
pause(500)
}
}
}
private def switches_on() {
log.debug "switches_on"
def x = 6
x.times { n ->
settings.switches.each {
if ( it != null && it.currentSwitch != "on") {
it.on()
}
}
if( n > 0) {
pause(500)
}
}
}