Update turn-on-before-sunset.groovy
[smartapps.git] / third-party / Hue-Party-Mode.groovy
1 /*************************************************************************************
2 *  Hue Party Mode
3 *
4 *  Author: Mitch Pond
5 *  Date: 2015-05-29
6
7 Copyright (c) 2015, Mitch Pond
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12
13 1. Redistributions of source code must retain the above copyright notice, this
14 list of conditions and the following disclaimer.
15 2. Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 *************************************************************************************/
31
32 definition(
33     name: "Hue Party Mode",
34     namespace: "mitchpond",
35     author: "Mitch Pond",
36     description: "Change the color of your lights randomly at an interval of your choosing.",
37     category: "Fun & Social",
38     iconUrl: "https://s3.amazonaws.com/smartapp-icons/FunAndSocial/App-ItsPartyTime.png",
39     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/FunAndSocial/App-ItsPartyTime@2x.png",
40 )
41
42 preferences {
43     section("Choose lights..."){
44         input "lights", "capability.colorControl", title: "Pick your lights", required: false, multiple: true
45     }
46     section("Adjust color change speed and timeout"){
47         input "interval", "number", title: "Color change interval (seconds)",   required: false, defaultValue: 10
48         input "timeout",  "number", title: "How long to run (minutes)", required: false, defaultValue: 60
49     }
50 }
51
52 def installed() {
53     settings.interval = 10    //default value: 10 seconds
54     settings.timeout  = 60    //default value: 60 minutes
55     state.running     = false
56     log.debug("Installed with settings: ${settings}")
57     updated()
58 }
59
60 // input "light", "capability.colorControl"
61 def updated() {
62     log.debug("Updated with settings: ${settings}")
63     unsubscribe()
64     subscribe(app, onAppTouch)
65     for (light in lights) {
66         subscribe(light, "switch.off", onLightOff)
67     }
68     
69     
70 }
71
72 def onLightOff(evt) {
73     //if one of the lights in our device list is turned off, and we are running, unschedule any pending color changes
74     if (state.running) {
75         log.info("${app.name}: One of our lights was turned off.")
76         stop()
77     }
78 }
79
80 def onAppTouch(evt) {
81     //if currently running, unschedule any scheduled function calls
82     //if not running, start our scheduling loop
83
84     if (state.running) {
85         log.debug("${app.name} is running.")
86         stop()
87     }
88     else if (!state.running) {
89         log.debug("${app.name} is not running.")
90         start()
91     }
92
93 }
94
95 def changeColor() {
96     if (!state.running) return  //just return without doing anything in case unschedule() doesn't finish before next function call
97
98     //calculate a random color, send the setColor command, then schedule our next execution
99     log.info("${app.name}: Running scheduled color change")
100     def nextHue = new Random().nextInt(101)
101     def nextSat = new Random().nextInt(51)+50
102     //def nextColor = Integer.toHexString(new Random().nextInt(0x1000000))
103     log.debug nextColor
104     lights*.setColor(hue: nextHue, saturation: nextSat)
105     runIn(settings.interval, changeColor)
106 }
107
108 def start() {
109     log.debug("${app.name}: Beginning execution...")
110     state.running = true
111     lights*.on()
112     changeColor()
113     runIn(settings.timeout*60, stop)
114 }
115
116 def stop() {
117     log.debug("${app.name}: Stopping execution...")
118     unschedule()
119     state.running = false
120 }