From 35da722e280a6e2b84519996c5e6d7e73f4b4216 Mon Sep 17 00:00:00 2001 From: Just Jam Date: Thu, 12 Dec 2024 17:44:33 +0000 Subject: [PATCH] delete unpaired bridges when toggling bridges off --- CHANGELOG.md | 1 + .../plugin-bridge/plugin-bridge.component.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e33175a42..ebf98fb58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to `homebridge-config-ui-x` will be documented in this file. - format svg files nicely as part of lint - upgrade angular from `v18` to `v19` +- delete unpaired bridges when toggling bridges off (#2284) (@justjam2013) (fixes [#2257](https://github.com/homebridge/homebridge-config-ui-x/issues/2257)) ## v4.65.2 (2024-12-15) diff --git a/ui/src/app/core/manage-plugins/plugin-bridge/plugin-bridge.component.ts b/ui/src/app/core/manage-plugins/plugin-bridge/plugin-bridge.component.ts index 0289e7fd4..6ce1e534d 100644 --- a/ui/src/app/core/manage-plugins/plugin-bridge/plugin-bridge.component.ts +++ b/ui/src/app/core/manage-plugins/plugin-bridge/plugin-bridge.component.ts @@ -47,6 +47,7 @@ export class PluginBridgeComponent implements OnInit { public showConfigFields: boolean[] = [] public saveInProgress = false public canShowBridgeDebug = false + public deleteBridgeIds: string[] = [] constructor() {} @@ -85,6 +86,9 @@ export class PluginBridgeComponent implements OnInit { async toggleExternalBridge(block: any, enable: boolean, index: number) { if (!enable) { + // Store unpaired child bridge id for deletion, so no bridges are orphaned + this.deleteBridgeIds.push(block._bridge.username) + delete block._bridge return } @@ -102,6 +106,10 @@ export class PluginBridgeComponent implements OnInit { env: bridgeCache?.env, } + if (this.deleteBridgeIds.includes(block._bridge.username)) { + this.deleteBridgeIds = this.deleteBridgeIds.filter(id => id !== block._bridge.username) + } + this.bridgeCache.set(index, block._bridge) await this.getDeviceInfo(block._bridge.username) } @@ -132,6 +140,17 @@ export class PluginBridgeComponent implements OnInit { try { await firstValueFrom(this.$api.post(`/config-editor/plugin/${encodeURIComponent(this.plugin.name)}`, this.configBlocks)) + + // Delete unpaired bridges, so no bridges are orphaned + for (const childBridgeId of this.deleteBridgeIds) { + try { + await firstValueFrom(this.$api.delete(`/server/pairings/${childBridgeId.replace(/:/g, '')}`)) + } catch (error) { + console.error(error) + this.$toastr.error(this.$translate.instant('settings.unpair_bridge.unpair_error'), this.$translate.instant('toast.title_error')) + } + } + this.$activeModal.close() this.$modal.open(RestartHomebridgeComponent, { size: 'lg',