4 * Copyright 2016 Nicola Russo
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 namespace: "gideon.api",
19 author: "Braindrain Solutions",
20 description: "Gideon AI Smart app allows you to connect and control all of your SmartThings devices through the Gideon AI app, making your SmartThings devices even smarter.",
22 iconUrl: "http://s33.postimg.org/t77u7y7v3/logo.png",
23 iconX2Url: "http://s33.postimg.org/t77u7y7v3/logo.png",
24 iconX3Url: "http://s33.postimg.org/t77u7y7v3/logo.png",
25 oauth: [displayName: "Gideon AI API", displayLink: "gideon.ai"])
29 section("Control these switches...") {
30 input "switches", "capability.switch", multiple:true
32 section("Control these motion sensors...") {
33 input "motions", "capability.motionSensor", multiple:true
35 section("Control these presence sensors...") {
36 input "presence_sensors", "capability.presenceSensor", multiple:true
38 section("Control these outlets...") {
39 input "outlets", "capability.switch", multiple:true
41 section("Control these locks...") {
42 input "locks", "capability.lock", multiple:true
44 section("Control these locks...") {
45 input "temperature_sensors", "capability.temperatureMeasurement"
50 log.debug "Installed with settings: ${settings}"
56 log.debug "Updated with settings: ${settings}"
63 // TODO: subscribe to attributes, devices, locations, etc.
64 subscribe(outlet, "energy", outletHandler)
65 subscribe(outlet, "switch", outletHandler)
68 // TODO: implement event handlers
69 def outletHandler(evt) {
70 log.debug "$outlet.currentEnergy"
75 private device(it, type) {
76 it ? [id: it.id, label: it.label, type: type] : null
81 path("/getalldevices") {
86 path("/doorlocks/:id/:command") {
91 path("/doorlocks/:id") {
93 GET: "getDoorLockStatus"
96 path("/tempsensors/:id") {
98 GET: "getTempSensorsStatus"
101 path("/presences/:id") {
103 GET: "getPresenceStatus"
106 path("/motions/:id") {
108 GET: "getMotionStatus"
111 path("/outlets/:id") {
113 GET: "getOutletStatus"
116 path("/outlets/:id/:command") {
121 path("/switches/:command") {
129 def getAllDevices() {
130 def locks_list = locks.collect{device(it,"Lock")}
131 def presences_list = presence_sensors.collect{device(it,"Presence")}
132 def motions_list = motions.collect{device(it,"Motion")}
133 def outlets_list = outlets.collect{device(it,"Outlet")}
134 def switches_list = switches.collect{device(it,"Switch")}
135 def temp_list = temperature_sensors.collect{device(it,"Temperature")}
136 return [Locks: locks_list, Presences: presences_list, Motions: motions_list, Outlets: outlets_list, Switches: switches_list, Temperatures: temp_list]
140 def getDoorLockStatus() {
141 def device = locks.find { it.id == params.id }
143 httpError(404, "Device not found")
145 return [Device_state: device.currentValue('lock')]
149 def updateDoorLock() {
150 def command = params.command
151 def device = locks.find { it.id == params.id }
154 httpError(404, "Device not found")
156 if(command == "toggle")
158 if(device.currentValue('lock') == "locked")
163 return [Device_id: params.id, result_action: "200"]
170 def getPresenceStatus() {
172 def device = presence_sensors.find { it.id == params.id }
174 httpError(404, "Device not found")
176 return [Device_state: device.currentValue('presence')]
181 def getMotionStatus() {
183 def device = motions.find { it.id == params.id }
185 httpError(404, "Device not found")
187 return [Device_state: device.currentValue('motion')]
192 def getOutletStatus() {
194 def device = outlets.find { it.id == params.id }
196 httpError(404, "Device not found")
198 return [Device_state: device.currentSwitch, Current_watt: device.currentValue("energy")]
204 def command = params.command
205 def device = outlets.find { it.id == params.id }
208 httpError(404, "Device not found")
210 if(command == "toggle")
212 if(device.currentSwitch == "on")
217 return [Device_id: params.id, result_action: "200"]
225 def command = params.command
226 def device = switches.find { it.id == params.id }
229 httpError(404, "Device not found")
231 if(command == "toggle")
233 if(device.currentSwitch == "on")
238 return [Device_id: params.id, result_action: "200"]
245 def getTempSensorsStatus() {
247 def device = temperature_sensors.find { it.id == params.id }
249 httpError(404, "Device not found")
251 return [Device_state: device.currentValue('temperature')]