Update sonos-remote-control.groovy
[smartapps.git] / official / curling-iron.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  Curling Iron
14  *
15  *  Author: SmartThings
16  *  Date: 2013-03-20
17  */
18 definition(
19     name: "Curling Iron",
20     namespace: "smartthings",
21     author: "SmartThings",
22     description: "Turns on an outlet when the user is present and off after a period of time",
23     category: "Convenience",
24     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo-switch.png",
25     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo-switch@2x.png"
26 )
27
28 preferences {
29         section("When someone's around because of...") {
30                 input name: "motionSensors", title: "Motion here", type: "capability.motionSensor", multiple: true, required: false
31                 input name: "presenceSensors", title: "And (optionally) these sensors being present", type: "capability.presenceSensor", multiple: true, required: false
32         }
33         section("Turn on these outlet(s)") {
34                 input name: "outlets", title: "Which?", type: "capability.switch", multiple: true
35         }
36         section("For this amount of time") {
37                 input name: "minutes", title: "Minutes?", type: "number", multiple: false
38         }
39 }
40
41 def installed() {
42         subscribeToEvents()
43 }
44
45 def updated() {
46         unsubscribe()
47         subscribeToEvents()
48 }
49
50 def subscribeToEvents() {
51         subscribe(motionSensors, "motion.active", motionActive)
52         subscribe(motionSensors, "motion.inactive", motionInactive)
53         subscribe(presenceSensors, "presence.not present", notPresent)
54 }
55
56 def motionActive(evt) {
57         log.debug "$evt.name: $evt.value"
58         if (anyHere()) {
59                 outletsOn()
60         }
61 }
62
63 def motionInactive(evt) {
64         log.debug "$evt.name: $evt.value"
65         if (allQuiet()) {
66                 outletsOff()
67         }
68 }
69
70 def notPresent(evt) {
71         log.debug "$evt.name: $evt.value"
72         if (!anyHere()) {
73                 outletsOff()
74         }
75 }
76
77 def allQuiet() {
78         def result = true
79         for (it in motionSensors) {
80                 if (it.currentMotion == "active") {
81                         result = false
82                         break
83                 }
84         }
85         return result
86 }
87
88 def anyHere() {
89         def result = true
90         for (it in presenceSensors) {
91                 if (it.currentPresence == "not present") {
92                         result = false
93                         break
94                 }
95         }
96         return result
97 }
98
99 def outletsOn() {
100         outlets.on()
101         unschedule("scheduledTurnOff")
102 }
103
104 def outletsOff() {
105         def delay = minutes * 60
106         runIn(delay, "scheduledTurnOff")
107 }
108
109 def scheduledTurnOff() {
110         outlets.off()
111         unschedule("scheduledTurnOff") // Temporary work-around to scheduling bug
112 }
113
114