Skip to content

Instantly share code, notes, and snippets.

@aurman
Created March 27, 2014 17:33
Show Gist options
  • Save aurman/9813279 to your computer and use it in GitHub Desktop.
Save aurman/9813279 to your computer and use it in GitHub Desktop.
SmartThings API Endpoint Example
/**
* App Endpoint API Access Example
*
* Author: SmartThings
*/
preferences {
section("Allow Endpoint to Control These Things...") {
input "switches", "capability.switch", title: "Which Switches?", multiple: true
input "locks", "capability.lock", title: "Which Locks?", multiple: true
}
}
mappings {
path("/switches") {
action: [
GET: "listSwitches"
]
}
path("/switches/:id") {
action: [
GET: "showSwitch"
]
}
path("/switches/:id/:command") {
action: [
GET: "updateSwitch"
]
}
path("/locks") {
action: [
GET: "listLocks"
]
}
path("/locks/:id") {
action: [
GET: "showLock"
]
}
path("/locks/:id/:command") {
action: [
GET: "updateLock"
]
}
}
def installed() {}
def updated() {}
//switches
def listSwitches() {
switches.collect{device(it,"switch")}
}
def showSwitch() {
show(switches, "switch")
}
void updateSwitch() {
update(switches)
}
//locks
def listLocks() {
locks.collect{device(it,"lock")}
}
def showLock() {
show(locks, "lock")
}
void updateLock() {
update(locks)
}
def deviceHandler(evt) {}
private void update(devices) {
log.debug "update, request: params: ${params}, devices: $devices.id"
//def command = request.JSON?.command
def command = params.command
//let's create a toggle option here
if (command)
{
def device = devices.find { it.id == params.id }
if (!device) {
httpError(404, "Device not found")
} else {
if(command == "toggle")
{
if(device.currentValue('switch') == "on")
device.off();
else
device.on();
}
else
{
device."$command"()
}
}
}
}
private show(devices, type) {
def device = devices.find { it.id == params.id }
if (!device) {
httpError(404, "Device not found")
}
else {
def attributeName = type == "motionSensor" ? "motion" : type
def s = device.currentState(attributeName)
[id: device.id, label: device.displayName, value: s?.value, unitTime: s?.date?.time, type: type]
}
}
private device(it, type) {
it ? [id: it.id, label: it.label, type: type] : null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment