Hi all,
I've written a number of simple apps/drivers but I'm embarking on a more complex app & I'm not sure I know the best way to design it.
I'd like to have the user select a set of devices as part of the configuration, and then for each device, specific a set of settings. Over the course of the operation, I'll need to store some state information about each device.
Currently, I'm using a map to store the settings & state of each selected device, but it's rather kludgy. I'm wondering if there's a better way.
For example: to generate the settings for each selected device, I'm doing the following :
section("Devices"){
input name: "arrivingSwitch", type: "capability.switch", title: "Select Switch which will indicate imminent arrival", multiple: false, required: true
input name: "devicesToControl", type: "capability.thermostat", title: "Select HVAC Devices to turn on/off", multiple: true, required: true, submitOnChange: true
input "notifyDevices", "capability.notification", title: "Devices to notify when Device is turned on", required: false, multiple: true, submitOnChange: true
}
section {
if (devicesToControl) {
devicesToControl.each{ h ->
String deviceName = (h.getLabel() == null) ? h.getName().toString() : h.getLabel().toString()
section(hideable: true, hidden: true, "Settings for: ${deviceName}") {
input name:"dev.${deviceName}_dayTemp", type: "number", title: "Daytime Temp for ${deviceName}:", required: true, defaultValue:68
input name:"dev.${deviceName}_nightTemp", type: "number", title: "Nighttime Temp for ${deviceName}:", required: true, defaultValue:60
input name:"dev.${deviceName}_adjustWhenHome", type: "bool", title: "Adjust ${deviceName} temp based on Day/Night settings?", defaultValue:true
input name:"dev.${deviceName}_turnOnWhenHome", type: "bool", title: "Turn ${deviceName} ON if powered off when Home?", defaultValue:false
input name:"dev.${deviceName}_turnOnwhenArriving", type: "bool", title: "Turn ${deviceName} ON When Arriving?", defaultValue:false
}
}
}
}
To use those settings, I store them in a Map.
void updateSettings(){
String deviceName
String settingName
devicesToControl.each { s ->
deviceName = (s.getLabel() == null) ? s.getName() : s.getLabel()
nightTemp = "dev_${deviceName}_nightTemp"
dayTemp = "dev_${deviceName}_dayTemp"
adjustWhenHome = "dev_${deviceName}_adjustWhenHome"
turnOnWhenHome = "dev_${deviceName}_turnOnWhenHome"
turnOnwhenArriving = "dev_${deviceName}_turnOnwhenArriving"
def thisSettings = [name: deviceName,
nightTemp: this."$nightTemp",
dayTemp: this."$dayTemp",
adjustWhenHome: this."$adjustWhenHome",
turnOnWhenHome: this."$turnOnWhenHome",
turnOnwhenArriving: this."$turnOnwhenArriving"
]
state.deviceSettings.put(deviceName, thisSettings)
}
}