Missing one official app.
[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         }
36         section ("Still at home past") {
37             input "timeOfDay", "time", title: "What time?", required: true
38         }
39
40         def phrases = location.helloHome?.getPhrases()*.label
41         if (phrases) {
42             phrases.sort()
43             section("Perform this action") {
44                 input "wfhPhrase", "enum", title: "\"Hello, Home\" action", required: true, options: phrases
45             }
46         }
47
48         section (title: "More options", hidden: hideOptions(), hideable: true) {
49             input "sendPushMessage", "bool", title: "Send a push notification?"
50             input "phone", "phone", title: "Send a Text Message?", required: false
51             input "days", "enum", title: "Set for specific day(s) of the week", multiple: true, required: false,
52                 options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
53         }
54
55         section([mobileOnly:true]) {
56             label title: "Assign a name", required: false
57             mode title: "Set for specific mode(s)", required: false
58         }
59     }
60 }
61
62 def installed() {
63     initialize()
64 }
65
66 def updated() {
67     unschedule()
68     initialize()
69 }
70
71 def initialize() {
72     schedule(timeToday(timeOfDay, location.timeZone), "checkPresence")
73     if (customName) {
74       app.setTitle(customName)
75     }
76 }
77
78 def checkPresence() {
79     if (daysOk && modeOk) {
80         if (person.latestValue("presence") == "present") {
81             log.debug "${person} is present, triggering WFH action."
82             location.helloHome.execute(settings.wfhPhrase)
83             def message = "${location.name} executed '${settings.wfhPhrase}' because ${person} is home."
84             send(message)
85         }
86     }
87 }
88
89 private send(msg) {
90     if (sendPushMessage != "No") {
91         sendPush(msg)
92     }
93
94     if (phone) {
95         sendSms(phone, msg)
96     }
97
98     log.debug msg
99 }
100
101 private getModeOk() {
102     def result = !modes || modes.contains(location.mode)
103     result
104 }
105
106 private getDaysOk() {
107     def result = true
108     if (days) {
109         def df = new java.text.SimpleDateFormat("EEEE")
110         if (location.timeZone) {
111             df.setTimeZone(location.timeZone)
112         }
113         else {
114             df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
115         }
116         def day = df.format(new Date())
117         result = days.contains(day)
118     }
119     result
120 }
121
122 private hideOptions() {
123     (days || modes)? false: true
124 }
125