From 6f7fc4c2afad9c73bb021f4c10137b5b16581d14 Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Fri, 24 Jan 2020 21:47:13 -0500 Subject: [PATCH] Fixed device buttons --- src/Accessories/DeviceButton.ts | 16 +++--- src/DataProviders/HarmonyDataProvider.ts | 66 +++++++++++++----------- src/Models/HarmonyHub.ts | 8 ++- src/index.ts | 23 +++++---- 4 files changed, 61 insertions(+), 52 deletions(-) diff --git a/src/Accessories/DeviceButton.ts b/src/Accessories/DeviceButton.ts index dc01b53..d37c4fa 100644 --- a/src/Accessories/DeviceButton.ts +++ b/src/Accessories/DeviceButton.ts @@ -2,6 +2,7 @@ import HarmonyDataProvider from "../DataProviders/HarmonyDataProvider"; import { IDeviceButton } from "../Models/Config"; import { IAccessory } from "./IAccessory"; import { ICommand } from "../Models"; +import { HarmonyDevice } from "../Models/HarmonyDevice"; let Service: HAPNodeJS.Service; let Characteristic: HAPNodeJS.Characteristic; @@ -29,7 +30,7 @@ export class DeviceButton implements IAccessory { private _dataProvider: HarmonyDataProvider; - private _deviceCommand?: ICommand; + private _device!: HarmonyDevice; private _buttonState: boolean; @@ -101,23 +102,20 @@ export class DeviceButton implements IAccessory { } //Get device command if we don't have it - if (!this._deviceCommand) { - let cmd = this._dataProvider.getCommand(this._buttonInfo.ButtonName, this._buttonInfo.DeviceName); - if (cmd) { - this._deviceCommand = cmd; - } + if (!this._device) { + this._device = this._dataProvider.getDeviceFromName(this._buttonInfo.DeviceName); } //Execute command - if (this._deviceCommand) { + if (this._device) { //change state if stateful if (this._buttonInfo.IsStateful && this._buttonState != newState) { this._buttonState = newState - //TODO await this._dataProvider.sendCommand(this._deviceCommand); + await this._device.sendCommand(this._buttonInfo.ButtonName); } else if (!this._buttonInfo.IsStateful) { //Send the number of configured key presses for (let i = 0; i < this._buttonInfo.NumberOfKeyPresses; i++) { - //TODO await this._dataProvider.sendCommand(this._deviceCommand); + await this._device.sendCommand(this._buttonInfo.ButtonName); } this._switchService.getCharacteristic(Characteristic.On).updateValue(false); return callback(new Error("Normal Response")); diff --git a/src/DataProviders/HarmonyDataProvider.ts b/src/DataProviders/HarmonyDataProvider.ts index b4872f1..422454f 100644 --- a/src/DataProviders/HarmonyDataProvider.ts +++ b/src/DataProviders/HarmonyDataProvider.ts @@ -49,6 +49,10 @@ class HarmonyDataProvider extends EventEmitter { // return this._devicesByHub; // } + public get hubs(): { [hubName: string]: HarmonyHub } { + return this._hubs; + } + /** * Power on all devices in an activity. */ @@ -265,41 +269,11 @@ class HarmonyDataProvider extends EventEmitter { return this._states[controlUnitName] ? this._states[controlUnitName]!.currentActivity : undefined; } - /** - * Gets device button commands - * @param deviceCommandName The device command name - * @param deviceName The device name - */ - public getCommand(deviceCommandName: string, deviceName: string): ICommand | undefined { - const device: HarmonyDevice = this.getDeviceFromName(deviceName); - if (device && device.supportsCommand(deviceCommandName)) { - return device.getCommand(deviceCommandName); - } else { - return undefined; - } - } - - 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(); - } - // await Promise.all( - // hubs.map(async (hub: IHub): Promise => { - // const newHarmonyHub = new HarmonyHub(hub.Ip, this._log); - // this._hubs[hub.Name] = newHarmonyHub; - // await newHarmonyHub.initialize(); - // }) - // ) - } - /** * Get the IDevice by name. * @param deviceName The device to retrieve. */ - private getDeviceFromName(deviceName: string): HarmonyDevice { + public getDeviceFromName(deviceName: string): HarmonyDevice { let device: HarmonyDevice | undefined; try { device = this._hubs[this._hubsByDevice[deviceName]].getDeviceByName(deviceName); @@ -310,6 +284,36 @@ class HarmonyDataProvider extends EventEmitter { return device!; } + // /** + // * Gets device button commands + // * @param deviceCommandName The device command name + // * @param deviceName The device name + // */ + // public getCommand(deviceCommandName: string, deviceName: string): ICommand | undefined { + // const device: HarmonyDevice = this.getDeviceFromName(deviceName); + // if (device && device.supportsCommand(deviceCommandName)) { + // return device.getCommand(deviceCommandName); + // } else { + // return undefined; + // } + // } + + 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(); + // } + await Promise.all( + hubs.map(async (hub: IHub): Promise => { + const newHarmonyHub = new HarmonyHub(hub.Name, hub.Ip, this._log); + this._hubs[hub.Name] = newHarmonyHub; + await newHarmonyHub.initialize(); + }) + ) + } + /** * Helper function to make sure no control unit depends on device list. * @param devicesToTurnOn The list of devices to modify. diff --git a/src/Models/HarmonyHub.ts b/src/Models/HarmonyHub.ts index 048e200..a78a904 100644 --- a/src/Models/HarmonyHub.ts +++ b/src/Models/HarmonyHub.ts @@ -10,17 +10,23 @@ export class HarmonyHub extends EventEmitter { private _ip: string; private _harmony: any; private _log: any; + private _name: string; - constructor(ipAddress: string, log: any) { + constructor(hubName: string, ipAddress: string, log: any) { super(); this._ip = ipAddress; this._log = log; + this._name = hubName; } public get devices(): { [deviceName: string]: HarmonyDevice } { return this._devices; } + public get hubName(): string { + return this._name; + } + public getDeviceByName = (deviceName: string): HarmonyDevice => { return this._devices[deviceName]; } diff --git a/src/index.ts b/src/index.ts index ace114a..01e960d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider"; import * as Config from "./Models/Config"; import { IDevice } from "./Models"; import { HarmonyDevice } from "./Models/HarmonyDevice"; +import { HarmonyHub } from "./Models/HarmonyHub"; let Accessory: any; let Homebridge: any; @@ -51,18 +52,18 @@ class HarmonyMatrixPlatform { if (this.config.EmitDevicesOnStartup) { this.dataProvider.on("Ready", () => { - // TODO const hubs: { [hubName: string]: { [deviceName: string]: HarmonyDevice } } = this.dataProvider!.devicesByHub; - // Object.keys(hubs).forEach((hubName: string) => { - // const deviceDictionary = hubs[hubName]; - // this.log(`${hubName}`) + const hubs = this.dataProvider!.hubs; + Object.values(hubs).forEach((hub: HarmonyHub) => { + const deviceDictionary = hub.devices; + this.log(`${hub.hubName}`) - // Object.values(deviceDictionary).forEach((device: HarmonyDevice) => { - // this.log(` ${device.name} : ${device.id}`); - // Object.keys(device.commands).forEach((command: string) => { - // this.log(` ${command}`); - // }); - // }); - // }); + Object.values(deviceDictionary).forEach((device: HarmonyDevice) => { + this.log(` ${device.name} : ${device.id}`); + Object.keys(device.commands).forEach((command: string) => { + this.log(` ${command}`); + }); + }); + }); }); }