How to subscribe to child device events?


(Stephan H.) #1

I’m not a programmer by any means but I’ve been building a smartApp and got quite a bit done but I’m stuck. See a snippet of my code below. During initialization it creates the childDevice with my custom device handler but does not connect it to the smartapp. Seems like the subscribe code needs something else to make the connection.

VMPlayer is a text string input by the user in the preferences and is used to set the childdevice’s label and name.

def initialize() {

if (vMPlayer) {
   	def childDevice = getAllChildDevices()?.find {
    it.device.deviceNetworkId == "VMP_${app.id}"
   	}
    
    if (!childDevice) {          
     	childDevice = addChildDevice("stephack", "Virtual Music Player", "VMP_${app.id}", null, [name: vMPlayer, label: vMPlayer, completedSetup: true])
        childDevice.refresh()
        log.info "creating vmp ${vMPlayer}"            
        }
    else {
        childDevice.label = vMPlayer
        childDevice.name = vMPlayer
        log.info "VMP renamed to ${vMPlayer}"
	}
}

  
subscribe(childDevice, "level", setSonosVolume)
subscribe(childDevice, "status", setSonosStatus)

If I add the following
newName = getAllChildDevices()

and subscribe to newName events, the connection is made between the app and the device.
I know this is something silly but reading through documentation is not helping.


(Eric) #2

You can have the child device call the parent SmartApp.


(codersaur) #3

Could it be that childDevice is defined within the if block only. Move def childDevice outside the if block, or move the subscribe() statements inside the first if (vMPlayer) block. In short, move the final } to below the subscribe statements.


(Stephan H.) #4

That worked!! I figured it was something simple. I’m now having issues passing commands from the subscription handlers to the childDevice but I have a feeling that @whoismoses parent call option might be helpful. This was so much simpler when I used an existing device. Trying to dynamically create the device may have pushed me past my skill level.

Researching parent calls in the ST docs…hopefully it’s not another rabbit hole. Thanks for the help guys…much appreciated.


(Stephan H.) #5

@whoismoses, your devicetype is changing what I thought was possible. Preferences in a handler? You can pass commands beyond a “sendEvent” to the smartApp? This opens up so many more options, I may have to reconsider half my code. Thank you…and dam you :slight_smile:

I realize the above shows how much of a novice I am to this. I feel like I’ve learned so much about how ST runs in the background while trying to make this simple smartApp that at the very least, it would help me troubleshoot issues in the future.

EDIT: see below from documentation: does this ONLY apply to LAN Service Managers. My smartapp will not be communicating directly through any LAN protocols. Any rela downsides to continuing down this road?

Best practices
For LAN Service Manager SmartApps, there are a couple items to keep in mind that might not be immediately apparent.

Use something static as the DNI for the child device, such as MAC address.
Avoid making calls from your child devices into the parent if possible, as this can lead to increased latency and unnecessary platform load. Instead, supply your child devices with enough information to make calls into the parent unnecessary, and use the Service Manager to manage any child device updates that need to happen based on network changes.