Homey Community Forum

Help with registerCapabilityListener

#1

Hi,

Is there a better example than the SDK offers for registering a listener for a capability change event? Within my app I want to act on certain changes in capabilities on some of my devices and in Homey 1.x it was simple by using the device.on method.

Now, when trying to implement the registerCapabilityListener on Homey 2.0 I can’t get it working; it’s trowing me errors like ‘device.registerCapabilityListener is not a function’, and the old device.on method just doesn’t seem to work anymore. Any hints/examples are much appreciated.

TIA, cheers,
/Cacti

#2

It isn’t a better example, but it is a real world usage.


    /**
     * Initialises the capability listener.
     *
     * Basically : Registers every capability the group (MultipleCapabilityListener) has, so
     * when any of the group capabilities are changed, the function is called  which sets the
     * value of all of the devices to said value.
     *
     * As this is only listening for capabilities (which cant be changed in the settings), we never have to reload this.
     *
     * @returns {Promise<void>}
     */
    async initGroupListener() {

        /**
         * Register all of the capabilities at once with a (async) call back.
         *
         * values : An object with the changed capability values, e.g. { dim: 0.5 }
         * options : An object with optional properties, per capability, e.g. { dim: { duration: 300 } }
         *
         * Increase the time out - as large groups will require more time. Especially via 3rd Party server (alexa/google)
         */
        return this.registerMultipleCapabilityListener(this.capabilities, async (values, options) => {
            return this.updateDevicesCapabilities(values, options);
        }, 1000);
    }

Where :


 this.capabilities = await this.getCapabilities();

and

    /**
     * Updates the devices capabilities called from the groups capability listener.
     *
     * @param values
     * @returns {Promise<boolean>}
     */
    async updateDevicesCapabilities(values) {

        // Do stuff 

    } 
#3

Thanks Jamie!

I’ll see if I can get that working.

Regards
/Cacti

#4

Here is another example for listening in on a particular capability

This gets called from here while cycling through all devices.

1 Like
#5

Thanks guys. All is well now!

Cheers,
/Cacti

1 Like