Skip to content

Instantly share code, notes, and snippets.

@mager
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mager/55df8d0ce89e44c76ab5 to your computer and use it in GitHub Desktop.
Save mager/55df8d0ce89e44c76ab5 to your computer and use it in GitHub Desktop.
SmartThings Code Example: RESTful Switch SmartApp
/**
* Control a Switch with an API call
*
* Copyright 2014 Andrew Mager
*
* 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: "Control a Switch with an API call",
namespace: "co.mager",
author: "Andrew Mager",
description: "V2 of 'RESTful Switch' example. Trying to make OAuth work properly.",
category: "My Apps",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
oauth: true)
preferences {
section("Allow External Service to Control These Things...") {
input "light1", "capability.switch", title: "Pick switch #1", required: false
}
}
/* This block defines which functions will fire when you hit certain endpoints. */
mappings {
path("/switch") {
action: [
GET: "getSwitch",
PUT: "setSwitch"
]
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
}
def updated() {
log.debug "Updated with settings: ${settings}"
}
def getSwitch() {
light1.currentState("switch")
}
def setSwitch($evt) {
log.debug "The event: " + request.JSON.value
if (request.JSON.value == "on") {
light1.on()
}
else if (request.JSON.value == "off") {
light1.off()
}
else {
log.error "Invalid value: $request.JSON.value"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment