Hubitat icon indicating copy to clipboard operation
Hubitat copied to clipboard

Removing nodes from association group will add them if none exist

Open mbbush opened this issue 3 years ago • 1 comments

I was reading through the code to try to learn more about how association groups work, and I found what appears to be a bug in the 4-in-1 sensor driver. I didn't look to see if the same bug is present in other drivers as well.

https://github.com/InovelliUSA/Hubitat/blob/1b6a5504d89d749ab1dde594362cbd59018b1c77/Drivers/inovelli-4-in-1-sensor.src/inovelli-4-in-1-sensor.groovy#L704

If state."desiredAssociation${group}" is null, the condition at the beginning of the function which seems designed to avoid a null pointer exception is assuming that the method was called with an attempt to add nodes, not remove them. That's probably true, but not necessarily; you can never fully predict what the user will try to do. As a consumer of the code, I find this behavior counter to what I'd expect. I'd expect an attempt to remove nodes from a null list to either be a no-op or an error.

I think it would be really easy to fix this to be a no-op, using the groovy "elvis" operator ?:. Just replace the entire method body with this:

        switch (action) {
            case 0:
                state."desiredAssociation${group}" = state."desiredAssociation${group}" ?: [] - nodes
            break
            case 1:
                state."desiredAssociation${group}" = state."desiredAssociation${group}" ?: [] + nodes
            break
        }

Note: I haven't tested this.

mbbush avatar Aug 16 '21 18:08 mbbush

Seems it is a bug, and your solution should work. But as far as bugs go, it is a rather minor one that only results in the remove operation being a toggle. I think your fix is sound.

apwelsh avatar Aug 16 '21 20:08 apwelsh