dgreif / homebridge-hatch-baby-rest

A homebridge plugin for Hatch Baby Rest night light
MIT License
74 stars 19 forks source link

Rest Plus Update Statements #61

Closed jbaldo closed 2 years ago

jbaldo commented 2 years ago

Hey, thanks for making this awesome library. I don't have Homekit stuff, but I'm trying to use your HatchBabyApi as a client for the API to do some automation. I'm hoping to implement a gradual volume fade over a certain period of time. Just being able to turn it on / off would be nice too.

I've got your Rest Plus example working. But now I'm trying to add in a statement to adjust the volume:

async function example() {
  light.onBrightness.subscribe((i) => console.log("Brightness", i));
  light.onVolume.subscribe((i) => console.log("Volume", i));
  light.onIsPowered.subscribe((i) => console.log("Is Powered", i));
  light.onAudioTrack.subscribe((i) => console.log("Audio Track", i));
  light.onBatteryLevel.subscribe((i) => console.log("Battery", i));
  light.setVolume(25);
}

and getting:

Unable to Update {Hatch Name} - No MQTT Client Registered

Since reading the API works, I'm guessing I'm just not doing this in an async way. I also notice that these error log message happens before the console logging. I see that the client gets created in the getDevices call. Is there something I need to wrap the setVolume / update call in to make it run only once the client is initiated?

Sorry if this is a very basic question. I never really got the hang of async and promises.