diff --git a/smartapps/curb-v2/curb-control.src/curb-control.groovy b/smartapps/curb-v2/curb-control.src/curb-control.groovy new file mode 100644 index 0000000..f098bc9 --- /dev/null +++ b/smartapps/curb-v2/curb-control.src/curb-control.groovy @@ -0,0 +1,103 @@ +/** + * Curb Control + * + * Copyright 2016 Savanni D'Gerinel + * + */ +definition( + name: "Curb Control", + namespace: "curb-v2", + author: "Savanni D'Gerinel", + description: "Control point for Curb/SmartThings integration", + category: "Green Living", + 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: true) + + +preferences { + section("Allow Endpoint to Control These Things...") { + input "switches", "capability.switch", title: "Which Switches?", multiple: true, required: false + } +} + +mappings { + + path("/switches") { + action: [ + GET: "listSwitches" + ] + } + path("/switches/:id") { + action: [ + GET: "showSwitch" + ] + } + path("/switches/:id/:command") { + action: [ + PUT: "updateSwitch" + ] + } +} + +def installed() {} + +def updated() {} + + +//switches +def listSwitches() { + switches.collect{device(it,"switch")} +} + +def showSwitch() { + show(switches, "switch") +} +void updateSwitch() { + update(switches) +} + + +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();; + } + } + } +} + +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 +}