What I did this Weekend [RELEASE]

It was a cold and rainy weekend in VT, so it was a good time to look at optimizing some of my automations app code.

I realized that I implement a form of radio button switch groups for scene or other switches in several of my apps. I decided to decouple this feature from the app's methods and make a radio group switch device that can just be used with the apps or other automations.

So I created a Radio Group Switches virtual device driver. It creates and manages the virtual switch group, so that when one switch is turned on, all other switches in the group turn off. The parent device creates and manages the child component virtual switch devices.

I added a preference to the parent to treat the switches as buttons. With that enabled, the component virtual switch turns off after being turned on, based on the delay set in the component device preferences (default is 500ms). This gives visual feedback on a dashboard when a switch, acting as a button, is pressed, when it turns on for 500ms. The active attribute in the parent still sends the event for what switch was "pushed".

Here is an example device created for my Front Scene Switches, with ten child virtual switches.


Just add both files to drivers code, then create the parent device with the Radio Group Switches Driver. Use the command in the parent to create the child devices. (note: state values only update on the driver page when the device page is reloaded, so the state map will not show new devices until parent device is reloaded).

Radio Group Switches.groovy
Generic Component Virtual Switch.groovy

Edit: Note, the parent device itself is also a switch. When it is on, it controls the radio group, when it is turned off, it will not do group updates. So it should be left on, unless for some reason you want to disable the radio group function so the switches work independently, in which case turn the parent device off.

6 Likes

Rained here a little while back. I made myself some oatmeal, read a little, and asked my son to fix my bike. Just saying that all of us aren't Einstein. :wink:

This stuff looks cool. :smiley:

1 Like

Yeah, sometimes I regret starting something like this on a weekend. It seemed like a good idea, but it took up all of Sunday to get it working right, when I could have just been lounging. :tired_face:

3 Likes

Please add to HPM!