Skip to content

Instantly share code, notes, and snippets.

@sidoh
Last active April 13, 2017 05:31
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 sidoh/da36d2c1099910ef38a6021dc39a61a4 to your computer and use it in GitHub Desktop.
Save sidoh/da36d2c1099910ef38a6021dc39a61a4 to your computer and use it in GitHub Desktop.
/**
* Control a Switch with an API call
*
* 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: "HaGateway API",
namespace: "sidoh",
author: "sidoh",
description: "Expose devices to HaGateway",
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("Select Devices to Authorize") {
input "theSwitches", "capability.switch", title: "Switches:", required: false, multiple: true
input "theThermometers", "capability.temperatureMeasurement", title: "Thermometers", required: false, multiple: true
}
}
mappings {
path("/devices") {
action: [
GET: "getDevices"
]
}
path("/switches") {
action: [
GET: "getSwitches",
PUT: "updateSwitches"
]
}
path("/thermometers") {
action: [
GET: "getThermometers"
]
}
path("/switches/:id") {
action: [
GET: "getSwitch",
PUT: "updateSwitch"
]
}
path("/thermometers/:id") {
action: [
PUT: "updateThermometer"
]
}
path("/routines/:name") {
action: [
GET: "runRoutine"
]
}
path("/routines") {
action: [
GET: "getRoutines"
]
}
}
def runRoutine() {
def allRoutines = location.helloHome?.getPhrases()*.label
def routine = allRoutines.find {
it.toLowerCase().replaceAll(" ", "_").replaceAll(/[^a-zA-Z0-9_]/, "") == params.name
}
log.trace "Running: ${routine}"
location.helloHome?.execute(routine)
return true
}
def getRoutines() {
location.helloHome?.getPhrases()*.label
}
def getDevices() {
[
switches: getSwitches(),
thermometers: getThermometers()
]
}
def getThermometers() {
def status = [:]
theThermometers.each {x ->
status.put(x.id, [name:x.displayName])
}
return status
}
def getSwitches() {
def status = [:]
theSwitches.each {theSwitch ->
status.put(theSwitch.id, [name:theSwitch.displayName,status:theSwitch.currentSwitch])
}
return status
}
def getSwitch() {
def theSwitch = theSwitches.find{it.id == params.id}
[theSwitch.displayName, theSwitch.currentSwitch]
}
def updateSwitches() {
theSwitches.each {
doCommand(it, params.command)
}
}
def updateSwitch() {
def theSwitch = theSwitches.find{it.id == params.id}
doCommand(theSwitch, params.command)
}
def updateThermometer() {
def theThermometer = theThermometers.find{it.id == params.id}
theThermometer.updateTemperature(params.temperature)
}
def doCommand(theSwitch, command) {
if (command == "toggle") {
if (theSwitch.currentSwitch == "on") {
log.debug "will try and turn switch ${theSwitch.displayName} on"
theSwitch.off()
} else {
log.debug "will try and turn switch ${theSwitch.displayName} off"
theSwitch.on()
}
} else if (command == "on" || command == "off") {
theSwitch."$command"()
} else {
httpError(400, "Unsupported command - only 'toggle', 'off', and 'on' supported")
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment