I wanted to get a map of all my driver id's/drive name pairs so I could do the following without needing an input statement in the preference section:
def DeviceMap = GetDeviceMap()
app.updateSetting("EM", [value:DeviceMap["EM"], type:"capability.*"])
subscribe(EM, "dryerWatts", dryerHandler)
I could not find anything that would work for me in a custom app or a driver so I coded the following routine:
def GetDeviceMap() {
def deviceMap = [:] // create empty device map
for (int i = 1; i < 500; i++) {
def deviceId = i.toString()
app.updateSetting("xyz", [value: deviceId, type: "capability.*"])
def obj = settings.xyz
try {
// only call getDisplayName if object is really a device
def name = obj?.hasProperty('displayName') ? obj.displayName : obj?.toString()
// lets make a device map
deviceMap[name] = deviceId
// log.debug "${i} xyz = ${name}"
} catch (e) {
// log.warn "${i} caused error: ${e.message}"
}
}
log.debug "Device Map: ${deviceMap}"
return deviceMap
}
It works great but I would like to know if I have a more straight forward way of getting this map. The api's that looked like they might work for me seemed to be restricted in both app's and driver's
Thanks