Update hue-minimote.groovy
[smartapps.git] / official / sleepy-time.groovy
1 /**
2  *  Sleepy Time
3  *
4  *  Copyright 2014 Physical Graph Corporation
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
17 definition(
18     name: "Sleepy Time",
19     namespace: "smartthings",
20     author: "SmartThings",
21     description: "Use Jawbone sleep mode events to automatically execute Hello, Home phrases. Automatially put the house to bed or wake it up in the morning by pushing the button on your UP.",
22     category: "SmartThings Labs",
23         iconUrl: "https://s3.amazonaws.com/smartapp-icons/Partner/jawbone-up.png",
24         iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Partner/jawbone-up@2x.png"
25 )
26
27 preferences {
28         page(name: "selectPhrases")
29 }
30
31 def selectPhrases() {
32     dynamicPage(name: "selectPhrases", title: "Configure Your Jawbone Phrases.", install: true, uninstall: true) {              
33                 section("Select your Jawbone UP") {
34                         input "jawbone", "device.jawboneUser", title: "Jawbone UP", required: true, multiple: false,  submitOnChange:true
35                 }
36         
37                 def phrases = location.helloHome?.getPhrases()*.label
38                 if (phrases) {
39                 phrases.sort()
40                         section("Hello Home Actions") {
41                                 log.trace phrases
42                                 input "sleepPhrase", "enum", title: "Enter Sleep Mode (Bedtime) Phrase", required: false, options: phrases,  submitOnChange:true
43                                 input "wakePhrase", "enum", title: "Exit Sleep Mode (Waking Up) Phrase", required: false, options: phrases,  submitOnChange:true
44                         }
45                 }
46     }
47 }
48
49 def installed() {
50         log.debug "Installed with settings: ${settings}"
51
52         initialize()
53     
54     log.debug "Subscribing to sleeping events."
55     
56         subscribe (jawbone, "sleeping", jawboneHandler)
57     
58 }
59
60 def updated() {
61         log.debug "Updated with settings: ${settings}"
62
63         unsubscribe()
64     
65     log.debug "Subscribing to sleeping events."
66         
67         subscribe (jawbone, "sleeping", jawboneHandler)
68     
69         initialize()
70 }
71
72 def initialize() {
73         // TODO: subscribe to attributes, devices, locations, etc.
74 }
75
76 def jawboneHandler(evt) {
77         log.debug "In Jawbone Event Handler, Event Name = ${evt.name}, Value = ${evt.value}"
78         if (evt.value == "sleeping" && sleepPhrase) {
79         log.debug "Sleeping"
80         sendNotificationEvent("Sleepy Time performing \"${sleepPhrase}\" for you as requested.")
81         location.helloHome.execute(settings.sleepPhrase)
82     }
83     else if (evt.value == "not sleeping" && wakePhrase) {
84         log.debug "Awake"
85         sendNotificationEvent("Sleepy Time performing \"${wakePhrase}\" for you as requested.")
86                 location.helloHome.execute(settings.wakePhrase)
87     }
88         
89 }