Skip to content

Instantly share code, notes, and snippets.

@joshualyon
Last active June 26, 2019 20:47
Show Gist options
  • Save joshualyon/b1504609112789dafd58feb230ef95ce to your computer and use it in GitHub Desktop.
Save joshualyon/b1504609112789dafd58feb230ef95ce to your computer and use it in GitHub Desktop.
// Developer: josh@sharptools.io
metadata {
definition (name: "Simulated Fan", namespace: "sharptools/testing", author: "josh") {
capability "Actuator"
capability "Sensor"
capability "Fan Control"
command "cycleSpeed"
}
}
def installed() {
log.trace "Executing 'installed'"
initialize()
setSpeed('off')
}
def updated() {
log.trace "Executing 'updated'"
initialize()
}
private initialize() {
log.trace "Executing 'initialize'"
}
def cycleSpeed(){
def cycleSpeeds = ["off","low","medium-low","medium","high"]
//find the current index
def currentIndex = cycleSpeeds.indexOf(device.currentValue("speed"))
//calculate the next index
def nextIndex = (currentIndex + 1 > cycleSpeeds.size() - 1) ? 0 : currentIndex + 1
//set the speed
log.trace "Executed 'cycleSpeed'. Next speed: ${cycleSpeeds[nextIndex]}"
setSpeed(cycleSpeeds[nextIndex])
}
def setSpeed(speed){
log.trace "Executing 'setSpeed' to ${speed}"
if(isValidSpeed(speed))
sendEvent(name: "speed", value: speed, isStateChange: true)
else
log.warn "Invalid speed: ${speed}"
}
def isValidSpeed(speed){
def validSpeeds = ["low","medium-low","medium","medium-high","high","on","off","auto"]
return validSpeeds.contains(speed)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment