Update README.md
[smartapps.git] / official / working-from-home.groovy
1 /**
2  *  Working From Home
3  *
4  *  Copyright 2014 George Sudarkoff
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: "Working From Home",
19     namespace: "com.sudarkoff",
20     author: "George Sudarkoff",
21     description: "If after a particular time of day a certain person is still at home, trigger a 'Working From Home' action.",
22     category: "Mode Magic",
23     iconUrl: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/Cat-ModeMagic.png",
24     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/Cat-ModeMagic@2x.png"
25 )
26
27 preferences {
28     page (name:"configActions")
29 }
30
31 def configActions() {
32     dynamicPage(name: "configActions", title: "Configure Actions", uninstall: true, install: true) {
33         section ("When this person") {
34             input "person", "capability.presenceSensor", title: "Who?", multiple: false, required: true
35             input "days", "enum", title: "Set for specific day(s) of the week", multiple: true, required: false,
36                 options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
37         }
38         section ("Still at home past") {
39             input "timeOfDay", "time", title: "What time?", required: true
40         }
41
42         def phrases = location.helloHome?.getPhrases()*.label
43         if (phrases) {
44             phrases.sort()
45             section("Perform this action") {
46                 input "wfhPhrase", "enum", title: "\"Hello, Home\" action", required: true, options: phrases
47             }
48         }
49
50         section (title: "More options", hidden: hideOptions(), hideable: true) {
51             input "sendPushMessage", "bool", title: "Send a push notification?"
52             input "phone", "phone", title: "Send a Text Message?", required: false
53             //input "days", "enum", title: "Set for specific day(s) of the week", multiple: true, required: false,
54             //    options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
55         }
56
57         section([mobileOnly:true]) {
58             label title: "Assign a name", required: false
59             mode title: "Set for specific mode(s)", required: false
60         }
61     }
62 }
63
64 def installed() {
65     initialize()
66 }
67
68 def updated() {
69     unschedule()
70     initialize()
71 }
72
73 def initialize() {
74     schedule("someTime", "checkPresence")
75     //schedule(timeToday(timeOfDay, location?.timeZone), "checkPresence")
76     //if (customName) {
77     //  app.setTitle(customName)
78     //}
79 }
80
81 def checkPresence() {
82     if (daysOk && modeOk) {
83         if (person.latestValue("presence") == "present") {
84             log.debug "${person} is present, triggering WFH action."
85             location.helloHome.execute(settings.wfhPhrase)
86             def message = "${location.name} executed '${settings.wfhPhrase}' because ${person} is home."
87             send(message)
88         }
89     }
90 }
91
92 private send(msg) {
93     if (sendPushMessage != "No") {
94         sendPush(msg)
95     }
96
97     if (phone) {
98         sendSms(phone, msg)
99     }
100
101     log.debug msg
102 }
103
104 private getModeOk() {
105     def result = !modes || modes.contains(location.mode)
106     result
107 }
108
109 private getDaysOk() {
110     def result = true
111     if (days) {
112         def df = new java.text.SimpleDateFormat("EEEE")
113         if (location.timeZone) {
114             df.setTimeZone(location.timeZone)
115         }
116         else {
117             df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
118         }
119         def day = df.format(new Date())
120         result = days.contains(day)
121     }
122     result
123 }
124
125 private hideOptions() {
126     (days || modes)? false: true
127 }
128