Component device programming

I have a question about the Hubitat example component devices, such as the dimmer component device here: HubitatPublic/genericComponentDimmer.groovy at master · hubitat/HubitatPublic · GitHub

I've noticed that Hubitat includes a "parse" function which sends sendEvent events. As an example, in the above driver, see lines 39-46.

void parse(List<Map> description) {
    description.each {
        if (it.name in ["switch","level"]) {
            if (txtEnable) log.info it.descriptionText
            sendEvent(it)
        }
    }
}

I assume you would call that for a child device using something like the following (where cd is a child device)

cd.parse([name:"level", value:55])

I've always called the sendEvent directly - i.e., like

cd.sendEvent([name:"level", value:55])

Is there a reason to use cd.parse versus cd.sendEvent?
The only one I could think of is calling cd.parse lets you add logging in the parse routine, but otherwise, the effect is the same. Is that correct?

Yes, but I believe the correct syntax is

cd.parse([[name:"level", value:55]])

as Hubitat's Generic Component drivers' parse() routine accept multiple maps of updates at once. This would come in handy if a child device has multiple attributes that you want to update all at once.

Yes, the effect should be the same... However, some child drivers may include additional functionality within their parse() routines, which might be nice to have. I know in some of my custom child drivers, I implement user settings to allow rounding to a specific number of decimal places for floating point attributes. Or, maybe apply a child device specific temperature or humidity offset. Things like that.

Thus, I feel it is a best practice to use the built-in Generic Component drivers in the same manner that Hubitat uses them. This also helps to safeguard your Parent Code from unexpected changes, as I believe Hubitat will try to keep this interface pretty standard.

4 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.