-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAI.groovy
699 lines (558 loc) · 19.9 KB
/
AI.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
definition(
name: "stage.home.ai",
namespace: "app.home.ai",
author: "Eric Greer",
description: "SmartThings SmartApp for stage.app.home.ai.",
category: "Fun & Social",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]",
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]"
)
// These are preferences displayed in the smart phone app
preferences {
// we need a settings section to enable subscriptions
section("Pick which devices home.ai will help you automate:"){
input "motion", "capability.motionSensor", title: "Choose motion sensors", required: false, multiple: true
input "contact", "capability.contactSensor", title: "Choose contact sensors", required: false, multiple: true
input "lightswitch", "capability.switch", title: "Choose normal power switches", required: false, multiple: true
input "lightswitchlevel", "capability.switchLevel", title: "Choose dimmer power switches", required: false, multiple: true
input "presence", "capability.presenceSensor", title: "Choose presence sensors", required: false, multiple: true
input "tempSensor", "capability.temperatureMeasurement", title: "Choose temperature sensors", required: false, multiple: true
input "waterSensor", "capability.waterSensor", title: "Choose water sensors", required: false, multiple: true
input "humidity", "capability.relativeHumidityMeasurement", title: "Choose humidity sensors", required: false, multiple: true
input "lock", "capability.lock", title: "Pick Door Locks", required: false, multiple: true
input "garagedoor", "capability.garageDoorControl", title: "Pick garage doors", required: false, multiple: true
input "touchsensor", "capability.touchSensor", title: "Pick touch sensors", required: false, multiple: true
input "speechparser", "capability.speechRecognition", title: "Pick speech recognizers", required: false, multiple: true
input "soundsensor", "capability.soundSensor", title: "Pick sound sensors", required: false, multiple: true
input "smokedetector", "capability.smokeDetector", title: "Pick smoke detectors", required: false, multiple: true
input "sleepsensor", "capability.sleepSensor", title: "Pick sleep sensors", required: false, multiple: true
input "carbonsensor", "capability.carbonMonoxideDetector", title: "Pick carbon monoxide detectors", required: false, multiple: true
input "button", "capability.button", title: "Pick buttons", required: false, multiple: true
input "beacon", "capability.beacon", title: "Pick beacons", required: false, multiple: true
input "alarm", "capability.alarm", title: "Pick alarms", required: false, multiple: true
input "thermostat", "capability.thermostat", title: "Pick thermostats", required: false, multiple: true
input "voltage", "capability.voltageMeasurement", title: "Pick voltage sensors", required: false, multiple: true
input "windowshade", "capability.windowShade", title: "Pick window shades", required: false, multiple: true
}
}
// vlaues for security system are 'away', 'stay', or 'off'
// off security
def offSecurity() {
sendLocationEvent(
name: "alarmSystemStatus",
value: "off",
displayed: false,
isStateChange: true)
}
// stay security
def staySecurity() {
sendLocationEvent(
name: "alarmSystemStatus",
value: "stay",
displayed: false,
isStateChange: true)
}
// away security
def awaySecurity() {
sendLocationEvent(
name: "alarmSystemStatus",
value: "away",
displayed: false,
isStateChange: true)
}
// sets window shade open temperature
def setWindowShadeOpen() {
def deviceID = params.deviceID
log.debug("setWindowShadeOpen command recieved ${deviceID}")
windowshade.each {
if (it.id == deviceID) {
log.debug("Operating window shade because it is the one specified: ${deviceID}");
it.open()
} else {
log.debug("NOT operting window shade because it is not the one specified: ${deviceID}");
}
}
}
// sets window shade close temperature
def setWindowShadeClosed() {
def deviceID = params.deviceID
log.debug("setWindowShadeClosed command recieved ${deviceID}")
windowshade.each {
if (it.id == deviceID) {
log.debug("Operating window shade because it is the one specified: ${deviceID}");
it.close()
} else {
log.debug("NOT operting window shade because it is not the one specified: ${deviceID}");
}
}
}
// sets thermostat heating temperature
def setThermostatHeatTemp() {
def deviceID = params.deviceID
log.debug("setThermostatHeat command recieved ${deviceID}")
thermostat.each {
if (it.id == deviceID) {
log.debug("Operating thermostat because it is the one specified: ${deviceID}");
it.setHeatingSetpoint(params.temp)
} else {
log.debug("NOT operting thermostat because it is not the one specified: ${deviceID}");
}
}
}
// sets thermostat cooling temperature
def setThermostatCoolTemp() {
def deviceID = params.deviceID
log.debug("setThermostatCool command recieved ${deviceID}")
thermostat.each {
if (it.id == deviceID) {
log.debug("Operating thermostat because it is the one specified: ${deviceID}");
it.setCoolingSetpoint(params.temp)
} else {
log.debug("NOT operting thermostat because it is not the one specified: ${deviceID}");
}
}
}
// sets thermostat off
def setThermostatOff() {
def deviceID = params.deviceID
log.debug("setThermostatOff command recieved ${deviceID}")
thermostat.each {
if (it.id == deviceID) {
log.debug("Operating thermostat because it is the one specified: ${deviceID}");
it.off()
} else {
log.debug("NOT operting thermostat because it is not the one specified: ${deviceID}");
}
}
}
// sets thermostat to heat
def setThermostatHeat() {
def deviceID = params.deviceID
log.debug("setThermostatHeat command recieved ${deviceID}")
thermostat.each {
if (it.id == deviceID) {
log.debug("Operating thermostat because it is the one specified: ${deviceID}");
it.heat()
} else {
log.debug("NOT operting thermostat because it is not the one specified: ${deviceID}");
}
}
}
// sets thermostat to cool
def setThermostatCool() {
def deviceID = params.deviceID
log.debug("setThermostatCool command recieved ${deviceID}")
thermostat.each {
if (it.id == deviceID) {
log.debug("Operating thermostat because it is the one specified: ${deviceID}");
it.cool()
} else {
log.debug("NOT operting thermostat because it is not the one specified: ${deviceID}");
}
}
}
// sets thermostat mode
def setThermostatMode() {
def deviceID = params.deviceID
log.debug("setThermostatMode command recieved ${deviceID}")
thermostat.each {
if (it.id == deviceID) {
log.debug("Operating thermostat because it is the one specified: ${deviceID}");
it.setThermostatMode(params.mode)
} else {
log.debug("NOT operting thermostat because it is not the one specified: ${deviceID}");
}
}
}
// sets thermostat fan mode
def setThermostatFanMode() {
def deviceID = params.deviceID
log.debug("setThermostatFanMode command recieved ${deviceID}")
thermostat.each {
if (it.id == deviceID) {
log.debug("Operating thermostat because it is the one specified: ${deviceID}");
it.setThermostatFanMode(params.mode)
} else {
log.debug("NOT operting thermostat because it is not the one specified: ${deviceID}");
}
}
}
// sends an alarm strobe
def strobeAlarm() {
def deviceID = params.deviceID
log.debug("Alarm strobe command recieved ${deviceID}")
alarm.each {
if (it.id == deviceID) {
log.debug("Operating alarm because it is the one specified: ${deviceID}");
it.strobe()
} else {
log.debug("NOT operting alarm because it is not the one specified: ${deviceID}");
}
}
}
// sends an alarm siren
def sirenAlarm() {
def deviceID = params.deviceID
log.debug("Alarm siren command recieved ${deviceID}")
alarm.each {
if (it.id == deviceID) {
log.debug("Operating alarm because it is the one specified: ${deviceID}");
it.siren()
} else {
log.debug("NOT operting alarm because it is not the one specified: ${deviceID}");
}
}
}
// disables an alarm siren
def silenceAlarm() {
def deviceID = params.deviceID
log.debug("Alarm silence command recieved ${deviceID}")
alarm.each {
if (it.id == deviceID) {
log.debug("Operating alarm because it is the one specified: ${deviceID}");
it.off()
} else {
log.debug("NOT operting alarm because it is not the one specified: ${deviceID}");
}
}
}
// opens a garage door
def openGarage() {
def deviceID = params.deviceID
log.debug("Open Garage command recieved ${deviceID}")
garagedoor.each {
if (it.id == deviceID) {
log.debug("Operating garage door because it is the one specified: ${deviceID}");
it.open()
} else {
log.debug("NOT operting garage door device because it is not the one specified: ${deviceID}");
}
}
}
// closes a garage door
def closeGarage() {
def deviceID = params.deviceID
log.debug("Close Garage command recieved ${deviceID}")
garagedoor.each {
if (it.id == deviceID) {
log.debug("Operating garage door because it is the one specified: ${deviceID}");
it.close()
} else {
log.debug("NOT operting garage door device because it is not the one specified: ${deviceID}");
}
}
}
// lock locks a door lock
def lockDoor() {
def deviceID = params.deviceID
log.debug("Lock command recieved ${deviceID}")
lock.each {
if (it.id == deviceID) {
log.debug("Operating lock device because it is the one specified: ${deviceID}");
it.lock()
} else {
log.debug("NOT operting lock device because it is not the one specified: ${deviceID}");
}
}
}
// unlock unlocks a door lock
def unlockDoor() {
def deviceID = params.deviceID
log.debug("Unlock command recieved ${deviceID}")
lock.each {
if (it.id == deviceID) {
log.debug("Operating lock device because it is the one specified: ${deviceID}");
it.unlock()
} else {
log.debug("NOT operting lock device because it is not the one specified: ${deviceID}");
}
}
}
// turns on a wall switch as instructed from the homeai webservice
def switchOn() {
def deviceID = params.deviceID
log.debug("Switch on command recieved ${deviceID}")
lightswitch.each {
if (it.id == deviceID) {
log.debug("Operating switch device because it is the one specified: ${deviceID}");
it.on()
} else {
log.debug("Skipping switch device because it is not the one specified: ${deviceID}");
}
}
}
// turns off a wall switch as instructed from the homeai webservice
def switchOff() {
def deviceID = params.deviceID
log.debug("Switch off desired for ${deviceID}")
lightswitch.each {
if (it.id == deviceID) {
log.debug("Operating switch device because it is the one specified: ${deviceID}");
it.off()
} else {
log.debug("Skipping switch device because it is not the one specified: ${deviceID}");
}
}
}
// fetch the id of this smartthings hub
def hubId() {
log.debug("hub id requested.")
def response = [hubId: location.hubs.id[0]]
}
// This handles requests for device inventories
def inventory() {
def response = []
lightswitch.each {
response << [name: it.displayName, value: it.currentValue("switch"), deviceId: it.id, type: "lightSwitch"]
}
contact.each {
response << [name: it.displayName, value: it.currentValue("contact"), deviceId: it.id, type: "contact"]
}
motion.each {
response << [name: it.displayName, value: it.currentValue("motion"), deviceId: it.id, type: "motion"]
}
presence.each {
response << [name: it.displayName, value: it.currentValue("presence"), deviceId: it.id, type: "presence"]
}
tempSensor.each {
response << [name: it.displayName, value: it.currentValue("temperature"), deviceId: it.id, type: "tempSensor"]
}
waterSensor.each {
response << [name: it.displayName, value: it.currentValue("water"), deviceId: it.id, type: "waterSensor"]
}
humidity.each {
response << [name: it.displayName, value: it.currentValue("humidity"), deviceId: it.id, type: "humiditySensor"]
}
lock.each {
response << [name: it.displayName, value: it.currentValue("lock"), deviceId: it.id, type: "lock"]
}
garagedoor.each {
response << [name: it.displayName, value: it.currentValue("door"), deviceId: it.id, type: "garagedoor"]
}
touchsensor.each {
response << [name: it.displayName, value: it.currentValue("touch"), deviceId: it.id, type: "touchsensor"]
}
speechparser.each {
response << [name: it.displayName, value: it.currentValue("phraseSpoken"), deviceId: it.id, type: "speechparser"]
}
soundsensor.each {
response << [name: it.displayName, value: it.currentValue("sound"), deviceId: it.id, type: "sound"]
}
smokedetector.each {
response << [name: it.displayName, value: it.currentValue("smoke"), deviceId: it.id, type: "smoke"]
}
sleepsensor.each {
response << [name: it.displayName, value: it.currentValue("sleeping"), deviceId: it.id, type: "sleepsensor"]
}
carbonsensor.each {
response << [name: it.displayName, value: it.currentValue("carbonMonoxide"), deviceId: it.id, type: "carbonsensor"]
}
button.each {
response << [name: it.displayName, value: it.currentValue("button"), deviceId: it.id, type: "button"]
}
beacon.each {
response << [name: it.displayName, value: it.currentValue("presence"), deviceId: it.id, type: "beacon"]
}
alarm.each {
response << [name: it.displayName, value: it.currentValue("alarm"), deviceId: it.id, type: "alarm"]
}
thermostat.each {
response << [name: it.displayName, value: it.currentValue("thermostatMode"), deviceId: it.id, type: "thermostat"]
}
voltage.each {
response << [name: it.displayName, value: it.currentValue("voltage"), deviceId: it.id, type: "voltage"]
}
windowshade.each {
response << [name: it.displayName, value: it.currentValue("windowShade"), deviceId: it.id, type: "windowshade"]
}
log.debug("Inventory request processed. Response: " + response)
return response
}
// After the user hits the 'install' button in the mobile app
def installed() {
initialize()
}
// After app settings are changed. All subscriptions are wiped before this is invoked by smartthings.
def updated() {
unsubscribe()
initialize()
}
// This appears to be what the tutorials meant to use in the examples
def initialize() {
// motion sensor subscription
subscribe(motion, "motion", eventForwarder)
// Contact sensor subscription
subscribe(contact, "contact", eventForwarder)
// power plug subscription
subscribe(lightswitch, "switch", eventForwarder)
// presence sensor subscription
subscribe(presence, "presence", eventForwarder)
// temperature sensor subscription
subscribe(tempSensor, "temperature", eventForwarder)
// water sensor subscription
subscribe(waterSensor, "water", eventForwarder)
// humidity sensor subscription
subscribe(humidity, "humidity", eventForwarder)
// lock subscription
subscribe(lock, "lock", eventForwarder)
// garage door subscription
subscribe(garagedoor, "garagedoor", eventForwarder)
// touch sensor subscription
subscribe(touchsensor, "touchsensor", eventForwarder)
// speech parser subscription
subscribe(speechparser, "phraseSpoken", eventForwarder)
// sound sensor subscription
subscribe(soundsensor, "sound", eventForwarder)
// smoke detector subscription
subscribe(smokedetector, "smoke", eventForwarder)
// sleep sensor subscription
subscribe(sleepsensor, "sleeping", eventForwarder)
// carbon monoxide sensor subscription
subscribe(carbonsensor, "carbonMonoxide", eventForwarder)
// button subscription
subscribe(button, "button", eventForwarder)
// beacon subscription
subscribe(beacon, "presence", eventForwarder)
// alarm subscription
subscribe(alarm, "alarm", eventForwarder)
// thermostat subscriptions
subscribe(thermostat, "temperature", eventForwarder)
subscribe(thermostat, "heatingSetpoint", eventForwarder)
subscribe(thermostat, "coolingSetpoint", eventForwarder)
subscribe(thermostat, "thermostatSetpoint", eventForwarder)
subscribe(thermostat, "thermostatMode", eventForwarder)
subscribe(thermostat, "thermostatFanMode", eventForwarder)
subscribe(thermostat, "thermostatOperatingState", eventForwarder)
// voltage subscription
subscribe(voltage, "voltage", eventForwarder)
// window shade subscription
subscribe(windowshade, "windowShade", eventForwarder)
}
// This is used to forward events to the home.ai webservice
def eventForwarder(evt) {
def hubId = location.hubs.id[0]
log.debug(params.uri + " " + params.path)
log.debug("FORWARDING EVENT" + evt.deviceId + " " + evt.value + " " + hubId)
def deviceId = evt.deviceId
def deviceState = evt.value
def params = [
uri: "https://stage.app.home.ai",
path: "/smartThingsPostback/stateChange/${hubId}/${deviceId}/${deviceState}"
]
log.info(params)
httpGet(params)
}
// Mappings that serve web requests against our smart app
mappings {
path("/inventory") {
action: [
GET: "inventory"
]
}
path("/hubId") {
action: [
GET: "hubId"
]
}
path("/switchOn/:deviceID") {
action: [
GET: "switchOn"
]
}
path("/switchOff/:deviceID") {
action: [
GET: "switchOff"
]
}
path("/lock/:deviceID") {
action: [
GET: "lockDoor"
]
}
path("/unlock/:deviceID") {
action: [
GET: "unlockDoor"
]
}
path("/opengarage/:deviceID") {
action: [
GET: "openGarage"
]
}
path("/closegarage/:deviceID") {
action: [
GET: "closeGarage"
]
}
path("/strobeAlarm/:deviceID") {
action: [
GET: "strobeAlarm"
]
}
path("/sirenAlarm/:deviceID") {
action: [
GET: "sirenAlarm"
]
}
path("/silenceAlarm/:deviceID") {
action: [
GET: "silenceAlarm"
]
}
path("/setThermostatHeatTemp/:deviceID/:temp") {
action: [
GET: "setThermostatHeatTemp"
]
}
path("/setThermostatCoolTemp/:deviceID/:temp") {
action: [
GET: "setThermostatCoolTemp"
]
}
path("/setThermostatHeat/:deviceID") {
action: [
GET: "setThermostatHeat"
]
}
path("/setThermostatCool/:deviceID") {
action: [
GET: "setThermostatCool"
]
}
path("/setThermostatMode/:deviceID/:mode") {
action: [
GET: "setThermostatMode"
]
}
path("/setThermostatFanMode/:deviceID/:mode") {
action: [
GET: "setThermostatFanMode"
]
}
path("/closeWindowShade/:deviceID") {
action: [
GET: "setWindowShadeClosed"
]
}
path("/openWindowShade/:deviceID") {
action: [
GET: "setWindowShadeOpen"
]
}
path("/awaySecurity") {
action: [
GET: "awaySecurity"
]
}
path("/staySecurity") {
action: [
GET: "staySecurity"
]
}
path("/offSecurity") {
action: [
GET: "offSecurity"
]
}
}