Update notify-me-with-hue.groovy
[smartapps.git] / official / gidjit-hub.groovy
1 /**
2  *  Gidjit Hub
3  *
4  *  Copyright 2016 Matthew Page
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 definition(
17     name: "Gidjit Hub",
18     namespace: "com.gidjit.smartthings.hub",
19     author: "Matthew Page",
20     description: "Act as an endpoint so user's of Gidjit can quickly access and control their devices and execute routines. Users can do this quickly as Gidjit filters these actions  based on their environment",
21     category: "Convenience",
22     iconUrl: "http://www.gidjit.com/appicon.png",
23     iconX2Url: "http://www.gidjit.com/appicon@2x.png",
24     iconX3Url: "http://www.gidjit.com/appicon@3x.png",
25     oauth: [displayName: "Gidjit", displayLink: "www.gidjit.com"])
26
27 preferences {
28     // deviceAuthorization page is simply the devices to authorize
29     page(name: "deviceAuthorization", title: "Device Authorization", nextPage: "instructionPage",
30          install: false, uninstall: true) {
31                 section ("Allow Gidjit to have access, thereby allowing you to quickly control and monitor your following devices. Privacy Policy can be found at http://priv.gidjit.com/privacy.html") {
32                 input "switches", "capability.switch", title: "Control/Monitor your switches", multiple: true, required: false
33                 input "thermostats", "capability.thermostat", title: "Control/Monitor your thermostats", multiple: true, required: false
34                 input "windowShades", "capability.windowShade", title: "Control/Monitor your window shades", multiple: true, required: false //windowShade
35         }
36
37     }
38     page(name: "instructionPage", title: "Device Discovery", install: true) {
39         section() {
40             paragraph "Now the process is complete return to the Devices section of the Detected Screen. From there and you can add actions to each of your device panels, including launching SmartThings routines."
41         }
42     }
43 }
44
45 mappings {
46   path("/structureinfo") {
47     action: [
48       GET: "structureInfo"
49     ]
50   }
51   path("/helloactions") {
52     action: [
53       GET: "helloActions"
54     ]
55   }
56   path("/helloactions/:label") {
57     action: [
58       PUT: "executeAction"
59     ]
60   }
61
62   path("/switch/:id/:command") {
63     action: [
64       PUT: "updateSwitch"
65     ]
66   }
67
68   path("/thermostat/:id/:command") {
69     action: [
70       PUT: "updateThermostat"
71     ]
72   }
73
74   path("/windowshade/:id/:command") {
75     action: [
76       PUT: "updateWindowShade"
77     ]
78   }
79   path("/acquiredata/:id") {
80     action: [
81       GET: "acquiredata"
82     ]
83   }
84 }
85
86
87 def installed() {
88         log.debug "Installed with settings: ${settings}"
89
90         initialize()
91 }
92
93 def updated() {
94         log.debug "Updated with settings: ${settings}"
95
96         unsubscribe()
97         initialize()
98 }
99
100 def initialize() {
101         // subscribe to attributes, devices, locations, etc.
102 }
103 def helloActions() {
104         def actions = location.helloHome?.getPhrases()*.label
105     if(!actions) {
106         return []
107     }
108         return actions
109 }
110 def executeAction() {
111     def actions = location.helloHome?.getPhrases()*.label
112     def a = actions?.find() { it == params.label }
113     if (!a) {
114         httpError(400, "invalid label $params.label")
115         return
116     }
117     location.helloHome?.execute(params.label)
118 }
119 /*  this is the primary function called to query at the structure and its devices */
120 def structureInfo() { //list all devices
121         def list = [:]
122     def currId = location.id
123     list[currId] = [:]
124     list[currId].name = location.name
125     list[currId].id = location.id
126     list[currId].temperatureScale = location.temperatureScale
127     list[currId].devices = [:]
128   
129     def setValues = {
130                 if (params.brief) {
131             return [id: it.id, name: it.displayName]
132         }
133         def newList = [id: it.id, name: it.displayName, suppCapab: it.capabilities.collect {
134             "$it.name"
135         }, suppAttributes: it.supportedAttributes.collect {
136             "$it.name"      
137         }, suppCommands: it.supportedCommands.collect {
138             "$it.name"      
139         }]
140
141         return newList
142     }
143     switches?.each {
144       list[currId].devices[it.id] = setValues(it)
145     }
146     thermostats?.each {
147       list[currId].devices[it.id] = setValues(it)
148     }
149     windowShades?.each {
150       list[currId].devices[it.id] = setValues(it)
151     }
152
153     return list
154
155 }
156 /*  This function returns all of the current values of the specified Devices attributes */
157 def acquiredata() {
158         def resp = [:]
159     if (!params.id) {
160         httpError(400, "invalid id $params.id")
161         return
162     }
163     def dev = switches.find() { it.id == params.id } ?: windowShades.find() { it.id == params.id } ?:
164         thermostats.find() { it.id == params.id }    
165    
166     if (!dev) {
167         httpError(400, "invalid id $params.id")
168         return    
169     }
170     def att = dev.supportedAttributes
171     att.each {
172         resp[it.name] = dev.currentValue("$it.name")
173     }
174     return resp
175 }
176
177 void updateSwitch() {
178     // use the built-in request object to get the command parameter
179     def command = params.command
180         def sw = switches.find() { it.id == params.id }
181     if (!sw) {
182         httpError(400, "invalid id $params.id")
183         return
184     }
185     switch(command) {
186         case "on":
187                 if ( sw.currentSwitch != "on" ) {
188                 sw.on()
189             }
190             break
191         case "off":
192                 if ( sw.currentSwitch != "off" ) {
193                 sw.off()
194             }
195             break
196         default:
197             httpError(400, "$command is not a valid")
198     }
199 }
200
201
202 void updateThermostat() {
203     // use the built-in request object to get the command parameter
204     def command = params.command
205         def therm = thermostats.find() { it.id == params.id }
206     if (!therm || !command) {
207         httpError(400, "invalid id $params.id")
208         return
209     }
210     def passComm = [
211         "off",
212         "heat",
213         "emergencyHeat",
214         "cool",
215         "fanOn",
216         "fanAuto",
217         "fanCirculate",
218         "auto"
219
220         ]
221     def passNumParamComm = [
222         "setHeatingSetpoint",
223         "setCoolingSetpoint",   
224     ]
225     def passStringParamComm = [
226         "setThermostatMode",
227         "setThermostatFanMode",
228         ]
229     if (command in passComm) {
230         therm."$command"()      
231     } else if (command in passNumParamComm && params.p1 && params.p1.isFloat()) {
232         therm."$command"(Float.parseFloat(params.p1))   
233     } else if (command in passStringParamComm && params.p1) {
234         therm."$command"(params.p1)     
235     } else {
236         httpError(400, "$command is not a valid command")
237     }
238 }
239
240 void updateWindowShade() {
241     // use the built-in request object to get the command parameter
242     def command = params.command
243         def ws = windowShades.find() { it.id == params.id }
244     if (!ws || !command) {
245         httpError(400, "invalid id $params.id")
246         return
247     }
248     def passComm = [
249                 "open",
250         "close",
251         "presetPosition",
252         ]
253     if (command in passComm) {
254         ws."$command"()         
255     } else {
256         httpError(400, "$command is not a valid command")
257     }
258 }
259 // TODO: implement event handlers