2 * Control a Switch with an API call
4 * Copyright 2015 SmartThings
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.
17 name: "Control a Switch with an API call",
18 namespace: "smartthings",
19 author: "SmartThings",
20 description: "V2 of 'RESTful Switch' example. Trying to make OAuth work properly.",
22 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
23 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
27 section("which switches?") {
28 input "theSwitches", "capability.switch", multiple: true
34 // GET requests to /switches endpoint go to listSwitches.
35 // PUT requests go to updateSwitches
42 // GET requests to endpoint /switches/<id> go to getSwitch
43 // PUT requests to endpoint /switches/<id> go to updateSwitch
44 path("/switches/:id") {
52 // return a map in the form of [switchName, switchStatus]
53 // the returned value will be converted to JSON by the platform
56 theSwitches.each {theSwitch ->
57 log.trace "will populate status map"
58 log.trace "theSwitch id: ${theSwitch.id}"
59 status.put(theSwitch.displayName, theSwitch.currentSwitch)
62 log.debug "listSwitches returning: $status"
67 def theSwitch = theSwitches.find{it.id == params.id}
68 [theSwitch.displayName, theSwitch.currentSwitch]
71 // execute the command specified in the request
72 // returns a 400 error if a non-supported command
73 // is specified (only on, off, or toggle supported)
74 // assumes request body with JSON in format {"command" : "<value>"}
75 def updateSwitches() {
76 log.trace "updateSwitches: request: $request"
77 log.trace "updateSwitches: params: $params"
80 doCommand(it, request.JSON.command)
84 // execute the command specified in the request
85 // return a 400 error if a non-supported command
86 // is specified (only on, off, or toggle supported)
87 // assumes request body with JSON in format {"command" : "<value>"}
89 log.trace "updateSwitch: look for swithc with id ${params.id}"
90 def theSwitch = theSwitches.find{it.id == params.id}
91 doCommand(theSwitch, request.JSON.command)
94 def doCommand(theSwitch, command) {
95 if (command == "toggle") {
96 if (theSwitch.currentSwitch == "on") {
97 log.debug "will try and turn switch ${theSwitch.displayName} on"
100 log.debug "will try and turn switch ${theSwitch.displayName} off"
103 } else if (command == "on" || command == "off") {
104 theSwitch."$command"()
106 httpError(400, "Unsupported command - only 'toggle', 'off', and 'on' supported")
110 // called when SmartApp is installed
112 log.debug "Installed with settings: ${settings}"
115 // called when any preferences are changed in this SmartApp.
117 log.debug "Updated with settings: ${settings}"