From 6b3ea1452055415fda6c4cf05c6c59f78f294bf1 Mon Sep 17 00:00:00 2001 From: Brandon Watson Date: Wed, 9 Jun 2021 19:36:49 -0400 Subject: [PATCH] Fixing issue where active accessories were pruned --- src/platform.ts | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/platform.ts b/src/platform.ts index 0c5b9fa..27c9144 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -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, [