-
Notifications
You must be signed in to change notification settings - Fork 27
/
Hue Party Mode.groovy
119 lines (100 loc) · 4.19 KB
/
Hue Party Mode.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
/*************************************************************************************
* Hue Party Mode
*
* Author: Mitch Pond
* Date: 2015-05-29
Copyright (c) 2015, Mitch Pond
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************************/
definition(
name: "Hue Party Mode",
namespace: "mitchpond",
author: "Mitch Pond",
description: "Change the color of your lights randomly at an interval of your choosing.",
category: "Fun & Social",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/FunAndSocial/App-ItsPartyTime.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/FunAndSocial/App-ItsPartyTime@2x.png",
)
preferences {
section("Choose lights..."){
input "lights", "capability.colorControl", title: "Pick your lights", required: false, multiple: true
}
section("Adjust color change speed and timeout"){
input "interval", "number", title: "Color change interval (seconds)", required: false, defaultValue: 10
input "timeout", "number", title: "How long to run (minutes)", required: false, defaultValue: 60
}
}
def installed() {
settings.interval = 10 //default value: 10 seconds
settings.timeout = 60 //default value: 60 minutes
state.running = false
log.debug("Installed with settings: ${settings}")
updated()
}
def updated() {
log.debug("Updated with settings: ${settings}")
unsubscribe()
subscribe(app, onAppTouch)
for (light in lights) {
subscribeToCommand(light, "off", onLightOff)
}
}
def onLightOff(evt) {
//if one of the lights in our device list is turned off, and we are running, unschedule any pending color changes
if (state.running) {
log.info("${app.name}: One of our lights was turned off.")
stop()
}
}
def onAppTouch(evt) {
//if currently running, unschedule any scheduled function calls
//if not running, start our scheduling loop
if (state.running) {
log.debug("${app.name} is running.")
stop()
}
else if (!state.running) {
log.debug("${app.name} is not running.")
start()
}
}
def changeColor() {
if (!state.running) return //just return without doing anything in case unschedule() doesn't finish before next function call
//calculate a random color, send the setColor command, then schedule our next execution
log.info("${app.name}: Running scheduled color change")
def nextHue = new Random().nextInt(101)
def nextSat = new Random().nextInt(51)+50
//def nextColor = Integer.toHexString(new Random().nextInt(0x1000000))
log.debug nextColor
lights*.setColor(hue: nextHue, saturation: nextSat)
runIn(settings.interval, changeColor)
}
def start() {
log.debug("${app.name}: Beginning execution...")
state.running = true
lights*.on()
changeColor()
runIn(settings.timeout*60, stop)
}
def stop() {
log.debug("${app.name}: Stopping execution...")
unschedule()
state.running = false
}