4 * Copyright 2015 WeatherBug
8 name: "WeatherBug Home",
9 namespace: "WeatherBug",
10 author: "WeatherBug Home",
11 description: "WeatherBug Home",
13 iconUrl: "http://stg.static.myenergy.enqa.co/apps/wbhc/v2/images/weatherbughomemedium.png",
14 iconX2Url: "http://stg.static.myenergy.enqa.co/apps/wbhc/v2/images/weatherbughomemedium.png",
15 iconX3Url: "http://stg.static.myenergy.enqa.co/apps/wbhc/v2/images/weatherbughome.png",
16 oauth: [displayName: "WeatherBug Home", displayLink: "http://weatherbughome.com/"])
20 section("Select thermostats") {
21 input "thermostatDevice", "capability.thermostat", multiple: true
26 path("/appInfo") { action: [ GET: "getAppInfo" ] }
27 path("/getLocation") { action: [ GET: "getLoc" ] }
28 path("/currentReport/:id") { action: [ GET: "getCurrentReport" ] }
29 path("/setTemp/:temp/:id") { action: [ POST: "setTemperature", GET: "setTemperature" ] }
33 * This API call will be leveraged by a WeatherBug Home Service to retrieve
34 * data from the installed SmartApp, including the location data, and
35 * a list of the devices that were authorized to be accessed. The WeatherBug
36 * Home Service will leverage this data to represent the connected devices as well as their
37 * location and associated the data with a WeatherBug user account.
38 * Privacy Policy: http://weatherbughome.com/privacy/
39 * @return Location, including id, latitude, longitude, zip code, and name, and the list of devices
42 def devices = thermostatDevice
43 def lat = location.latitude
44 def lon = location.longitude
45 if(!(devices instanceof Collection))
50 Id: UUID.randomUUID().toString(),
53 Result: [ "Devices": devices,
58 "ZipCode":location.zipCode,
66 * This API call will be leveraged by a WeatherBug Home Service to retrieve
67 * location data from the installed SmartApp. The WeatherBug
68 * Home Service will leverage this data to associate the location to a WeatherBug Home account
69 * Privacy Policy: http://weatherbughome.com/privacy/
71 * @return Location, including id, latitude, longitude, zip code, and name
75 Id: UUID.randomUUID().toString(),
80 "Latitude":location.latitude,
81 "Longitude":location.longitude,
82 "ZipCode":location.zipCode,
88 * This API call will be leveraged by a WeatherBug Home Service to retrieve
89 * thermostat data and store it for display to a WeatherBug user.
90 * Privacy Policy: http://weatherbughome.com/privacy/
92 * @param id The id of the device to get data for
93 * @return Thermostat data including temperature, set points, running modes, and operating states
95 def getCurrentReport() {
96 log.debug "device id parameter=" + params.id
97 def unixTime = (int)((new Date().getTime() / 1000))
98 def device = thermostatDevice.find{ it.id == params.id}
103 Id: UUID.randomUUID().toString(),
105 ErrorMessage: "Device not found. id=" + params.id,
110 Id: UUID.randomUUID().toString(),
115 LocationId: location.id,
118 [Key: "Temperature", Value: GetOrDefault(device, "temperature")],
119 [Key: "ThermostatSetpoint", Value: GetOrDefault(device, "thermostatSetpoint")],
120 [Key: "CoolingSetpoint", Value: GetOrDefault(device, "coolingSetpoint")],
121 [Key: "HeatingSetpoint", Value: GetOrDefault(device, "heatingSetpoint")],
122 [Key: "ThermostatMode", Value: GetOrDefault(device, "thermostatMode")],
123 [Key: "ThermostatFanMode", Value: GetOrDefault(device, "thermostatFanMode")],
124 [Key: "ThermostatOperatingState", Value: GetOrDefault(device, "thermostatOperatingState")]
132 * This API call will be leveraged by a WeatherBug Home Service to set
133 * the thermostat setpoint.
134 * Privacy Policy: http://weatherbughome.com/privacy/
136 * @param id The id of the device to set
137 * @return Indication of whether the operation succeeded or failed
139 def setTemperature() {
140 log.debug "device id parameter=" + params.id
141 def device = thermostatDevice.find{ it.id == params.id}
144 def mode = device.latestState('thermostatMode').stringValue
145 def value = params.temp as Integer
146 log.trace "Suggested temperature: $value, $mode"
148 device.setCoolingSetpoint(value)
149 else if ( mode == "heat")
150 device.setHeatingSetpoint(value)
152 Id: UUID.randomUUID().toString(),
159 Id: UUID.randomUUID().toString(),
161 ErrorMessage: "Device not found. id=" + params.id,
169 log.debug "Installed with settings: ${settings}"
174 * The updated event will be pushed to a WeatherBug Home Service to notify the system to take appropriate action.
175 * Data that will be sent includes the list of devices, and location data
176 * Privacy Policy: http://weatherbughome.com/privacy/
179 log.debug "Updated with settings: ${settings}"
180 log.debug "Updated with state: ${state}"
181 log.debug "Updated with location ${location} ${location.id} ${location.name}"
185 uri: 'https://smartthingsrec.api.earthnetworks.com/api/v1/receive/smartapp/update',
190 "Latitude":location.latitude,
191 "Longitude":location.longitude,
192 "ZipCode":location.zipCode,
197 sendToWeatherBug(postParams)
201 * Subscribe to changes on the thermostat attributes
204 log.trace "initialize enter"
205 subscribe(thermostatDevice, "heatingSetpoint", pushLatest)
206 subscribe(thermostatDevice, "coolingSetpoint", pushLatest)
207 subscribe(thermostatDevice, "thermostatSetpoint", pushLatest)
208 subscribe(thermostatDevice, "thermostatMode", pushLatest)
209 subscribe(thermostatDevice, "thermostatFanMode", pushLatest)
210 subscribe(thermostatDevice, "thermostatOperatingState", pushLatest)
211 subscribe(thermostatDevice, "temperature", pushLatest)
215 * The uninstall event will be pushed to a WeatherBug Home Service to notify the system to take appropriate action.
216 * Data that will be sent includes the list of devices, and location data
217 * Privacy Policy: http://weatherbughome.com/privacy/
220 log.trace "uninstall entered"
222 uri: 'https://smartthingsrec.api.earthnetworks.com/api/v1/receive/smartapp/delete',
227 "Latitude":location.latitude,
228 "Longitude":location.longitude,
229 "ZipCode":location.zipCode,
234 sendToWeatherBug(postParams)
238 * This method will push the latest thermostat data to the WeatherBug Home Service so it can store
239 * and display the data to the WeatherBug user. Data pushed includes the thermostat data as well
241 * Privacy Policy: http://weatherbughome.com/privacy/
243 def pushLatest(evt) {
244 def unixTime = (int)((new Date().getTime() / 1000))
245 def device = thermostatDevice.find{ it.id == evt.deviceId}
247 uri: 'https://smartthingsrec.api.earthnetworks.com/api/v1/receive',
249 DeviceId: evt.deviceId,
250 LocationId: location.id,
253 [Key: "Temperature", Value: GetOrDefault(device, "temperature")],
254 [Key: "ThermostatSetpoint", Value: GetOrDefault(device, "thermostatSetpoint")],
255 [Key: "CoolingSetpoint", Value: GetOrDefault(device, "coolingSetpoint")],
256 [Key: "HeatingSetpoint", Value: GetOrDefault(device, "heatingSetpoint")],
257 [Key: "ThermostatMode", Value: GetOrDefault(device, "thermostatMode")],
258 [Key: "ThermostatFanMode", Value: GetOrDefault(device, "thermostatFanMode")],
259 [Key: "ThermostatOperatingState", Value: GetOrDefault(device, "thermostatOperatingState")]
265 sendToWeatherBug(postParams)
269 * This method attempts to get the value of a device attribute, but if an error occurs null is returned
270 * @return The device attribute value, or null
272 def GetOrDefault(device, attrib)
276 val = device.latestValue(attrib)
280 log.debug "Failed to get attribute " + attrib + " from device " + device
287 * Convenience method that sends data to WeatherBug, logging any exceptions that may occur
288 * Privacy Policy: http://weatherbughome.com/privacy/
290 def sendToWeatherBug(postParams)
294 httpPostJson(postParams) { resp ->
296 log.debug "${it.name} : ${it.value}"
298 log.debug "response contentType: ${resp.contentType}"
299 log.debug "response data: ${resp.data}"
301 log.debug "Communication with WeatherBug succeeded";
305 log.debug "Communication with WeatherBug failed.\n${ex}";