athombv / homey-apps-sdk-issues

This issue tracker is for Homey Developers using the Apps SDK.
21 stars 4 forks source link

Driver&Device ready() method does not work as I expect in SDKv3 #266

Closed cgHome closed 2 years ago

cgHome commented 2 years ago

I want to use the Driver&Device ready() method, which according to the documentation is executed when onInit() is finished.

However, the method is called several times and at the wrong time

Unfortunately I could not find an example in the documentation for the ready() method. And now I don't know, is it because of my implementation or is this a bug in SDKv3.


Example:

In the first version of the example I made a wrong copy & paste! Sorry..

module.exports = class Driver extends Homey.Driver {

  onInit(options = {}) {
    this.debug('onInit()');

    this.ready(this.driverReady());
  }

  driverReady() {
    return new Promise((resolve, reject) => {
      this.log('Driver ready');
    });
  }

Output:

[log] 2022-10-27 07:51:40 [DingzApp] dingzX app - v1.1.3 is running...
[log] 2022-10-27 07:51:41 [DingzApp] DingzDriver > Driver ready
[log] 2022-10-27 07:51:41 [DingzApp] DingzDriver > Driver ready
[log] 2022-10-27 07:51:41 [DingzApp] [DEBUG] DingzDriver > onInit()
[log] 2022-10-27 07:51:41 [DingzApp] [DEBUG] ### > HttpAPI API has been inited - undefined

Expected:

[log] 2022-10-27 07:51:40 [DingzApp] dingzX app - v1.1.3 is running...
[log] 2022-10-27 07:51:41 [DingzApp] [DEBUG] DingzDriver > onInit()
[log] 2022-10-27 07:51:41 [DingzApp] [DEBUG] ### > HttpAPI API has been inited - undefined
[log] 2022-10-27 07:51:41 [DingzApp] DingzDriver > Driver ready

My app code:

https://github.com/cgHome/homey.dingz/blob/be11f94e47c4d8a20fcc11a9beb6fb17d0b6c2bb/drivers/driver.js#L5 https://github.com/cgHome/homey.dingz/blob/be11f94e47c4d8a20fcc11a9beb6fb17d0b6c2bb/drivers/device.js#L50

jeroenwienk commented 2 years ago

https://community.homey.app/t/how-do-i-use-the-driver-ready-method-in-sdkv3/71280/3 Robert's answer is correct. It also the same I told you in Slack.

Calling this.ready() inside your driver returns a Promise that is resolved when the drivers devices are inited and the driver onInit method is done.

If you override ready like you did you are breaking the SDK and your app.

cgHome commented 2 years ago

I solved my problem differently. THX Chris