How do I use the Driver#ready() method in SDKv3

I have come on Slack on no solution, many maybe someone can help me here

I want to use the Driver method ready(), which is executed according to the documentation when onInit() is finished. Unfortunately it does not work as I expect because it is called several times.

Does anyone have a code example for me?

My test example:

module.exports = class Driver extends Homey.Driver {

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

  ready() {
    return Promise.resolve('Success')
      .then(this.log('Driver ready'));
  }

module.exports = class DingzDriver extends Driver {

  onInit(options = {}) {
    super.onInit(options);
    this.http = new Http(this.homey);
  }

Log:

[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

BDW: This also applies to the Device#ready() method.

THX Chris

@Jero wrote on Slack: “you should not override ready() you can await this.ready() somewhere in your code to respond to it”

Can you explain what exactly you want to achieve? Perhaps there are solutions that don’t require using ready() (or at least not in the way you’re trying to use it now).

Good morning,

As I understand it, the ready() method is a hook that is called when the onInit() method has been executed. And that’s how it’s implemented, ready() is not overridden, it’s just defined in the base class.

Baseclass:

  ready() {
    return Promise.resolve(this.deviceReady());
  }

  async deviceReady() {
    try {
      await this.setAvailable();
      await this.getDeviceValues();
      this.log('Device ready');
    } catch {}
  }

Subclass

async deviceReady() {
    try {
      super.deviceReady();

      const dingzDevice = await this.getDingzDevice();
      await this.initMotionDetector(dingzDevice);
      await this.initDingzSensors();
      this.updateSettingLabels();
    } catch {}
  }

It’s not a hook, it returns a promise that you can use as a sort-of hook. That promise will be resolved when onInit has been executed.

Also, you are overriding it because it’s declared in Homey.Driver and you’re overriding it in your subclass.

It’s still not clear to me what you’re trying to do though. If you want to call the deviceReady method after onInit has been run, just call it explicitly:

async onInit() {
  ...
  await this.deviceReady();
}