forked from castlecole/customdevices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebHooks.groovy
236 lines (214 loc) · 9.72 KB
/
WebHooks.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
/**
* SmartThings webhooks
*
* Author: Harper Reed (@harper - [email protected])
* URL: https://github.com/harperreed/SmartThings-webhook
*/
/**
* Let's define this dude
*
*/
definition(
name: "Webhooks",
author: "Harper Reed (@harper - [email protected])",
description: "Send Smartthings events to a webhook",
category: "My Apps",
iconUrl: "https://s3.amazonaws.com/kinlane-productions/bw-icons/webhooks.png",
iconX2Url: "https://s3.amazonaws.com/kinlane-productions/bw-icons/webhooks.png"
)
/**
* Collect some preferences
* The important piece here is the webhook URL. without that we can't do anything
*
* The event selector is where we let the user select which sensor events they want to send to the webhook
*
*/
preferences {
/**
* This should be an accessible URL
* For debugging, I suggest http://requestb.in/
*/
section("Webhook URL"){
input "url", "text", title: "Webhook URL", description: "Your webhook URL", required: true
}
/**
* We will probably need to add other items as smartthings sensors grow
*
* Feel free to add any sensors that we are missing and then submit a pull request
*/
section("Choose what events you want to trigger"){
input "accelerationSensor", "capability.accelerationSensor", title: "Acceleration Sensor", required: false, multiple: true
input "alarm", "capability.alarm", title: "Alarm", required: false, multiple: true
input "battery", "capability.battery", title: "Battery", required: false, multiple: true
input "beacon", "capability.beacon", title: "Beacon", required: false, multiple: true
input "button", "capability.button", title: "Button", required: false, multiple: true
input "carbonMonoxideDetector", "capability.carbonMonoxideDetector", title: "Carbon Monoxide Detector", required: false, multiple: true
input "colorControl", "capability.colorControl", title: "Color Control", required: false, multiple: true
input "contactSensor", "capability.contactSensor", title: "Contact Sensor", required: false, multiple: true
input "doorControl", "capability.doorControl", title: "Door Control", required: false, multiple: true
input "energyMeter", "capability.energyMeter", title: "Energy Meter", required: false, multiple: true
input "illuminanceMeasurement", "capability.illuminanceMeasurement", title: "Illuminance Measurement", required: false, multiple: true
input "imageCapture", "capability.imageCapture", title: "Image Capture", required: false, multiple: true
input "indicator", "capability.indicator", title: "Indicator", required: false, multiple: true
input "locationMode", "capability.locationMode", title: "Location Mode", required: false, multiple: true
input "lock", "capability.lock", title: "Lock", required: false, multiple: true
input "mediaController", "capability.mediaController", title: "Media Controller", required: false, multiple: true
input "motionSensor", "capability.motionSensor", title: "Motion Sensor", required: false, multiple: true
input "musicPlayer", "capability.musicPlayer", title: "Music Player", required: false, multiple: true
input "powerMeter", "capability.powerMeter", title: "Power Meter", required: false, multiple: true
input "presenceSensor", "capability.presenceSensor", title: "Presence Sensor", required: false, multiple: true
input "relativeHumidityMeasurement", "capability.relativeHumidityMeasurement", title: "Relative Humidity Measurement", required: false, multiple: true
input "relaySwitch", "capability.relaySwitch", title: "Relay Switch", required: false, multiple: true
input "sensor", "capability.sensor", title: "Sensor", required: false, multiple: true
input "signalStrength", "capability.signalStrength", title: "Signal Strength", required: false, multiple: true
input "sleepSensor", "capability.sleepSensor", title: "Sleep Sensor", required: false, multiple: true
input "smokeDetector", "capability.smokeDetector", title: "Smoke Detector", required: false, multiple: true
input "speechRecognition", "capability.speechRecognition", title: "Speech Recognition", required: false, multiple: true
input "stepSensor", "capability.stepSensor", title: "Step Sensor", required: false, multiple: true
input "switchv", "capability.switch", title: "Switch", required: false, multiple: true
input "switchLevel", "capability.switchLevel", title: "Switch Level", required: false, multiple: true
input "temperatureMeasurement", "capability.temperatureMeasurement", title: "Temperature Measurement", required: false, multiple: true
input "thermostat", "capability.thermostat", title: "Thermostat", required: false, multiple: true
input "thermostatCoolingSetpoint", "capability.thermostatCoolingSetpoint", title: "Thermostat Cooling Setpoint", required: false, multiple: true
input "threeAxis", "capability.threeAxis", title: "Three Axis", required: false, multiple: true
input "touchSensor", "capability.touchSensor", title: "TouchSensor", required: false, multiple: true
input "valve", "capability.valve", title: "Valve", required: false, multiple: true
input "waterSensor", "capability.waterSensor", title: "Water Sensor", required: false, multiple: true
}
}
/**
* Installer handler
*
* The key piece here is the subscribeToEvents() function. This triggers the subscription of the events that matter
*/
def installed() {
log.debug "Installed with settings: ${settings}"
subscribeToEvents()
initialize()
}
/**
* Update handler
*
* Basically the same as the installer - but it will unsubscribe everything before you subscribe again
*/
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
subscribeToEvents()
initialize()
}
/**
* Init this dude!!
*/
def initialize() {
subscribe(app, eventHandler)
}
/**
* Subscribe to those events
*
* This is the meat of the app. When an event happens that we have subscribed to, we throw that event to the eventHandler function
* Works pretty well.
*/
def subscribeToEvents() {
subscribe(accelerationSensor, "acceleration ", eventHandler)
subscribe(alarm, "alarm", eventHandler)
subscribe(battery, "battery", eventHandler)
subscribe(beacon, "presence", eventHandler)
subscribe(button, "button", eventHandler)
subscribe(carbonMonoxideDetector, "carbonMonoxide", eventHandler)
subscribe(colorControl, "hue", eventHandler)
subscribe(colorControl, "saturation", eventHandler)
subscribe(contactSensor, "contact", eventHandler)
subscribe(doorControl, "door", eventHandler)
subscribe(energyMeter, "energy", eventHandler)
subscribe(illuminanceMeasurement, "illuminance", eventHandler)
subscribe(imageCapture, "image", eventHandler)
subscribe(indicator, "indicatorStatus", eventHandler)
subscribe(locationMode, "mode", eventHandler)
subscribe(lock, "lock", eventHandler)
subscribe(mediaController, "activities", eventHandler)
subscribe(mediaController, "currentActivity", eventHandler)
subscribe(motionSensor, "motion", eventHandler)
subscribe(musicPlayer, "status", eventHandler)
subscribe(musicPlayer, "level", eventHandler)
subscribe(musicPlayer, "trackDescription", eventHandler)
subscribe(musicPlayer, "trackData", eventHandler)
subscribe(musicPlayer, "mute", eventHandler)
subscribe(powerMeter, "power", eventHandler)
subscribe(presenceSensor, "presence", eventHandler)
subscribe(relativeHumidityMeasurement, "humidity", eventHandler)
subscribe(relaySwitch, "switch", eventHandler)
subscribe(sensor, "sensor", eventHandler)
subscribe(signalStrength, "lqi", eventHandler)
subscribe(signalStrength, "rssi", eventHandler)
subscribe(sleepSensor, "sleeping", eventHandler)
subscribe(smokeDetector, "smoke", eventHandler)
subscribe(speechRecognition, "phraseSpoken", eventHandler)
subscribe(stepSensor, "goals", eventHandler)
subscribe(stepSensor, "steps", eventHandler)
subscribe(switchv, "switch", eventHandler)
subscribe(switchLevel, "level", eventHandler)
subscribe(temperatureMeasurement, "temperature", eventHandler)
subscribe(thermostat, "temperature", eventHandler)
subscribe(thermostat, "heatingSetpoint", eventHandler)
subscribe(thermostat, "coolingSetpoint", eventHandler)
subscribe(thermostat, "thermostatSetpoint", eventHandler)
subscribe(thermostat, "thermostatMode", eventHandler)
subscribe(thermostat, "thermostatFanMode", eventHandler)
subscribe(thermostat, "thermostatOperatingState", eventHandler)
subscribe(thermostatCoolingSetpoint, "coolingSetpoint", eventHandler)
subscribe(threeAxis, "threeAxis", eventHandler)
subscribe(touchSensor, "touch", eventHandler)
subscribe(valve, "contact", eventHandler)
subscribe(waterSensor, "water", eventHandler)
}
/**
* EventHandler!!
*
* This is the function that communicates externally.
* There are 3 parts:
* 1) Create the json object
* 2) Create the request params object (including URL)
* 3) make the request to webhook URL
*
* I am forcing json because it is 2015
*
* This works pretty well
*/
def eventHandler(evt) {
def state_changed = evt.isStateChange()
/**
* Json object
* I tired to include everything
*/
def json_body = [
id: evt.deviceId,
date: evt.isoDate,
value: evt.value,
name: evt.name,
display_name: evt.displayName,
description: evt.descriptionText,
source: evt.source,
state_changed: evt.isStateChange(),
physical: evt.isPhysical(),
location_id: evt.locationId,
hub_id: evt.hubId,
smartapp_id: evt.installedSmartAppId
]
def json_params = [
uri: settings.url,
success: successClosure,
body: json_body
]
try {
httpPostJson(json_params)
} catch (e) {
log.error "http post failed: $e"
}
}
/**
* Called when a successful webhook post happens!
*/
def successClosure = { response ->
log.debug "Request was successful, $response"
}