Here are links to my existing device code. It can use some tweaks, any suggestions are welcome.
Main Device:
Child Devices:
SmartApp to set it all up: