Progress commit

This commit is contained in:
watsonb8 2020-01-23 08:44:11 -05:00
parent e9693435ce
commit 5ddd1e1af0
9 changed files with 195 additions and 95 deletions

View File

@ -1,6 +1,6 @@
{
"name": "homebridge-harmony-control",
"version": "1.0.5",
"version": "1.0.6",
"description": "Homebridge platform to control smart home equipment by room.",
"main": "bin/index.js",
"scripts": {

View File

@ -48,6 +48,15 @@ export class DeviceButton implements IAccessory {
this._buttonState = false;
if (this._buttonInfo.NumberOfKeyPresses && this._buttonInfo.IsStateful) {
throw new Error("A button cannot be stateful and be pressed more than once");
}
//Assign default number of key presses
if (!this._buttonInfo.NumberOfKeyPresses) {
this._buttonInfo.NumberOfKeyPresses = 1;
}
this.platformAccessory = new this._homebridge.platformAccessory(this.name, this.generateUUID(), this._homebridge.hap.Accessory.Categories.SWITCH);
//@ts-ignore
@ -106,11 +115,13 @@ export class DeviceButton implements IAccessory {
this._buttonState = newState
await this._dataProvider.sendCommand(this._deviceCommand);
} else if (!this._buttonInfo.IsStateful) {
await this._dataProvider.sendCommand(this._deviceCommand);
//Send the number of configured key presses
for (let i = 0; i < this._buttonInfo.NumberOfKeyPresses; i++) {
await this._dataProvider.sendCommand(this._deviceCommand);
}
this._switchService.getCharacteristic(Characteristic.On).updateValue(false);
return callback(new Error("Normal Response"));
}
}
return callback();
}

View File

@ -5,6 +5,8 @@ import { RemoteKey } from '../Accessories/ControlUnit';
import { sleep } from '../Util/Sleep';
import { EventEmitter } from "events";
import { IDevice, ICommand } from '../Models/IDevice';
import { IHub } from "../Models/Config/IHub";
import { IDeviceConfig } from "../Models/Config/IDeviceConfig";
let Characteristic: HAPNodeJS.Characteristic;
@ -15,16 +17,16 @@ interface IActivityState {
}
interface IHarmonyDataProviderProps {
hubAddress: string,
hubs: Array<IHub>;
deviceConfigs: Array<IDeviceConfig>;
log: any,
matrix: IMatrix
}
class HarmonyDataProvider extends EventEmitter {
private _harmony: any;
private _log: any;
private _hubAddress: string = "";
private _connected: boolean = false;
private _deviceConfigs: Array<IDeviceConfig>;
private _hubs: { [hubName: string]: IHub } = {};
private _devices: { [name: string]: IDevice; } = {};
private _states: { [controlUnitName: string]: (IActivityState | undefined) } = {};
@ -34,18 +36,13 @@ class HarmonyDataProvider extends EventEmitter {
constructor(props: IHarmonyDataProviderProps) {
super();
this._log = props.log;
this._hubAddress = props.hubAddress;
//Construct hubs
props.hubs.forEach((value: IHub) => {
value.Harmony = new Harmony();
this._hubs[value.Name] = value;
});
this._matrix = props.matrix;
this._harmony = new Harmony();
//Listeners
this._harmony.on('open', () => {
this._connected = true;
});
this._harmony.on('close', () => {
this._connected = false;
});
this._deviceConfigs = props.deviceConfigs;
this.connect();
}
@ -82,8 +79,8 @@ class HarmonyDataProvider extends EventEmitter {
devicesToTurnOff = this.sanitizeDeviceList(devicesToTurnOff, controlUnitName);
//Turn off devices
devicesToTurnOff.forEach((device: IDevice) => {
this.powerOffDevice(device);
devicesToTurnOff.forEach(async (device: IDevice) => {
await device.powerOff();
});
this._states[controlUnitName] = undefined;
@ -112,7 +109,7 @@ class HarmonyDataProvider extends EventEmitter {
if (device && device.name && this._devices[device.name]) {
if (!device.on) {
this._log(`Turning on device ${device.name}`)
await this.powerOnDevice(device);
await device.powerOn();
}
}
}));
@ -123,8 +120,7 @@ class HarmonyDataProvider extends EventEmitter {
let device: IDevice = this.getDeviceFromName(value.DeviceName);
if (device && device.supportsCommand(`Input${value.Input}`)) {
let command: ICommand = device.getCommand(`Input${value.Input}`);
await this.sendCommand(command);
await device.sendCommand(`Input${value.Input}`)
}
})
);
@ -141,10 +137,10 @@ class HarmonyDataProvider extends EventEmitter {
//Route hdmi
if (matrixDevice.supportsCommand(inputCommandName) && matrixDevice.supportsCommand(outputCommandName)) {
await this.sendCommand(matrixDevice.getCommand(outputCommandName));
await this.sendCommand(matrixDevice.getCommand(inputCommandName));
await this.sendCommand(matrixDevice.getCommand(outputCommandName));
await this.sendCommand(matrixDevice.getCommand(inputCommandName));
await matrixDevice.sendCommand(outputCommandName);
await matrixDevice.sendCommand(inputCommandName);
await matrixDevice.sendCommand(outputCommandName);
await matrixDevice.sendCommand(inputCommandName);
}
}
@ -175,7 +171,7 @@ class HarmonyDataProvider extends EventEmitter {
if (device) {
if (device.on) {
this._log(`Turning off device ${device.name}`)
await this.powerOffDevice(device);
await device.powerOff();
}
}
})
@ -194,9 +190,7 @@ class HarmonyDataProvider extends EventEmitter {
let volumeUpCommand: string = "Volume Up"
if (this._states[controlUnitName]) {
let volumeDevice: IDevice = this.getDeviceFromName(this._states[controlUnitName]!.currentActivity.VolumeDevice);
if (volumeDevice.supportsCommand(volumeUpCommand)) {
this.sendCommand(volumeDevice.getCommand(volumeUpCommand));
}
await volumeDevice.sendCommand(volumeUpCommand);
}
}
@ -207,9 +201,7 @@ class HarmonyDataProvider extends EventEmitter {
let volumeDownCommand: string = "Volume Down"
if (this._states[controlUnitName]) {
let volumeDevice: IDevice = this.getDeviceFromName(this._states[controlUnitName]!.currentActivity.VolumeDevice);
if (volumeDevice.supportsCommand(volumeDownCommand)) {
this.sendCommand(volumeDevice.getCommand(volumeDownCommand));
}
await volumeDevice.sendCommand(volumeDownCommand);
}
}
@ -263,9 +255,7 @@ class HarmonyDataProvider extends EventEmitter {
}
}
if (device && device.supportsCommand(commandName)) {
this.sendCommand(device.getCommand(commandName));
}
await device.sendCommand(commandName);
}
}
@ -291,40 +281,28 @@ class HarmonyDataProvider extends EventEmitter {
}
}
/**
* Send a command to the harmony hub.
* @param command The command to send.
*/
public sendCommand = async (command: ICommand) => {
try {
//Execute command
let response = await this._harmony.sendCommand(JSON.stringify(command));
//Sleep
await sleep(800);
} catch (err) {
this._log(`ERROR - error sending command to harmony: ${err}`);
}
}
/**
* Connect to harmony and receive device info
*/
private connect = async () => {
await this._harmony.connect(this._hubAddress);
Object.values(this._hubs).forEach((value: IHub) => {
value.Harmony.connect(value.Ip);
});
let self = this;
setTimeout(async function () {
if (self._connected) {
let devices: any = await self._harmony.getDevices();
Object.keys(self._hubs).forEach(async (key: string) => {
let hub: IHub = self._hubs[key];
//Gather devices
let devices: any = await hub.Harmony.getDevices();
try {
await Promise.all(
//Add each to dictionary
devices.map(async (dev: any) => {
//get commands
let commands: { [name: string]: ICommand } = {};
let deviceCommands: any = await self._harmony.getDeviceCommands(dev.id);
let deviceCommands: any = await hub.Harmony.getDeviceCommands(dev.id);
deviceCommands.forEach((command: any) => {
commands[command.label] = command.action;
});
@ -333,6 +311,8 @@ class HarmonyDataProvider extends EventEmitter {
name: dev.label,
commands: commands,
on: false,
harmony: hub.Harmony,
log: self._log,
//Define device methods
supportsCommand(commandName: string): boolean {
let command = commands[commandName];
@ -340,6 +320,44 @@ class HarmonyDataProvider extends EventEmitter {
},
getCommand(commandName: string): ICommand {
return commands[commandName];
},
async powerOn(): Promise<void> {
let powerOnCommand: string = "Power On";
let powerToggleCommand: string = "Power Toggle";
if (this.supportsCommand(powerOnCommand)) {
await this.sendCommand(powerOnCommand);
this.on = true;
} else if (this.supportsCommand(powerToggleCommand)) {
await this.sendCommand(powerToggleCommand);
this.on = true;
}
},
async powerOff(): Promise<void> {
let powerOffCommand: string = "Power Off";
let powerToggleCommand: string = "Power Toggle";
if (this.supportsCommand(powerOffCommand)) {
await this.sendCommand(powerOffCommand);
this.on = false;
} else if (this.supportsCommand(powerToggleCommand)) {
await this.sendCommand(powerToggleCommand);
this.on = false;
}
},
async sendCommand(commandName: string): Promise<void> {
let command!: ICommand;
if (this.supportsCommand(commandName)) {
command = this.getCommand(commandName);
}
try {
//Execute command
await this.harmony.sendCommand(JSON.stringify(command));
//Sleep
await sleep(800);
} catch (err) {
this.log(`ERROR - error sending command to harmony: ${err}`);
}
}
}
}));
@ -349,40 +367,13 @@ class HarmonyDataProvider extends EventEmitter {
} catch (err) {
self._log(`ERROR - error connecting to harmony: ${err}`);
}
}
});
}, 1000);
}
/**
* Power off a device (Power toggle if no power off).
*/
private powerOffDevice = async (device: IDevice) => {
let powerOffCommand: string = "Power Off";
let powerToggleCommand: string = "Power Toggle";
if (device && device.supportsCommand(powerOffCommand)) {
await this.sendCommand(device.getCommand(powerOffCommand));
device.on = false;
} else if (device && device.supportsCommand(powerToggleCommand)) {
await this.sendCommand(device.getCommand(powerToggleCommand));
device.on = false;
}
}
/**
* Power on a device (Power toggle if no power on).
*/
private powerOnDevice = async (device: IDevice) => {
let powerOnCommand: string = "Power On";
let powerToggleCommand: string = "Power Toggle";
if (device && device.supportsCommand(powerOnCommand)) {
await this.sendCommand(device.getCommand(powerOnCommand));
device.on = true;
} else if (device && device.supportsCommand(powerToggleCommand)) {
await this.sendCommand(device.getCommand(powerToggleCommand));
device.on = true;
}
}
/**
* Get the IDevice by name.
* @param deviceName The device to retrieve.

View File

@ -1,6 +1,8 @@
import { IMatrix } from "./IMatrix";
import { IActivity } from "./IActivity";
import { IDeviceButton } from "./IDeviceButton";
import { IDeviceConfig } from "./IDeviceConfig";
import { IHub } from './IHub';
export interface IControlUnit {
DisplayName: string;
@ -9,8 +11,10 @@ export interface IControlUnit {
export interface IConfig {
hubIp: string;
EmitDevicesOnStartup: boolean,
Matrix: IMatrix
ControlUnits: Array<IControlUnit>
DeviceButtons: Array<IDeviceButton>
EmitDevicesOnStartup: boolean;
Matrix: IMatrix;
ControlUnits: Array<IControlUnit>;
DeviceButtons: Array<IDeviceButton>;
Devices: Array<IDeviceConfig>;
Hubs: Array<IHub>;
}

View File

@ -2,5 +2,6 @@ export interface IDeviceButton {
DeviceName: string;
ButtonName: string;
DisplayName: string;
NumberOfKeyPresses: number;
IsStateful: boolean;
}

View File

@ -0,0 +1,4 @@
export interface IDeviceConfig {
Name: string;
Hub: string;
}

View File

@ -0,0 +1,5 @@
export interface IHub {
Name: string;
Ip: string;
Harmony: any;
}

View File

@ -0,0 +1,79 @@
import { ICommand } from "./IDevice";
import { sleep } from "../Util/Sleep";
export interface IHarmonyDeviceProps {
id: string;
name: string;
harmony: any;
log: any;
commands: { [name: string]: ICommand };
}
export class HarmonyDevice {
private _harmony: any;
private _log: any;
private _commands: { [name: string]: ICommand } = {};
private _on: boolean;
constructor(props: IHarmonyDeviceProps) {
this.id = props.id;
this.name = props.name;
this._harmony = props.harmony;
this._on = false;
this._commands = props.commands;
}
public id: string;
public name: string;
//Define device methods
public supportsCommand(commandName: string): boolean {
let command = this._commands[commandName];
return (command) ? true : false;
}
public getCommand(commandName: string): ICommand {
return this._commands[commandName];
}
public async powerOn(): Promise<void> {
let powerOnCommand: string = "Power On";
let powerToggleCommand: string = "Power Toggle";
if (this.supportsCommand(powerOnCommand)) {
await this.sendCommand(powerOnCommand);
this._on = true;
} else if (this.supportsCommand(powerToggleCommand)) {
await this.sendCommand(powerToggleCommand);
this._on = true;
}
}
public async powerOff(): Promise<void> {
let powerOffCommand: string = "Power Off";
let powerToggleCommand: string = "Power Toggle";
if (this.supportsCommand(powerOffCommand)) {
await this.sendCommand(powerOffCommand);
this._on = false;
} else if (this.supportsCommand(powerToggleCommand)) {
await this.sendCommand(powerToggleCommand);
this._on = false;
}
}
public async sendCommand(commandName: string): Promise<void> {
let command!: ICommand;
if (this.supportsCommand(commandName)) {
command = this.getCommand(commandName);
}
try {
//Execute command
await this._harmony.sendCommand(JSON.stringify(command));
//Sleep
await sleep(800);
} catch (err) {
this._log(`ERROR - error sending command to harmony: ${err}`);
}
}
}

View File

@ -5,10 +5,15 @@ export interface ICommand {
}
export interface IDevice {
id: string,
name: string,
id: string;
name: string;
harmony: any;
log: any
supportsCommand(commandName: string): boolean,
getCommand(commandName: string): ICommand,
powerOn(): Promise<void>;
powerOff(): Promise<void>;
sendCommand(commandName: string): Promise<void>;
commands: { [name: string]: ICommand };
on: boolean;
}