Update speaker-mood-music.groovy
[smartapps.git] / official / lock-it-at-a-specific-time.groovy
1 /**
2  *  Lock it at a specific time
3  *
4  *  Copyright 2014 Erik Thayer
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: "Lock it at a specific time",
18     namespace: "user8798",
19     author: "Erik Thayer",
20     description: "Make sure a door is locked at a specific time.  Option to add door contact sensor to only lock if closed.",
21     category: "Safety & Security",
22     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
23     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png"
24 )
25
26
27 preferences {
28   section("At this time every day") {
29     input "time", "time", title: "Time of Day"
30   }
31   section("Make sure this is locked") {
32     input "lock","capability.lock"
33   }
34   section("Make sure it's closed first..."){
35     input "contact", "capability.contactSensor", title: "Which contact sensor?", required: false
36   }
37   section( "Notifications" ) {
38     input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes", "No"]], required: false
39     input "phone", "phone", title: "Send a text message?", required: false
40   } 
41 }
42 def installed() {
43   schedule(time, "setTimeCallback")
44
45 }
46
47 def updated(settings) {
48   unschedule()
49   schedule(time, "setTimeCallback")
50 }
51
52 def setTimeCallback() {
53   if (contact) {
54     doorOpenCheck()
55   } else {
56     lockMessage()
57     lock.lock()
58   }
59 }
60 def doorOpenCheck() {
61   def currentState = contact.contactState
62   if (currentState?.value == "open") {
63     def msg = "${contact.displayName} is open.  Scheduled lock failed."
64     log.info msg
65     if (sendPushMessage) {
66       sendPush msg
67     }
68     if (phone) {
69       sendSms phone, msg
70     }
71   } else {
72     lockMessage()
73     lock.lock()
74   }
75 }
76
77 def lockMessage() {
78   def msg = "Locking ${lock.displayName} due to scheduled lock."
79   log.info msg
80   if (sendPushMessage) {
81     sendPush msg
82   }
83   if (phone) {
84     sendSms phone, msg
85   }
86 }