From c2a10fc4e58b9779bf79531352ee7feb289a3f2d Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Thu, 19 Oct 2023 01:15:22 -0400 Subject: [PATCH] fix frontend based on backend changes --- src/data/zwave_js.ts | 18 +++++++----- .../dialog-zwave_js-hard-reset-controller.ts | 29 ++++++++++++++++--- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/data/zwave_js.ts b/src/data/zwave_js.ts index fd5ee1b4c20e..82e8bd2ce570 100644 --- a/src/data/zwave_js.ts +++ b/src/data/zwave_js.ts @@ -757,14 +757,18 @@ export const fetchZwaveNodeFirmwareUpdateCapabilities = ( device_id, }); -export const hardResetController = ( +export const subscribeHardResetControllerDeviceAdded = ( hass: HomeAssistant, - entry_id: string -): Promise => - hass.callWS({ - type: "zwave_js/hard_reset_controller", - entry_id, - }); + entry_id: string, + callbackFunction: (message: { device_id: string }) => void +): Promise => + hass.connection.subscribeMessage( + (message: any) => callbackFunction(message), + { + type: "zwave_js/hard_reset_controller", + entry_id: entry_id, + } + ); export const uploadFirmwareAndBeginUpdate = async ( hass: HomeAssistant, diff --git a/src/panels/config/integrations/integration-panels/zwave_js/dialog-zwave_js-hard-reset-controller.ts b/src/panels/config/integrations/integration-panels/zwave_js/dialog-zwave_js-hard-reset-controller.ts index ea132ad76d3f..b26e5a60c1df 100644 --- a/src/panels/config/integrations/integration-panels/zwave_js/dialog-zwave_js-hard-reset-controller.ts +++ b/src/panels/config/integrations/integration-panels/zwave_js/dialog-zwave_js-hard-reset-controller.ts @@ -2,10 +2,11 @@ import { mdiCheckCircle, mdiDeleteForever, mdiRestore } from "@mdi/js"; import "@material/mwc-button/mwc-button"; import { css, CSSResultGroup, html, LitElement, nothing } from "lit"; import { customElement, property, state } from "lit/decorators"; +import { UnsubscribeFunc } from "home-assistant-js-websocket"; import { fireEvent } from "../../../../../common/dom/fire_event"; import { createCloseHeading } from "../../../../../components/ha-dialog"; import "../../../../../components/ha-svg-icon"; -import { hardResetController } from "../../../../../data/zwave_js"; +import { subscribeHardResetControllerDeviceAdded } from "../../../../../data/zwave_js"; import { haStyleDialog } from "../../../../../resources/styles"; import { HomeAssistant } from "../../../../../types"; import { ZWaveJSHardResetControllerDialogParams } from "./show-dialog-zwave_js-hard-reset-controller"; @@ -32,6 +33,8 @@ class DialogZWaveJSHardResetController extends LitElement { @state() private _resetStatus = ResetStatus.NotStarted; + private _subscribedDeviceAdded?: Promise; + public showDialog(params: ZWaveJSHardResetControllerDialogParams): void { this._entryId = params.entryId; } @@ -39,6 +42,7 @@ class DialogZWaveJSHardResetController extends LitElement { public closeDialog(): void { this._entryId = undefined; this._resetStatus = ResetStatus.NotStarted; + this._unsubscribeDeviceAdded(); fireEvent(this, "dialog-closed", { dialog: this.localName }); } @@ -99,13 +103,30 @@ class DialogZWaveJSHardResetController extends LitElement { confirmText: this.hass.localize("ui.common.continue"), }) ) { + this._subscribedDeviceAdded = subscribeHardResetControllerDeviceAdded( + this.hass, + this._entryId!, + (message: { device_id: string }) => { + setTimeout( + () => navigate(`/config/devices/device/${message.device_id}`), + 0 + ); + this._resetStatus = ResetStatus.Done; + this._unsubscribeDeviceAdded(); + } + ); this._resetStatus = ResetStatus.InProgress; - const deviceId = await hardResetController(this.hass, this._entryId!); - setTimeout(() => navigate(`/config/devices/device/${deviceId}`), 0); - this._resetStatus = ResetStatus.Done; } } + private _unsubscribeDeviceAdded(): void { + if (!this._subscribedDeviceAdded) { + return; + } + this._subscribedDeviceAdded.then((unsub) => unsub()); + this._subscribedDeviceAdded = undefined; + } + static get styles(): CSSResultGroup { return [ haStyleDialog,