Update step-notifier.groovy
[smartapps.git] / official / Light_Rule.groovy
1 /**
2  *  Light Rule
3  *
4  *  Copyright 2016 Tim Slagle
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  *  in compliance with the License. You may obtain a copy of the License at:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13  *  for the specific language governing permissions and limitations under the License.
14  *
15  */
16 definition(
17     name: "Light Rule",
18     namespace: "tslagle13",
19     author: "Tim Slagle",
20     description: "Light rule child app for \"Motion Activated Light\"",
21     category: "Convenience",
22     parent: "tslagle13:Contact Activated Lighting",
23     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
24     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
25     iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
26
27
28 preferences {
29         page name: "mainPage", title: "Automate Lights & Switches", install: false, uninstall: true, nextPage: "namePage"
30     page name: "namePage", title: "Automate Lights & Switches", install: true, uninstall: true
31 }
32
33 def mainPage() {
34         dynamicPage(name: "mainPage") {
35         section("Which contact sensor?") {
36             input "contactSensor", "capability.contactSensor", title: "Which contact sensor(s)?", multiple: true
37         }
38         section("Which light would you like to turn on?") {
39             input "light", "capability.switch", title: "Which light?", multiple: true
40         }
41         section("Extras") {
42             input "contactInactive", "bool", title: "Turn off if contacts closes?", submitOnChange: true
43             if (contactInactive) {
44                 input "delay", "number", title: "After how many seconds?", required: false
45             }
46         }
47     }
48 }
49
50 def namePage() {
51         if (!overrideLabel) {
52         // if the user selects to not change the label, give a default label
53         def l = "$contactSensor turns on $light"
54         log.debug "will set default label of $l"
55         app.updateLabel(l)
56     }
57     dynamicPage(name: "namePage") {
58         if (overrideLabel) {
59             section("Automation name") {
60                 label title: "Enter custom name", defaultValue: app.label, required: false
61             }
62         } else {
63             section("Automation name") {
64                 paragraph app.label
65             }
66         }
67         section {
68             input "overrideLabel", "bool", title: "Edit automation name", defaultValue: "false", required: "false", submitOnChange: true
69         }
70     }
71 }
72
73 def installed() {
74         log.debug "Installed with settings: ${settings}"
75
76         initialize()
77 }
78
79 def updated() {
80         log.debug "Updated with settings: ${settings}"
81
82         unsubscribe()
83         initialize()
84 }
85
86 def initialize() {
87         subscribe (contactSensor, "contact" , contactHandler)
88 }
89
90 def contactHandler(evt){
91         log.debug evt.name
92     log.debug evt.value
93     log.debug evt.date
94     log.debug evt.isStateChange()
95     
96     if (evt.value == "open") {
97         turnLightOn()
98     }
99     else if (contactInactive) {
100         runIn(delay , turnLightOff)
101     }
102 }
103
104 def turnLightOn() {
105         light.on()
106 }
107
108 def turnLightOff() {
109         light.off()
110 }