From 0fbde5024e2b47722f3f8e49e2078509398b6bb3 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Tue, 27 Jan 2026 21:31:05 +0100 Subject: [PATCH] Fix back button in energy panel (#29214) --- src/panels/energy/ha-panel-energy.ts | 4 ++++ .../energy/hui-energy-distribution-card.ts | 17 ++++++++++++++++- src/panels/lovelace/hui-root.ts | 8 +++++++- .../home/home-overview-view-strategy.ts | 4 +++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/panels/energy/ha-panel-energy.ts b/src/panels/energy/ha-panel-energy.ts index 16b9660f59..4236ff308d 100644 --- a/src/panels/energy/ha-panel-energy.ts +++ b/src/panels/energy/ha-panel-energy.ts @@ -115,6 +115,8 @@ class PanelEnergy extends LitElement { @state() private _prefs?: EnergyPreferences; + @state() private _searchParms = new URLSearchParams(window.location.search); + @state() private _error?: string; @@ -248,6 +250,8 @@ class PanelEnergy extends LitElement { .lovelace=${this._lovelace} .route=${this.route} .panel=${this.panel} + .backButton=${this._searchParms.has("historyBack")} + .backPath=${this._searchParms.get("backPath") || "/"} .extraActionItems=${this._extraActionItems} @reload-energy-panel=${this._reloadConfig} class=${classMap({ "has-period-selector": showEnergySelector })} diff --git a/src/panels/lovelace/cards/energy/hui-energy-distribution-card.ts b/src/panels/lovelace/cards/energy/hui-energy-distribution-card.ts index ca1cb0b1cb..ff2cb7d67d 100644 --- a/src/panels/lovelace/cards/energy/hui-energy-distribution-card.ts +++ b/src/panels/lovelace/cards/energy/hui-energy-distribution-card.ts @@ -65,6 +65,17 @@ class HuiEnergyDistrubutionCard ]; } + private get _energyDashboardHref(): string { + const params = new URLSearchParams({ + historyBack: "1", + }); + const backPath = window.location.pathname; + if (backPath) { + params.append("backPath", backPath); + } + return `/energy?${params.toString()}`; + } + public getCardSize(): Promise | number { return 3; } @@ -789,7 +800,11 @@ class HuiEnergyDistrubutionCard ${this._config.link_dashboard ? html`
- + ${this.hass.localize( "ui.panel.lovelace.cards.energy.energy_distribution.go_to_energy_dashboard" )} diff --git a/src/panels/lovelace/hui-root.ts b/src/panels/lovelace/hui-root.ts index aa29e73386..5b9e035c70 100644 --- a/src/panels/lovelace/hui-root.ts +++ b/src/panels/lovelace/hui-root.ts @@ -146,6 +146,10 @@ class HUIRoot extends LitElement { @property({ type: Boolean, attribute: "no-edit" }) public noEdit = false; + @property({ attribute: false }) public backButton = false; + + @property({ attribute: false }) public backPath?: string; + @state() private _curView?: number | "hass-unused-entities"; private _configChangedByUndo = false; @@ -567,7 +571,7 @@ class HUIRoot extends LitElement {
${this._renderActionItems()}
` : html` - ${isSubview + ${isSubview || this.backButton ? html` 1) { goBack(); } else if (!views[0].subview) { diff --git a/src/panels/lovelace/strategies/home/home-overview-view-strategy.ts b/src/panels/lovelace/strategies/home/home-overview-view-strategy.ts index 848bd33433..ed7c2ff2a2 100644 --- a/src/panels/lovelace/strategies/home/home-overview-view-strategy.ts +++ b/src/panels/lovelace/strategies/home/home-overview-view-strategy.ts @@ -305,7 +305,9 @@ export class HomeOverviewViewStrategy extends ReactiveElement { summary: "energy", tap_action: { action: "navigate", - navigation_path: "/energy?historyBack=1", + navigation_path: config.home_panel + ? "/energy?historyBack=1&backPath=/home" + : "/energy?historyBack=1", }, } satisfies HomeSummaryCard), ].filter(Boolean) as LovelaceCardConfig[];