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.
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
}
Another question, how do I delete/remove/detach/destroy a listener? I use makeCapabilityInstance to attach a listener to specific capabilities, but what if I want to detach a listener? I can’t find any examples, and I’m unable to figure out how to implement the destroy method myself
I have two functions one for attaching the eventlistener, and one for destroying it. I guess that is the reason this solution isn’t working for me; the instance created in the first function doesn’t seem to be available in the second function. And because I have many listeners I can’t make them global I think.
dit zou de oplossing zijn voor mijn probleem,
ik heb namelijk meerder apparaten in een device en ik kan niet uitlezen welke aan of uit gaat er gaat gewoon een aan of uit.