homebridge-harmony-control/src/index.ts

111 lines
3.9 KiB
TypeScript
Raw Normal View History

import { ControlUnit } from "./Accessories/ControlUnit";
import { Activity } from "./Models/Activity";
import { DeviceSetupItem } from "./Models/DeviceSetupItem";
import { Input, Output, Matrix } from "./Models/Matrix";
2019-06-13 14:00:29 +00:00
export default function (homebridge: any) {
homebridge.registerPlatform(
'homebridge-harmony-watson',
'HarmonyHubMatrix',
HarmonyMatrixPlatform,
true
2019-06-13 14:00:29 +00:00
);
};
class HarmonyMatrixPlatform {
log: any = {};
config: any = {};
api: any;
2019-06-13 14:00:29 +00:00
constructor(log: any, config: any, api: any) {
this.log = log;
this.config = config;
this.api = api;
2019-06-13 14:00:29 +00:00
this.log('INFO - Registering Harmony Matrix Platform');
}
/**
* Called by homebridge to gather accessories.
* @param callback
*/
accessories(callback: (accessories: Array<ControlUnit>) => void) {
//Parse matrix
let configInputs: any = this.config["Matrix"]["Inputs"];
let configOutputs: any = this.config["Matrix"]["Outputs"];
let inputs: Array<Input> = [];
let outputs: Array<Output> = [];
configInputs.forEach((configInput: any) => {
let inputDevice: string = configInput["InputDevice"];
let inputNumber: string = configInput["InputNumber"];
this.log(`INFO - Added input to matrix '${inputDevice}'`);
inputs.push({
inputDevice: inputDevice,
inputNumber: inputNumber
});
});
configOutputs.forEach((configOutput: any) => {
let outputDevice: string = configOutput["OutputDevice"];
let outputLetter: string = configOutput["OutputLetter"];
this.log(`INFO - Added output to matrix '${outputDevice}'`);
outputs.push({
outputDevice: outputDevice,
outputLetter: outputLetter
});
});
let matrix = new Matrix({
inputs: inputs,
outputs: outputs,
});
//Parse control units
let configControlUnits: any = this.config["ControlUnits"];
let controlUnits: Array<ControlUnit> = [];
configControlUnits.forEach((configControlUnit: any) => {
//Parse activities list
let configActivities: any = configControlUnit["Activities"];
let activities: Array<Activity> = [];
configActivities.forEach((configActivity: any) => {
//parse devices
let configDevices: any = configActivity["DeviceSetupList"];
let devices: Array<DeviceSetupItem> = [];
configDevices.forEach((configDevice: any) => {
//Add device
devices.push(new DeviceSetupItem({
deviceId: configDevice["DeviceName"],
input: configDevice["Input"]
}));
this.log(`INFO - Added device '${configDevice["DeviceName"]}' for activity '${configActivity["DisplayName"]}'`);
});
//Add activity
activities.push(new Activity({
volumeDeviceId: configActivity["VolumeDevice"],
controlDeviceId: configActivity["ControlDevice"],
outputDeviceId: configActivity["OutputDevice"],
displayName: configActivity["DisplayName"],
useMatrix: configActivity["UseMatrix"],
deviceList: devices
}));
this.log(`INFO - Added activity '${configActivity["DisplayName"]}'`);
});
2019-06-13 14:00:29 +00:00
//Add control unit
controlUnits.push(new ControlUnit({
displayName: configControlUnit["DisplayName"],
api: this.api,
log: this.log,
activities: activities,
matrix: matrix,
}));
this.log(`INFO - Added ControlUnit`);
});
callback(controlUnits);
2019-06-13 14:00:29 +00:00
}
}