Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZwaveJS: Resume adding a device if the page is refreshed #22519

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,11 @@ class DialogZWaveJSAddNode extends LitElement {
undefined,
undefined,
dsk
);
).catch((err) => {
this._error = err.message;
this._status = "failed";
return () => {};
});
this._addNodeTimeoutHandle = window.setTimeout(() => {
this._unsubscribe();
this._status = "timed_out";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "@material/mwc-button/mwc-button";
import { mdiCheckCircle, mdiCloseCircle } from "@mdi/js";
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 "../../../../../components/ha-circular-progress";
import { createCloseHeading } from "../../../../../components/ha-dialog";
Expand All @@ -25,9 +26,13 @@ class DialogZWaveJSRemoveNode extends LitElement {

@state() private _node?: ZWaveJSRemovedNode;

@state() private _removedCallback?: () => void;

private _removeNodeTimeoutHandle?: number;

private _subscribed?: Promise<() => Promise<void>>;
private _subscribed?: Promise<UnsubscribeFunc>;

@state() private _error?: string;

public disconnectedCallback(): void {
super.disconnectedCallback();
Expand All @@ -38,6 +43,10 @@ class DialogZWaveJSRemoveNode extends LitElement {
params: ZWaveJSRemoveNodeDialogParams
): Promise<void> {
this.entry_id = params.entry_id;
this._removedCallback = params.removedCallback;
if (params.skipConfirmation) {
this._startExclusion();
}
}

protected render() {
Expand Down Expand Up @@ -107,6 +116,11 @@ class DialogZWaveJSRemoveNode extends LitElement {
"ui.panel.config.zwave_js.remove_node.exclusion_failed"
)}
</p>
${this._error
? html`<ha-alert alert-type="error">
${this._error}
</ha-alert>`
: ""}
MindFreeze marked this conversation as resolved.
Show resolved Hide resolved
</div>
</div>
<mwc-button slot="primaryAction" @click=${this.closeDialog}>
Expand Down Expand Up @@ -143,13 +157,17 @@ class DialogZWaveJSRemoveNode extends LitElement {
if (!this.hass) {
return;
}
this._subscribed = this.hass.connection.subscribeMessage(
(message) => this._handleMessage(message),
{
this._subscribed = this.hass.connection
.subscribeMessage((message) => this._handleMessage(message), {
type: "zwave_js/remove_node",
entry_id: this.entry_id,
}
);
})
.catch((err) => {
this._status = "failed";
this._error = err.message;
return () => {};
});
this._status = "started";
this._removeNodeTimeoutHandle = window.setTimeout(
() => this._unsubscribe(),
120000
Expand All @@ -174,6 +192,9 @@ class DialogZWaveJSRemoveNode extends LitElement {
this._status = "finished";
this._node = message.node;
this._unsubscribe();
if (this._removedCallback) {
this._removedCallback();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { fireEvent } from "../../../../../common/dom/fire_event";

export interface ZWaveJSRemoveNodeDialogParams {
entry_id: string;
skipConfirmation?: boolean;
removedCallback?: () => void;
}

export const loadRemoveNodeDialog = () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ import {
fetchZwaveProvisioningEntries,
InclusionState,
setZwaveDataCollectionPreference,
stopZwaveExclusion,
stopZwaveInclusion,
subscribeZwaveControllerStatistics,
ZWaveJSClient,
ZWaveJSControllerStatisticsUpdatedMessage,
Expand Down Expand Up @@ -83,7 +81,17 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) {

protected firstUpdated() {
if (this.hass) {
this._fetchData();
this._fetchData().then(() => {
if (this._status === "connected") {
const inclusion_state = this._network?.controller.inclusion_state;
// show dialog if inclusion/exclusion is already in progress
if (inclusion_state === InclusionState.Including) {
this._addNodeClicked();
} else if (inclusion_state === InclusionState.Excluding) {
this._removeNodeClicked();
}
}
});
MindFreeze marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -126,31 +134,6 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) {
.path=${mdiRefresh}
.label=${this.hass!.localize("ui.common.refresh")}
></ha-icon-button>
${this._network &&
this._status === "connected" &&
(this._network?.controller.inclusion_state ===
InclusionState.Including ||
this._network?.controller.inclusion_state ===
InclusionState.Excluding)
? html`
<ha-alert alert-type="info">
${this.hass.localize(
`ui.panel.config.zwave_js.common.in_progress_inclusion_exclusion`
)}
<mwc-button
slot="action"
.label=${this.hass.localize(
`ui.panel.config.zwave_js.common.cancel_inclusion_exclusion`
)}
@click=${this._network?.controller.inclusion_state ===
InclusionState.Including
? this._cancelInclusion
: this._cancelExclusion}
>
</mwc-button>
</ha-alert>
`
: ""}
${this._network
? html`
<ha-card class="content network-status">
Expand Down Expand Up @@ -593,6 +576,9 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) {
private async _removeNodeClicked() {
showZWaveJSRemoveNodeDialog(this, {
entry_id: this.configEntryId!,
skipConfirmation:
this._network?.controller.inclusion_state === InclusionState.Excluding,
removedCallback: () => this._fetchData(),
});
}

Expand All @@ -602,16 +588,6 @@ class ZWaveJSConfigDashboard extends SubscribeMixin(LitElement) {
});
}

private async _cancelInclusion() {
stopZwaveInclusion(this.hass!, this.configEntryId!);
await this._fetchData();
}

private async _cancelExclusion() {
stopZwaveExclusion(this.hass!, this.configEntryId!);
await this._fetchData();
}

private _dataCollectionToggled(ev) {
setZwaveDataCollectionPreference(
this.hass!,
Expand Down
Loading