Changes to Subscribe()

Previously, I would get a subscription object the result of subscribe(), but it seems this is no longer the case. Looking at the documentation, it seems subscribe() now returns void.

I had noted that sometimes the subscribe() method would not return a subscription, so I put in a fallback to look through app.subscriptions to find the recently subscribed item so I could return it. It seems that even this doesn’t work any more as the subscription is not available immediately.

(Note that the Logitech Harmony Connect code in the IDE ‘From Template’ section uses the same logic listed above.)

I now have a final fallback to return a ‘pending’ subscription, but it seems like there has to be a better way of handling this.

@Jim any thoughts?

//example code 
//get the result of the subscribe call 
subscription = subscribe(device, attribute, deviceHandler)
   //if the subscription isn't isn't available, let's try to find it another way
   if (!subscription || !subscription.eventSubscription) {
      //get the subscription from the list of subscriptions (using the attributes we just used to subscribe)
      subscription = app.subscriptions?.find { it.deviceId && it.deviceId == stateKey && it.data == attribute && it.handler == 'deviceHandler' }
      //if that doesn't work, go jump in a river
      if(!subscription || !subscription.eventSubscription){
          //do something else 
2 Likes

@mager ,

Will we ever get a developer channel so that developers can get detailed notifications of supported syntax changes? Preferably in advance of release?

Given that so much of the value of ST to so many people comes from the very sophisticated smartapps that community members have developed, it only seems reasonable that that value be protected with detailed developer support with regard to platform changes.

4 Likes

By chance do you have an example/scenario when this doesn’t work, or does it simply appear to be random?

It seems to consistently fail as subscribe() always seems to return void now… and even if I try to fallback to find the new subscription through app.subscriptions.find immediately after subscribing, it seems to consistently not be available immediately after subscribing now.
Edit: I guess I need to do some more testing - it was consistently failing last night, but I am seeing varying results this morning and I haven’t identified a pattern yet. I have some suspicions that it could be related to the eventually consistent nature of the platform and perhaps performance/load of the system running the SmartApp.

Note that if I wait just a second and then make a separate request to look through app.subscriptions, I can find the subscription.

I would rather not make two calls to the platform or use a simple pause as I was hoping for something more deterministic in nature, so I was hoping someone had some insights as to how I could get the subscription information in the same method call.

Yes, Jim and I are coming up with a plan for a changelog. It could live on our developer portal or in the docs.

2 Likes

@Jim @mager @April any thoughts on how I can subscribe to a device and get the resulting subscription in a single call? The code I was using had been working for several months, so it seems this could be related to a platform update?