Push notifications from Homey Web Api

Im using Web Api for building web based Homey Dashboard. To retrieve updates (for devices, activities, alarms,) from my Homey, does Web Api support pushing events/notification to my web app or do I need to poll for new updates frequently?

Couldnt find this answer in the doc (https://developer.athom.com/docs/api)

The webAPI uses even emitters, for example for device capability updates it would look something :slight_smile:

With the group app I do it in the following way :

 try {
                            // Link our event to a state change of the capability.
                            this.instances[device.id][this.capabilities[i]] = device.makeCapabilityInstance(this.capabilities[i], listener);
                        } catch (error) {
                            this.error('validateDeviceInstances: error setting ' + device.id + ' capability ' + this.capabilities[i] + ' for ' +  device.name + '. Is device is ready? : ' + device.ready + '. Does the instance exist ' + exists)
}
1 Like

I’m pretty sure that makeCapabilityInstance doesn’t work from a web app (one that runs in a browser). I remember having tried and got errors inside of athom-api.min.js that I didn’t bother debugging because of lack of source maps.

That may have been the case, but it looks like it is "fixed’ now.

Homeydash uses the same method

1 Like

I see, that’s problem #1 solved. Problem #2 is that you’ll need dozens, if not hundreds of listeners if you want to catch all capability changes, but it looks like socket.io is being used under the hood, so it should be possible to tap into that event stream. I’ll try and see if I can get something to work.

Nice, this is very useful! It would be good if we could subscribe on specific topic/group in stead of each device capability, but it is still doable.

What exactly is HomeyAPI.ManagerFlowToken.getFlowTokens() used for? I thought this was also two-way sync for latest device and Homey status, but maybe I misunderstood?

I dont know anything about tapping into the socket.io event steam, but it sounds like a good way to go about it.

A dashboard is in fact incredibility similar to how the groups app works.

There are two ‘tricks’ a) you only need to subscribe to the capabilities the devices you have. b) You can easily loop though ‘all devices’ and for each device - loop through ’ all capabilities and just add a listener. - three lines of code :slight_smile:

Sorry I have not used the webAPI flow manager - Based on the name, I do know that flow tokens in the SDK refer to tags - and the two API generally match up. ( https://apps.developer.athom.com/tutorial-Flow-Tokens.html )

GetFlowTokens() does indeed return all ‘tags’ as they are called in the floweditor. In homeydas.com I use it to retrieve the sunrise, sunset and battery levels: https://github.com/daneedk/homeydash.com/blob/90c8d9ca77320e91b25e31ab73c274894809eac6/app/js/homeydash.app.js#L264

Could you please explain what tags are and when to use makeCapabilityInstance() and getFlowTokens()?

I managed to get event emitters to work for device capability updates. :+1: But can it also be used for Notifications?