forked from sudarkoff/SmartThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartPresence.groovy
303 lines (253 loc) · 9.16 KB
/
SmartPresence.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/** Name: SmartPresence
* Author: George Sudarkoff
* Change mode based on presense and current mode.
*/
definition(
name: "SmartPresence",
namespace: "sudarkoff.com",
author: "George Sudarkoff",
description: "Change mode (by invoking various \"Hello, Home\" phrases) based on presense and current mode.",
category: "Mode Magic",
iconUrl: "https://raw.githubusercontent.com/sudarkoff/smarttings/master/SmartPresence.png",
iconX2Url: "https://raw.githubusercontent.com/sudarkoff/smarttings/master/[email protected]"
)
preferences {
page(name: "selectPhrases")
page( name:"Settings", title:"Settings", uninstall:true, install:true ) {
section("False alarm threshold (defaults to 10 min)") {
input "falseAlarmThreshold", "decimal", title: "Number of minutes", required: false
}
section("Zip code (for sunrise/sunset)") {
input "zip", "decimal", required: true
}
section("Notifications") {
input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes","No"]], required:false
}
section(title: "More options", hidden: hideOptionsSection(), hideable: true) {
input "days", "enum", title: "Only on certain days of the week", multiple: true, required: false,
options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
input "modes", "mode", title: "Only when mode is", multiple: true, required: false
}
}
}
def selectPhrases() {
def configured = (settings.awayDay && settings.awayNight && settings.homeDay && settings.awayDay)
dynamicPage(name: "selectPhrases", title: "Configure", nextPage:"Settings", uninstall: true) {
section("All of these sensors") {
input "people", "capability.presenceSensor", title: "Monitor All of These Presences", required: true, multiple: true, refreshAfterSelection:true
}
def phrases = location.helloHome?.getPhrases()*.label
if (phrases) {
phrases.sort()
section("Run This Phrase When...") {
log.trace phrases
input "sunriseAway", "enum", title: "It's Sunrise and Everybody's Away", required: true, options: phrases, refreshAfterSelection:true
input "sunsetAway", "enum", title: "It's Sunset and Everybody's Away", required: true, options: phrases, refreshAfterSelection:true
input "sunriseHome", "enum", title: "It's Sunrise and Somebody's Home", required: true, options: phrases, refreshAfterSelection:true
input "sunsetHome", "enum", title: "It's Sunset and Somebody's Home", required: true, options: phrases, refreshAfterSelection:true
input "awayDay", "enum", title: "Last Person Leaves and It's Daytime", required: true, options: phrases, refreshAfterSelection:true
input "awayNight", "enum", title: "Last Person Leaves and It's Nighttime", required: true, options: phrases, refreshAfterSelection:true
input "homeDay", "enum", title: "Somebody's Back and It's Daytime", required: true, options: phrases, refreshAfterSelection:true
input "homeNight", "enum", title: "Somebody's Back and It's Nighttime", required: true, options: phrases, refreshAfterSelection:true
}
}
}
}
def installed() {
init()
initialize()
subscribe(app)
}
def updated() {
unsubscribe()
initialize()
init()
}
def init() {
subscribe(people, "presence", presence)
checkSun();
}
def uninstalled() {
unsubscribe()
}
def initialize() {
schedule("0 0/5 * 1/1 * ? *", checkSun)
}
def checkSun() {
// TODO: Use location information if zip is not provided
def zip = settings.zip as String
def sunInfo = getSunriseAndSunset(zipCode: zip)
def current = now()
if(sunInfo.sunrise.time > current ||
sunInfo.sunset.time < current) {
state.sunMode = "sunset"
}
else {
state.sunMode = "sunrise"
}
log.info("Sunset: ${sunInfo.sunset.time}")
log.info("Sunrise: ${sunInfo.sunrise.time}")
log.info("Current: ${current}")
log.info("sunMode: ${state.sunMode}")
if(current < sunInfo.sunrise.time) {
runIn(((sunInfo.sunrise.time - current) / 1000).toInteger(), setSunrise)
}
if(current < sunInfo.sunset.time) {
runIn(((sunInfo.sunset.time - current) / 1000).toInteger(), setSunset)
}
}
def setSunrise() {
state.sunMode = "sunrise";
changeSunMode(newMode)
}
def setSunset() {
state.sunMode = "sunset";
changeSunMode(newMode)
}
def changeSunMode(newMode) {
if(allOk) {
if(everyoneIsAway() && (state.sunMode = "sunrise")) {
log.info("Sunrise but nobody's home, switching to Away mode.")
def delay = (falseAlarmThreshold != null && falseAlarmThreshold != "") ? falseAlarmThreshold * 60 : 10 * 60
runIn(delay, "setAway")
}
if(everyoneIsAway() && (state.sunMode = "sunset")) {
log.info("Sunset and nobody's home, switching to Away mode")
def delay = (falseAlarmThreshold != null && falseAlarmThreshold != "") ? falseAlarmThreshold * 60 : 10 * 60
runIn(delay, "setAway")
}
else {
log.info("Somebody's home, switching to Home mode")
setHome()
}
}
}
def presence(evt) {
if(allOk) {
if(evt.value == "not present") {
log.debug("Checking if everyone is away")
if(everyoneIsAway()) {
log.info("Everybody's gone, running Away sequence.")
def delay = (falseAlarmThreshold != null && falseAlarmThreshold != "") ? falseAlarmThreshold * 60 : 10 * 60
runIn(delay, "setAway")
}
} else {
def lastTime = state[evt.deviceId]
if (lastTime == null || now() - lastTime >= 1 * 60000) {
log.info("Somebody's back, running Home sequence")
setHome()
}
state[evt.deviceId] = now()
}
}
}
def setAway() {
if(everyoneIsAway()) {
if(state.sunMode == "sunset") {
def message = "SmartMode says \"${awayNight}\"."
log.info(message)
send(message)
location.helloHome.execute(settings.awayNight)
} else if(state.sunMode == "sunrise") {
def message = "SmartMode says \"${awayDay}\"."
log.info(message)
send(message)
location.helloHome.execute(settings.awayDay)
} else {
log.debug("Mode is the same, not evaluating.")
}
} else {
log.info("Somebody returned home before we switched to '${newAwayMode}'")
}
}
def setHome() {
log.info("Setting Home Mode!")
if(anyoneIsHome()) {
if(state.sunMode == "sunset") {
def message = "SmartMode says \"${homeNight}\"."
log.info(message)
location.helloHome.execute(settings.homeNight)
send(message)
sendSms(phone1, message)
}
if(state.sunMode == "sunrise"){
def message = "SmartMode says \"${homeDay}\"."
log.info(message)
location.helloHome.execute(settings.homeDay)
send(message)
sendSms(phone1, message)
}
}
}
private everyoneIsAway() {
def result = true
if(people.findAll { it?.currentPresence == "present" }) {
result = false
}
log.debug("everyoneIsAway: ${result}")
return result
}
private anyoneIsHome() {
def result = false
if(people.findAll { it?.currentPresence == "present" }) {
result = true
}
log.debug("anyoneIsHome: ${result}")
return result
}
private send(msg) {
if(sendPushMessage != "No") {
log.debug("Sending push message")
sendPush(msg)
}
log.debug(msg)
}
private getAllOk() {
modeOk && daysOk && timeOk
}
private getModeOk() {
def result = !modes || modes.contains(location.mode)
log.trace "modeOk = $result"
result
}
private getDaysOk() {
def result = true
if (days) {
def df = new java.text.SimpleDateFormat("EEEE")
if (location.timeZone) {
df.setTimeZone(location.timeZone)
}
else {
df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
}
def day = df.format(new Date())
result = days.contains(day)
}
log.trace "daysOk = $result"
result
}
private getTimeOk() {
def result = true
if (starting && ending) {
def currTime = now()
def start = timeToday(starting).time
def stop = timeToday(ending).time
result = start < stop ? currTime >= start && currTime <= stop : currTime <= stop || currTime >= start
}
log.trace "timeOk = $result"
result
}
private hhmm(time, fmt = "h:mm a")
{
def t = timeToday(time, location.timeZone)
def f = new java.text.SimpleDateFormat(fmt)
f.setTimeZone(location.timeZone ?: timeZone(time))
f.format(t)
}
private getTimeIntervalLabel()
{
(starting && ending) ? hhmm(starting) + "-" + hhmm(ending, "h:mm a z") : ""
}
private hideOptionsSection() {
(starting || ending || days || modes) ? false : true
}