Update to use homebridge types
This commit is contained in:
119
src/index.ts
119
src/index.ts
@@ -1,118 +1,11 @@
|
||||
import * as Accessories from "./Accessories";
|
||||
import HarmonyDataProvider from "./DataProviders/HarmonyDataProvider";
|
||||
import * as Config from "./Models/Config";
|
||||
import { HarmonyDevice } from "./Models/HarmonyDevice";
|
||||
import { HarmonyHub } from "./Models/HarmonyHub";
|
||||
import { API, Logging, StaticPlatformPlugin, AccessoryPlugin } from "homebridge";
|
||||
import { API } from "homebridge";
|
||||
|
||||
let Accessory: any;
|
||||
let Homebridge: API;
|
||||
import { PLATFORM_NAME } from "./settings";
|
||||
import { Platform } from "./platform";
|
||||
|
||||
/**
|
||||
* Main entry.
|
||||
* @param homebridge
|
||||
* This method registers the platform with Homebridge
|
||||
*/
|
||||
export default function (homebridge: API) {
|
||||
Homebridge = homebridge;
|
||||
Accessory = homebridge.platformAccessory;
|
||||
homebridge.registerPlatform(
|
||||
'homebridge-harmony-watson',
|
||||
'HarmonyHubMatrix',
|
||||
HarmonyMatrixPlatform,
|
||||
);
|
||||
export = (api: API) => {
|
||||
api.registerPlatform(PLATFORM_NAME, Platform);
|
||||
};
|
||||
|
||||
class HarmonyMatrixPlatform implements StaticPlatformPlugin {
|
||||
log: Logging;
|
||||
config: Config.IConfig;
|
||||
api: API;
|
||||
dataProvider: HarmonyDataProvider | null;
|
||||
accessoryList: Array<AccessoryPlugin> = [];
|
||||
|
||||
constructor(logger: Logging, config: any, api: API) {
|
||||
this.log = logger;
|
||||
this.config = config;
|
||||
this.api = api;
|
||||
this.log.info('INFO - Registering Harmony Matrix Platform');
|
||||
this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
|
||||
|
||||
this.dataProvider = null;
|
||||
|
||||
if (this.config) {
|
||||
//construct data provider
|
||||
this.dataProvider = new HarmonyDataProvider({
|
||||
hubs: this.config.Hubs,
|
||||
deviceConfigs: this.config.Devices,
|
||||
matrix: this.config.Matrix,
|
||||
log: this.log
|
||||
});
|
||||
|
||||
//Emit devices if requested
|
||||
|
||||
this.dataProvider.on("Ready", () => {
|
||||
this.log("All hubs connected");
|
||||
if (this.config.EmitDevicesOnStartup) {
|
||||
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}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for didFinishLaunching
|
||||
* Happens after constructor
|
||||
*/
|
||||
didFinishLaunching() {
|
||||
this.log(`Publishing external accessories`);
|
||||
|
||||
//This is required in order to have multiple tv remotes on one platform
|
||||
this.accessoryList.forEach((accessory: AccessoryPlugin) => {
|
||||
if (accessory instanceof Accessories.ControlUnit) {
|
||||
this.api.publishExternalAccessories("HarmonyMatrixPlatform", [accessory.platformAccessory]);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by homebridge to gather accessories.
|
||||
* @param callback
|
||||
*/
|
||||
accessories(callback: (accessories: Array<AccessoryPlugin>) => void) {
|
||||
|
||||
//Add control units
|
||||
this.config.ControlUnits.forEach((unit: Config.IControlUnit) => {
|
||||
this.accessoryList.push(new Accessories.ControlUnit({
|
||||
dataProvider: this.dataProvider!,
|
||||
displayName: unit.DisplayName,
|
||||
api: this.api,
|
||||
log: this.log,
|
||||
activities: unit.Activities,
|
||||
}));
|
||||
});
|
||||
|
||||
//Add device buttons
|
||||
this.config.DeviceButtons.forEach((button: Config.IDeviceButton) => {
|
||||
this.accessoryList.push(new Accessories.DeviceButton({
|
||||
dataProvider: this.dataProvider!,
|
||||
buttonName: button.ButtonName,
|
||||
displayName: button.DisplayName,
|
||||
deviceInfo: button,
|
||||
api: this.api,
|
||||
log: this.log
|
||||
}))
|
||||
});
|
||||
callback(this.accessoryList);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user