Hi,
I have a device with switch and switchlevel capabilities.
On/Off and Level commands work fine but setLevel does not work.
There are right() and left() commands on the device.
If the level set is higher the current level it should run the right() command, if lower than the current level it should run the left() command.
And according to the logs , it runs these commands. But the server does not receive the commands. So I guess hubaction is not triggered.
What is wrong in my code ?
Btw, if I hit the "right" or "left" buttons on Hubitat UI, these commands run fine and the server receives the command.
metadata {
definition (name: "Coffee Cup Mover", namespace: "ilkeraktuna", author: "ilkeraktuna") {
capability "Switch"
capability "SwitchLevel"
command "on"
command "off"
command "right"
command "left"
}
preferences {
section {
input title: "", description: "Coffee Cup Mover", displayDuringSetup: true, type: "paragraph", element: "paragraph"
input("ip", "string", title:"LAN IP address", description: "LAN IP address", required: true, displayDuringSetup: true)
input "username", "string", title: "Username", required: true
input "password", "password", title: "Password", required: true
}
}
tiles(scale: 2) {
standardTile("switch", "device.switch", width: 6, height: 6, canChangeIcon: true) {
state "off", label: 'Offline', icon: "st.Electronics.electronics18", backgroundColor: "#ff0000"
state "on", label: 'Online', icon: "st.Electronics.electronics18", backgroundColor: "#79b821"
}
main("switch")
details(["switch"])
}
}
def off() {
if(debugEnable)log.debug "turning off"
sendEvent(name: "switch", value: "off");
def userpass
if(password != null && password != "")
userpass = encodeCredentials(username, password)
def headers = getHeader(userpass)
def hubAction = new hubitat.device.HubAction(
method: "GET",
path: "/disabledrv",
headers: headers,
body: "\r\n\r\n"
)
return hubAction
}
def on() {
if(debugEnable)log.debug "turning on"
sendEvent(name: "switch", value: "on");
def userpass
if(password != null && password != "")
userpass = encodeCredentials(username, password)
def headers = getHeader(userpass)
def hubAction = new hubitat.device.HubAction(
method: "GET",
path: "/enabledrv",
headers: headers,
body: "\r\n\r\n"
)
return hubAction
}
def right() {
log.debug "Right x"
def userpass
if(password != null && password != "")
userpass = encodeCredentials(username, password)
def headers = getHeader(userpass)
def hubAction = new hubitat.device.HubAction(
method: "GET",
path: "/right",
headers: headers,
body: "\r\n\r\n"
)
return hubAction
}
def left() {
log.debug "Left x"
def userpass
if(password != null && password != "")
userpass = encodeCredentials(username, password)
def headers = getHeader(userpass)
def hubAction = new hubitat.device.HubAction(
method: "GET",
path: "/left",
headers: headers,
body: "\r\n\r\n"
)
return hubAction
}
def setLevel(level){
def lastLevel = device.currentValue("level")
if(level > lastLevel) {
right()
log.debug "Right"
}
if(level < lastLevel) {
left()
log.debug "Left"
}
log.debug "Level '${lastLevel}'"
sendEvent(name: "level", value: level)
}
private encodeCredentials(username, password){
def userpassascii = "${username}:${password}"
def userpass = "Basic " + userpassascii.bytes.encodeBase64().toString()
return userpass
}
private getHeader(userpass = null){
def headers = [:]
headers.put("Host", ip)
headers.put("Content-Type", "application/x-www-form-urlencoded")
if (userpass != null)
headers.put("Authorization", userpass)
return headers
}
private String convertIPtoHex(ipAddress) {
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
return hex
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04x', port.toInteger() )
return hexport
}
def parse(String description) {
log.debug "Parsing '${description}'"
}
private delayAction(long time) {
new hubitat.device.HubAction("delay $time")
}
def installed() {
initialize()
}
def updated() {
initialize()
}
def poll(){
refresh()
}
def initialize() {
unschedule()
}
def refresh() {
def host = ip
}
def setUp(str1) {
device.updateSetting("ip",[value:"$str1",type:"string"])
}