Update WorkingFromHome.groovy
[smartapps.git] / third-party / WorkingFromHome.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/smartthings-device-icons/Office/office22-icn.png",
24     iconX2Url: "https://s3.amazonaws.com/smartthings-device-icons/Office/office22-icn@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(timeToday(timeOfDay, location.timeZone), "checkPresence")
75     //if (customName) {
76     //  app.setTitle(customName)
77     //}
78 }
79
80 def checkPresence() {
81     if (daysOk && modeOk) {
82         if (person.latestValue("presence") == "present") {
83             log.debug "${person} is present, triggering WFH action."
84             location.helloHome.execute(settings.wfhPhrase)
85             def message = "${location.name} executed '${settings.wfhPhrase}' because ${person} is home."
86             send(message)
87         }
88     }
89 }
90
91 private send(msg) {
92     if (sendPushMessage != "No") {
93         sendPush(msg)
94     }
95
96     if (phone) {
97         sendSms(phone, msg)
98     }
99
100     log.debug msg
101 }
102
103 private getModeOk() {
104     def result = !modes || modes.contains(location.mode)
105     result
106 }
107
108 private getDaysOk() {
109     def result = true
110     if (days) {
111         def df = new java.text.SimpleDateFormat("EEEE")
112         if (location.timeZone) {
113             df.setTimeZone(location.timeZone)
114         }
115         else {
116             df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
117         }
118         def day = df.format(new Date())
119         result = days.contains(day)
120     }
121     result
122 }
123
124 private hideOptions() {
125     (days || modes)? false: true
126 }
127