Update medicine-management-temp-motion.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         section {
52             input "overrideLabel", "bool", title: "Edit automation name", defaultValue: "false", required: "false", submitOnChange: true
53         }
54         if (!overrideLabel) {
55         // if the user selects to not change the label, give a default label
56         def l = "$contactSensor turns on $light"
57         log.debug "will set default label of $l"
58     }
59     dynamicPage(name: "namePage") {
60         if (overrideLabel) {
61             section("Automation name") {
62                 label title: "Enter custom name", required: false
63             }
64         }
65     }
66 }
67
68 def installed() {
69         log.debug "Installed with settings: ${settings}"
70
71         initialize()
72 }
73
74 def updated() {
75         log.debug "Updated with settings: ${settings}"
76
77         unsubscribe()
78         initialize()
79 }
80
81 def initialize() {
82         subscribe (contactSensor, "contact" , contactHandler)
83 }
84
85 def contactHandler(evt){
86         log.debug evt.name
87     log.debug evt.value
88     log.debug evt.date
89     //log.debug evt.isStateChange()
90     
91     if (evt.value == "open") {
92         turnLightOn()
93     }
94     else if (contactInactive) {
95         runIn(delay , turnLightOff)
96     }
97 }
98
99 def turnLightOn() {
100         light.on()
101 }
102
103 def turnLightOff() {
104         light.off()
105 }