Fixed device buttons
This commit is contained in:
parent
97c0f625db
commit
6f7fc4c2af
@ -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"));
|
||||
|
@ -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<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.
|
||||
* @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<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.
|
||||
* @param devicesToTurnOn The list of devices to modify.
|
||||
|
@ -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];
|
||||
}
|
||||
|
23
src/index.ts
23
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}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user