How to unsubscribe to attribute specific callbacks


#1

Hi -

The API allows you to subscribe to callbacks for changes to specific device attributes but doesn’t allow you to unsubscribe to such changes. The API does have an unsubscribe which removes all subscriptions for a device but I’m only looking to do it for a single attribute, not all of them. Any insight would be appreciated!

Stephen


(Convinced ST will never be unbroken…) #2

This is documented:

unsubscribe
Deletes all subscriptions for the SmartApp.

unsubscribe(device)
Deletes all subscriptions for the specified device or devices. Argument can be either a single device or a list of devices.

(Joshua Lyon (SharpTools.io Dashboard)) #3

@scottinpollock I think what @snbaker is saying is that they can call a subscribe(device, attributeName, eventHandler) which subscribes to the changing of a specific attribute on a device. The unsubscribe(device) unsubscribes ALL subscriptions for the device and I think @snbaker is looking for something closer to unsubscribe(device, attributeName) which doesn’t appear to exist.

I would imagine their use case involves allowing the user to subscribe to multiple attributes and they may want to unsubscribe to only a single attribute at a time.

//STEP 1: subscribe to the switch event of the light switch
subscribe(myLightSwitch, 'switch', lightHandler)

//STEP 2: subscribe to the level event of the light switch
subscribe(myLightSwitch, 'level', levelHandler)

//STEP 3: only unsubscribe from the switch, leaving the level subscription in place
unsubscribe(myLightSwitch, 'switch');  //  <---theoretical command

After a bit of testing, I noted that there was a signature for unsubscribe(java.lang.String) and when I passed that a subscription ID, it would allow me to unsubscribe from a particular subscription (eg. device/attribute pair), but further testing seems to indicate this wants a handler as the string.

groovy.lang.MissingMethodException: No signature of method: unsubscribe() is applicable for argument types: (physicalgraph.app.EventSubscriptionWrapper)
Possible solutions: unsubscribe(), unsubscribe(java.lang.String), unsubscribe(java.util.Collection), unsubscribe(physicalgraph.app.DeviceWrapper), subscribe(physicalgraph.app.AttributeWrapper), subscribe(physicalgraph.app.DeviceWrapper) @ line 311


#4

@joshua_lyon has it exactly right (he describes my issue much more clearly than I - thank you Joshua). I’ll check out that undocumented method but hopefully it’ll become more official!

Thanks for your help!


(Joshua Lyon (SharpTools.io Dashboard)) #5

I did some additional testing and was going to post some sample code, but this doesn’t seem to be working as I originally thought it was. From what I’m interpreting in the logs, the unsubscribe(STRING) seems to be looking for a handler name.

[TRACE] AppName is attempting to unsubscribe from handler STRING

Using the example from my original post, you could use something like unsubscribe('levelHandler') and the system would unsubscribe all items which were using the levelHandler.

Perhaps @mager or someone from ST can chime in on if there is a way to remove a specific event subscription (eg. device/attribute).