From 3ac6ffb8a32dce21c84ad7fa673cb2710259ac89 Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Fri, 24 Jan 2020 23:17:33 -0500 Subject: [PATCH] Worked around bug where receiver was not turning off --- src/DataProviders/HarmonyDataProvider.ts | 17 ++++++++++------- src/Models/HarmonyDevice.ts | 7 +++++++ src/Models/HarmonyHub.ts | 1 - src/index.ts | 9 +++++---- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/DataProviders/HarmonyDataProvider.ts b/src/DataProviders/HarmonyDataProvider.ts index 422454f..592ab41 100644 --- a/src/DataProviders/HarmonyDataProvider.ts +++ b/src/DataProviders/HarmonyDataProvider.ts @@ -82,7 +82,9 @@ class HarmonyDataProvider extends EventEmitter { //Turn off devices devicesToTurnOff.forEach(async (device: HarmonyDevice) => { - await device.powerOff(); + if (device) { + await device.powerOff(); + } }); this._states[controlUnitName] = undefined; @@ -299,16 +301,17 @@ class HarmonyDataProvider extends EventEmitter { // } private connect = async (hubs: Array) => { - // for (let i = 0; i < hubs.length; i++) { - // const hub = hubs[i]; - // const newHarmonyHub = new HarmonyHub(hub.Ip, this._log); - // this._hubs[hub.Name] = newHarmonyHub; - // await newHarmonyHub.initialize(); - // } + let readyCount = 0; await Promise.all( hubs.map(async (hub: IHub): Promise => { const newHarmonyHub = new HarmonyHub(hub.Name, hub.Ip, this._log); this._hubs[hub.Name] = newHarmonyHub; + newHarmonyHub.on("Ready", () => { + readyCount++; + if (readyCount === Object.keys(this._hubs).length) { + this.emit("Ready"); + } + }) await newHarmonyHub.initialize(); }) ) diff --git a/src/Models/HarmonyDevice.ts b/src/Models/HarmonyDevice.ts index 9a321d7..cecece4 100644 --- a/src/Models/HarmonyDevice.ts +++ b/src/Models/HarmonyDevice.ts @@ -76,6 +76,13 @@ export class HarmonyDevice { try { //Execute command + //HACK to fix Harmon Kardon receiver not turning off + if (command.command === "PowerOff") { + for (let i = 0; i < 2; i++) { + await this._harmony.sendCommand(JSON.stringify(command)); + } + + } await this._harmony.sendCommand(JSON.stringify(command)); //Sleep diff --git a/src/Models/HarmonyHub.ts b/src/Models/HarmonyHub.ts index a78a904..8e4ce88 100644 --- a/src/Models/HarmonyHub.ts +++ b/src/Models/HarmonyHub.ts @@ -55,7 +55,6 @@ export class HarmonyHub extends EventEmitter { harmony: this._harmony }); })); - this._log(`Harmony data provider ready`); this.emit("Ready"); } catch (err) { diff --git a/src/index.ts b/src/index.ts index 01e960d..9a904c4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -49,9 +49,10 @@ class HarmonyMatrixPlatform { }); //Emit devices if requested - if (this.config.EmitDevicesOnStartup) { - this.dataProvider.on("Ready", () => { + this.dataProvider.on("Ready", () => { + this.log("All hubs connected"); + if (this.config.EmitDevicesOnStartup) { const hubs = this.dataProvider!.hubs; Object.values(hubs).forEach((hub: HarmonyHub) => { const deviceDictionary = hub.devices; @@ -64,9 +65,9 @@ class HarmonyMatrixPlatform { }); }); }); + } + }); - }); - } } }