Fixed device buttons

This commit is contained in:
watsonb8 2020-01-24 21:47:13 -05:00
parent 97c0f625db
commit 6f7fc4c2af
4 changed files with 61 additions and 52 deletions

View File

@ -2,6 +2,7 @@ import HarmonyDataProvider from "../DataProviders/HarmonyDataProvider";
import { IDeviceButton } from "../Models/Config"; import { IDeviceButton } from "../Models/Config";
import { IAccessory } from "./IAccessory"; import { IAccessory } from "./IAccessory";
import { ICommand } from "../Models"; import { ICommand } from "../Models";
import { HarmonyDevice } from "../Models/HarmonyDevice";
let Service: HAPNodeJS.Service; let Service: HAPNodeJS.Service;
let Characteristic: HAPNodeJS.Characteristic; let Characteristic: HAPNodeJS.Characteristic;
@ -29,7 +30,7 @@ export class DeviceButton implements IAccessory {
private _dataProvider: HarmonyDataProvider; private _dataProvider: HarmonyDataProvider;
private _deviceCommand?: ICommand; private _device!: HarmonyDevice;
private _buttonState: boolean; private _buttonState: boolean;
@ -101,23 +102,20 @@ export class DeviceButton implements IAccessory {
} }
//Get device command if we don't have it //Get device command if we don't have it
if (!this._deviceCommand) { if (!this._device) {
let cmd = this._dataProvider.getCommand(this._buttonInfo.ButtonName, this._buttonInfo.DeviceName); this._device = this._dataProvider.getDeviceFromName(this._buttonInfo.DeviceName);
if (cmd) {
this._deviceCommand = cmd;
}
} }
//Execute command //Execute command
if (this._deviceCommand) { if (this._device) {
//change state if stateful //change state if stateful
if (this._buttonInfo.IsStateful && this._buttonState != newState) { if (this._buttonInfo.IsStateful && this._buttonState != newState) {
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) { } else if (!this._buttonInfo.IsStateful) {
//Send the number of configured key presses //Send the number of configured key presses
for (let i = 0; i < this._buttonInfo.NumberOfKeyPresses; i++) { 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); this._switchService.getCharacteristic(Characteristic.On).updateValue(false);
return callback(new Error("Normal Response")); return callback(new Error("Normal Response"));

View File

@ -49,6 +49,10 @@ class HarmonyDataProvider extends EventEmitter {
// return this._devicesByHub; // return this._devicesByHub;
// } // }
public get hubs(): { [hubName: string]: HarmonyHub } {
return this._hubs;
}
/** /**
* Power on all devices in an activity. * Power on all devices in an activity.
*/ */
@ -265,41 +269,11 @@ class HarmonyDataProvider extends EventEmitter {
return this._states[controlUnitName] ? this._states[controlUnitName]!.currentActivity : undefined; 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<IHub>) => {
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<void> => {
// const newHarmonyHub = new HarmonyHub(hub.Ip, this._log);
// this._hubs[hub.Name] = newHarmonyHub;
// await newHarmonyHub.initialize();
// })
// )
}
/** /**
* Get the IDevice by name. * Get the IDevice by name.
* @param deviceName The device to retrieve. * @param deviceName The device to retrieve.
*/ */
private getDeviceFromName(deviceName: string): HarmonyDevice { public getDeviceFromName(deviceName: string): HarmonyDevice {
let device: HarmonyDevice | undefined; let device: HarmonyDevice | undefined;
try { try {
device = this._hubs[this._hubsByDevice[deviceName]].getDeviceByName(deviceName); device = this._hubs[this._hubsByDevice[deviceName]].getDeviceByName(deviceName);
@ -310,6 +284,36 @@ class HarmonyDataProvider extends EventEmitter {
return device!; 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<IHub>) => {
// 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<void> => {
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. * Helper function to make sure no control unit depends on device list.
* @param devicesToTurnOn The list of devices to modify. * @param devicesToTurnOn The list of devices to modify.

View File

@ -10,17 +10,23 @@ export class HarmonyHub extends EventEmitter {
private _ip: string; private _ip: string;
private _harmony: any; private _harmony: any;
private _log: any; private _log: any;
private _name: string;
constructor(ipAddress: string, log: any) { constructor(hubName: string, ipAddress: string, log: any) {
super(); super();
this._ip = ipAddress; this._ip = ipAddress;
this._log = log; this._log = log;
this._name = hubName;
} }
public get devices(): { [deviceName: string]: HarmonyDevice } { public get devices(): { [deviceName: string]: HarmonyDevice } {
return this._devices; return this._devices;
} }
public get hubName(): string {
return this._name;
}
public getDeviceByName = (deviceName: string): HarmonyDevice => { public getDeviceByName = (deviceName: string): HarmonyDevice => {
return this._devices[deviceName]; return this._devices[deviceName];
} }

View File

@ -3,6 +3,7 @@ import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider";
import * as Config from "./Models/Config"; import * as Config from "./Models/Config";
import { IDevice } from "./Models"; import { IDevice } from "./Models";
import { HarmonyDevice } from "./Models/HarmonyDevice"; import { HarmonyDevice } from "./Models/HarmonyDevice";
import { HarmonyHub } from "./Models/HarmonyHub";
let Accessory: any; let Accessory: any;
let Homebridge: any; let Homebridge: any;
@ -51,18 +52,18 @@ class HarmonyMatrixPlatform {
if (this.config.EmitDevicesOnStartup) { if (this.config.EmitDevicesOnStartup) {
this.dataProvider.on("Ready", () => { this.dataProvider.on("Ready", () => {
// TODO const hubs: { [hubName: string]: { [deviceName: string]: HarmonyDevice } } = this.dataProvider!.devicesByHub; const hubs = this.dataProvider!.hubs;
// Object.keys(hubs).forEach((hubName: string) => { Object.values(hubs).forEach((hub: HarmonyHub) => {
// const deviceDictionary = hubs[hubName]; const deviceDictionary = hub.devices;
// this.log(`${hubName}`) this.log(`${hub.hubName}`)
// Object.values(deviceDictionary).forEach((device: HarmonyDevice) => { Object.values(deviceDictionary).forEach((device: HarmonyDevice) => {
// this.log(` ${device.name} : ${device.id}`); this.log(` ${device.name} : ${device.id}`);
// Object.keys(device.commands).forEach((command: string) => { Object.keys(device.commands).forEach((command: string) => {
// this.log(` ${command}`); this.log(` ${command}`);
// }); });
// }); });
// }); });
}); });
} }