The above code works verbatim for me in a test app I created to try exactly this, though you might want to specify a device name or label so a bunch of devices don't get created displaying as "Virtual Switch." If you can provide a minimal non-working example, that may help someone troubleshoot or help you figure out the problem on your own as you remove other, potentially problematic code.
Also, there's no explicit list of devices, but you can get a list of driver names from the "Type" dropdown on any device page, and the namespace for all built-in drivers is "hubitat", as you are already using.
//
// Install a device paired to a bridge
//
def addGrandchildDevice ()
{
logDebug "addGrandchildDevice: IN"
def deviceData = [:]
deviceData.DeviceType = "Switch"
deviceData.DeviceTypeName = "Virtual Switch"
deviceData.DeviceId = "${device.data.data.DeviceId}-2"
deviceData.Name = "${device.name} latch switch"
deviceData.DebugLoggingRequired = debugLogging // if this device is running with debugLogging, the child device will run with it too
def deviceProperties = [:]
deviceProperties.label = deviceData.Name
deviceProperties.name = deviceData.DeviceTypeName
deviceProperties.data = deviceData
deviceProperties.isComponent = true // the child device will be "attached" to this device
try
{
//logDebug "addGrandchildDevice: trying to install device with nukiId = ${nukiDevice.nukiId}"
def deviceDNI = "${device.dni}-Latch"
def childDevice = addChildDevice ("hubitat", // namespace - must be the same for this app and driver
deviceData.DeviceTypeName, // typeName = driver of the child device - must have been previously loaded into this HE hub
deviceDNI, // deviceNetworkId
deviceProperties)
// if we pass through here, it means that the device was correcly added. Let's flag it!
logDebug "addGrandchildDevice: device with deviceId = '${deviceData.DeviceId}' and deviceDNI = '${deviceDNI}' successfully added"
//nukiDevice.successfullyInstalled = true
}
catch (com.hubitat.app.exception.UnknownDeviceTypeException e)
{
logWarn "${deviceData.DeviceTypeName}: Failed to install device with nukiId = ${deviceData.DeviceId}. Driver (${deviceData.DeviceTypeName}) not installed on this Hubitat Elevation hub; install it before attempting to run this app again."
//nukiDevice.successfullyInstalled = false
}
catch (error)
{
logWarn "${deviceData.DeviceTypeName}: Failed to install device with nukiId = ${deviceData.DeviceId}. Error = ${error}"
//nukiDevice.successfullyInstalled = false
}
logDebug "addGrandchildDevice: OUT"
}
The try block is catching the exception "com.hubitat.app.exception.UnknownDeviceTypeException".
This works for me if done from an app (with minor changes to fill in some blanks that would otherwise be missing from the data). I don't have a good "minimal driver" to test this with, but perhaps something is different there.
addChildDevice ("hubitat", // namespace - must be the same for this app and driver
deviceData.DeviceTypeName, // typeName = driver of the child device - must have been previously loaded into this HE hub
deviceDNI, // deviceNetworkId
null,
deviceProperties)
This is unrelated, but I'm pretty sure dni isn't an attribute of device so you might want to change the line below to use deviceNetworkId...
First @krlaframboise, I'm sorry for taking so long to answer you - kinda busy here.
Not exactly ... this what I did:
def childDevice = addChildDevice ("hubitat", // namespace - must be the same for this app and driver
"Virtual Switch", // typeName = driver of the child device - must have been previously loaded into this HE hub
"anyDniWillDo", // deviceNetworkId
deviceProperties)
I've used simple strings wherever possible, to make it simple. And it worked ! Why it did not work before? Well, this is for the phase 2 ...