I'm wish to make a custom Virtual Device Driver to work with my DEW point calculation App
In the below example by BRavenel I can't figure out the purpose of the parse(description)
nor how the "def setOpenVentArea(area)" gets its data.
I suspect it's a "shortcut" where the parse output is implicitly available/sent to the "setOpenVentArea(area)" Method but cannot find and info to support this or help me understand its workings.
Any info would be appreciated.
John
metadata {
definition (name: "Virtual Open Vent Area", namespace: "hubitat", author: "Bruce Ravenel") {
capability "Sensor"
command "setOpenVentArea", ["NUMBER"]
attribute "openVentArea", "Number"
}
preferences { // These become entries in the "device" page to ask us for our preferences!
input name: "txtEnable", type: "bool", title: "Enable descriptionText logging", defaultValue: true
}
}
def installed() {
log.warn "installed..."
setOpenVentArea(0)
}
def updated() {
log.info "updated..."
log.warn "description logging is: ${txtEnable == true}"
}
def parse(String description) {
}
def setOpenVentArea(area) {
def descriptionText = "${device.displayName} was set to $area"
if (txtEnable) log.info "${descriptionText}"
sendEvent(name: "openVentArea", value: area, unit: "sq. m", descriptionText: descriptionText)
}
Any message from a device is sent to Parse, where it's 'decoded' or... um parsed The result is sent to the constituent methods.
In this case the parse is 'leave-the-message-alone' and just let it hit the method unaltered.
Indeed, virtuals don't get messages.
"When Events from Z-devices are passed into your Driver’s parse method, they are in an encoded string format. The first thing your parse method should do is call zigbee/zwave.parse on the description string to convert it to a Z-device command object."
It's difficult to fathom from a Virtual, I think, because it's nothing to nothing...
Instead, consider a ZWave Driver... if i whittle it down to the minimum.. parse gets the 'raw' message. It might check for invalid, etc. but for legitimate messages, it sends it off to zwave.parse, as-is...
/*
parse
Respond to received zwave commands.
*/
def parse(String description) {
def cmd = zwave.parse(description, [0x31: 5, 0x30: 1, 0x70: 1, 0x72: 1, 0x84: 1])
if (cmd) { result = zwaveEvent(cmd) }
return result
}
Which gets the 'result' back and then allows the Platform to call the 'handler'... of which are many overloaded:
Drivers are basic tools to interpret.. and parse is the first step in that process.. and in this case, each zwaveEvent() is the last step... which itself may lead to more ZWave commands being sent.
Again, I'm not sure how helpful this massive simplification works for you, but this code is taken from the Aeotec MultiSensor 6 Driver.. if you want to see it without the simplification.
Generally speaking for a virtual device driver there is no need for a parse() method. However, there is one important exception: It is possible to route LAN messages to a virtual device's parse method. This is done for example with Hub Link app, and you can see an example of how that's done here:
So, as a general rule, one puts a parse() method in a virtual device driver, and does with it whatever would make sense were it called with some value in the message to it.
This is the basic way you create "custom" drivers, with custom attributes and custom commands. sendEvent is the means inside the command method to set the attribute, as this driver illustrates.