I’ve been following the guide for a web services SmartApp here: http://smartthings.readthedocs.org/en/latest/smartapp-web-services-developers-guide/tutorial-part1.html
It works great - if I wanted to control ALL Switches. Which I don’t. Instead, I’m looking to have the SmartApp have access to 8 switches, and through the POST URL, only manipulate the setting of an individual switch.
Here’s the code I’ve come up with, but it’s giving me an error that the method isn’t supported.
Admittedly it’s a clone of the tutorial, which is a great starting point. Just trying to do a foreach loop of the switches array from the defined switches. if a match is made from the :zoneName URL variable, then do the command (on or off).
Majority of my changes can be found in the updateSwitches function.
/**
* Web Services Tutorial
*
* Copyright 2015 SmartThings
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
definition(
name: "Web Services Tutorial",
namespace: "smartthings",
author: "SmartThings",
description: "web services tutorial",
category: "",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
oauth: [displayName: "web services tutorial ", displayLink: "http://localhost:4567"])
preferences {
section ("Allow external service to control these things...") {
input "switches", "capability.switch", multiple: true, required: true
}
}
mappings {
path("/switches") {
action: [
GET: "listSwitches"
]
}
path("/switches/:zoneName/:command") {
action: [
PUT: "updateSwitches"
]
}
}
// returns a list like
// [[name: "kitchen lamp", value: "off"], [name: "bathroom", value: "on"]]
def listSwitches() {
def status
def contentType
def data
def headers = [:]
def resp = []
switches.each {
resp << [displayNameLabel: it.displayName, name: it.name, value: it.currentValue("switch")]
}
return resp
}
void updateSwitches() {
// use the built-in request object to get the command parameter
def status
def contentType
def data
def headers = [:]
def command = params.command
def zoneName = params.zoneName
if (zoneName) {
// check that the switch supports the specified command
// If not, return an error using httpError, providing a HTTP status code.
switches.each {
if (!it.hasCommand(command)) {
httpError(501, "$command is not a valid command for all switches specified")
}
}
// all switches have the comand
// execute the command on all switches
// (note we can do this on the array - the command will be invoked on every element
//switches."$command"()
switches.each {
if ($zoneName == it.displayName) {
it."$command"()
}
}
}
}
def installed() {}
def updated() {}