Last active
June 26, 2019 20:47
-
-
Save joshualyon/b1504609112789dafd58feb230ef95ce to your computer and use it in GitHub Desktop.
This file contains 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
// 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