Fixing issue where active accessories were pruned
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Brandon Watson 2021-06-09 19:36:49 -04:00
parent 5dc28b2409
commit 6b3ea14520

View File

@ -15,7 +15,6 @@ import { ISequence } from "./Models/Config/ISequence";
import { HarmonyDevice } from "./Models/HarmonyDevice";
import { HarmonyHub } from "./Models/HarmonyHub";
import { PLATFORM_NAME, PLUGIN_NAME } from "./settings";
import { sleep } from "./Util";
export class Platform implements DynamicPlatformPlugin {
constructor(
@ -58,6 +57,8 @@ export class Platform implements DynamicPlatformPlugin {
this.discoverControlUnitAccessories(dataProvider);
this.discoverDeviceButtonAccessories(dataProvider);
this.discoverSequenceAccessories(dataProvider);
this.pruneAccessories();
if (this.config.EmitDevicesOnStartup) {
const hubs = this.dataProvider!.hubs;
Object.values(hubs).forEach((hub: HarmonyHub) => {
@ -113,6 +114,7 @@ export class Platform implements DynamicPlatformPlugin {
uuid
);
accessory.context["DeviceName"] = unit.DisplayName;
accessory.context["Type"] = typeof ControlUnit.name;
new ControlUnit(this, accessory, dataProvider, unit.Activities);
@ -147,34 +149,23 @@ export class Platform implements DynamicPlatformPlugin {
uuid
);
accessory.context["DeviceName"] = button.DisplayName;
accessory.context["Type"] = typeof DeviceButton;
new DeviceButton(this, accessory, dataProvider, button);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
accessory,
]);
this.accessories.push(accessory);
}
});
// Remove old device buttons
for (const accessory of this.accessories) {
if (
this.config.DeviceButtons.filter(
(button) => button.DisplayName === accessory.displayName
).length === 0
) {
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
accessory,
]);
}
}
}
/**
* Discover new sequence accessories
* @param dataProvider
*/
public discoverSequenceAccessories(dataProvider: HarmonyDataProvider): void {
private discoverSequenceAccessories(dataProvider: HarmonyDataProvider): void {
this.config.Sequences.forEach((sequence: ISequence) => {
const uuid = this.api.hap.uuid.generate(sequence.DisplayName);
const existingAccessory = this.accessories.find((e) => e.UUID === uuid);
@ -193,20 +184,29 @@ export class Platform implements DynamicPlatformPlugin {
uuid
);
accessory.context["DeviceName"] = sequence.DisplayName;
accessory.context["Type"] = typeof "Sequence";
new Sequence(this, accessory, dataProvider, sequence);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
accessory,
]);
this.accessories.push(accessory);
}
});
}
// Remove old device buttons
private pruneAccessories(): void {
// Remove devices not discovered
const listOfConfiguredAccessories = [
...this.config.DeviceButtons.map((e) => e.DisplayName),
...this.config.Sequences.map((e) => e.DisplayName),
...this.config.ControlUnits.map((e) => e.DisplayName),
];
for (const accessory of this.accessories) {
if (
this.config.Sequences.filter(
(sequence) => sequence.DisplayName === accessory.displayName
listOfConfiguredAccessories.filter(
(displayName) => displayName === accessory.displayName
).length === 0
) {
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [