106 lines
3.0 KiB
TypeScript
106 lines
3.0 KiB
TypeScript
import {
|
|
CharacteristicGetCallback,
|
|
CharacteristicSetCallback,
|
|
CharacteristicValue,
|
|
PlatformAccessory,
|
|
Service,
|
|
} from "homebridge";
|
|
import { HarmonyDataProvider } from "../dataProviders/harmonyDataProvider";
|
|
import { ISequence } from "../models/config/sequence";
|
|
import { HarmonyDevice } from "../models/harmonyDevice";
|
|
import { Platform } from "../platform";
|
|
import { sleep } from "../util";
|
|
|
|
export class Sequence {
|
|
private _devices: { [deviceName: string]: HarmonyDevice };
|
|
private _switchService: Service;
|
|
|
|
constructor(
|
|
private readonly _platform: Platform,
|
|
private readonly _accessory: PlatformAccessory,
|
|
private _dataProvider: HarmonyDataProvider,
|
|
private _sequence: ISequence
|
|
) {
|
|
this._accessory
|
|
.getService(this._platform.Service.AccessoryInformation)!
|
|
.setCharacteristic(
|
|
this._platform.Characteristic.Manufacturer,
|
|
"Brandon Watson"
|
|
)
|
|
.setCharacteristic(this._platform.Characteristic.Model, "Sequence Button")
|
|
.setCharacteristic(
|
|
this._platform.Characteristic.SerialNumber,
|
|
"123-456-789"
|
|
);
|
|
|
|
const switchUUID = this._platform.api.hap.uuid.generate(
|
|
`${this._accessory.displayName} Switch`
|
|
);
|
|
|
|
this._switchService =
|
|
this._accessory.getService(this._platform.Service.Switch) ||
|
|
this._accessory.addService(
|
|
this._platform.Service.Switch,
|
|
this._accessory.displayName,
|
|
switchUUID
|
|
);
|
|
|
|
this._switchService
|
|
.getCharacteristic(this._platform.Characteristic.On)
|
|
.on("set", this.onSwitchSet)
|
|
.updateValue(false)
|
|
.on("get", (callback: CharacteristicGetCallback): void => {
|
|
return callback(null);
|
|
});
|
|
|
|
this._devices = {};
|
|
// Get devices in sequence
|
|
for (const deviceName of _sequence.Steps.map((e) => e.DeviceName)) {
|
|
if (!deviceName) {
|
|
continue;
|
|
}
|
|
const device = this._dataProvider.getDeviceFromName(deviceName);
|
|
if (device) {
|
|
this._devices[deviceName] = device;
|
|
} else {
|
|
this._platform.log.warn(
|
|
`Device ${deviceName} was not found in harmony configuration`
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handler for switchSet command
|
|
* @param callback
|
|
*/
|
|
public onSwitchSet = async (
|
|
_value: CharacteristicValue,
|
|
callback: CharacteristicSetCallback
|
|
): Promise<void> => {
|
|
// Execute sequence
|
|
for (const step of this._sequence.Steps) {
|
|
await sleep(step.Delay);
|
|
const device: HarmonyDevice = this._devices[step.DeviceName ?? ""];
|
|
if (
|
|
device &&
|
|
step.DeviceCommand &&
|
|
device.supportsCommand(step.DeviceCommand)
|
|
) {
|
|
await device.sendCommand(step.DeviceCommand);
|
|
} else {
|
|
this._platform.log.warn(
|
|
`Attempted to execute command ${step.DeviceCommand} on device ${step.DeviceName} but the device or command was not found`
|
|
);
|
|
}
|
|
}
|
|
|
|
// Deactivate button
|
|
this._switchService
|
|
.getCharacteristic(this._platform.Characteristic.On)
|
|
.updateValue(false);
|
|
|
|
callback(null);
|
|
};
|
|
}
|