From ef31bce5ee4259edc7a2fe0bc9e7bde6fed0cea5 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 16 Mar 2021 13:26:48 -0700 Subject: [PATCH] Allow linking to trace (#8658) --- .../automation/trace/ha-automation-trace.ts | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/panels/config/automation/trace/ha-automation-trace.ts b/src/panels/config/automation/trace/ha-automation-trace.ts index bc0f8bf060..a85196c144 100644 --- a/src/panels/config/automation/trace/ha-automation-trace.ts +++ b/src/panels/config/automation/trace/ha-automation-trace.ts @@ -115,10 +115,22 @@ export class HaAutomationTrace extends LitElement { `; } + protected firstUpdated(changedProps) { + super.firstUpdated(changedProps); + + if (!this.automationId) { + return; + } + + const params = new URLSearchParams(location.search); + this._loadTraces(params.get("run_id") || undefined); + } + protected updated(changedProps) { super.updated(changedProps); - if (changedProps.has("automationId")) { + // Only reset if automationId has changed and we had one before. + if (changedProps.get("automationId")) { this._traces = undefined; this._entityId = undefined; this._runId = undefined; @@ -132,9 +144,8 @@ export class HaAutomationTrace extends LitElement { if (changedProps.has("_runId") && this._runId) { this._trace = undefined; this._logbookEntries = undefined; - if (this._runId) { - this._loadTrace(); - } + this.shadowRoot!.querySelector("select")!.value = this._runId; + this._loadTrace(); } if ( @@ -153,20 +164,36 @@ export class HaAutomationTrace extends LitElement { this._runId = ev.target.value; } - private async _loadTraces() { + private async _loadTraces(runId?: string) { this._traces = await loadAutomationTraces(this.hass, this.automationId); // Newest will be on top. this._traces.reverse(); + if (runId) { + this._runId = runId; + } + // Check if current run ID still exists if ( this._runId && !this._traces.some((trace) => trace.run_id === this._runId) ) { + this._runId = undefined; + + // If we came here from a trace passed into the url, clear it. + if (runId) { + const params = new URLSearchParams(location.search); + params.delete("run_id"); + history.replaceState( + null, + "", + `${location.pathname}?${params.toString()}` + ); + } + await showAlertDialog(this, { text: "Chosen trace is no longer available", }); - this._runId = undefined; } // See if we can set a default runID