I have what, I think should be simple code that works with child devices and uses the parse in a component driver, but also needs to use hasAttribute and other functions for the child. I am running into an inconsistency that I can't figure out. Maybe there's a solution.
Here's what I'm doing.
I need to work with a bunch of child devices as well as the root device, so I create a list of them
List<com.hubitat.app.DeviceWrapper> targetDevices = getChildDevices()
I then have a block of code in which I want to check each child device for a property and call its parse, like:
targetDevices.each{
if (it.hasAttribute("something")) { do something}
it.parse( [[event:"switch", value:"on"]]) // sample event
}
My actual code is much more complex, but this gives a basic idea.
Now that all works fine. But sometimes I also want perform the same set of test and generate a similar parse in the parent device. To do that, I have a void parse(List<Map> events) method defined in the parent device, so it can perform a "parse" using the same method call that is available in a child component device.
It seems the next step is obvious - I should be able to add the "parent" onto my list of target devices. I've attempted that in two ways:
Method #1
List<com.hubitat.app.DeviceWrapper> targetDevices = getChildDevices()
targetDevices += device
targetDevices.each{
if (it.hasAttribute("something")) { do something}
it.parse( [[event:"switch", value:"on"]]) // sample event
}
and
Method #2
List<com.hubitat.app.DeviceWrapper> targetDevices = getChildDevices()
targetDevices += this
targetDevices.each{
if (it.hasAttribute("something")) { do something}
it.parse( [[event:"switch", value:"on"]]) // sample event
}
If I add the parent to the targetDevices list , using Method #1, (by adding 'device') then the it.hasAttribute works for the parent device, but the call to it.parse fails for the parent (but both work for the child devices).
If I include the parent using Method #2, (by adding 'this') then the it.hasAttribute fails for the parent device, but the call to it.parse works for the parent (and both work for the child devices).
Logically, it seems to me that a device on the targetDevice list should behave similarly - i.e., calling an it.hasAttribute or one of the device methods through it.methodName() should be logically the same. Is there a way to accomplish what I've described?
An obvious solution would be to handle the parent in code specific to the parent device, but it seems it should be possible to have a list of devices that include the parent and be able to operate on it consistent with what you can do for child devices.