-
-
Save SunboX/38809e68ff1df8e85024c900ab08f655 to your computer and use it in GitHub Desktop.
SmartThings API Endpoint Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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