diff --git a/src/panels/history/ha-panel-history.ts b/src/panels/history/ha-panel-history.ts index fbed2990a9..56f8aeb77a 100644 --- a/src/panels/history/ha-panel-history.ts +++ b/src/panels/history/ha-panel-history.ts @@ -54,6 +54,7 @@ import { SubscribeMixin } from "../../mixins/subscribe-mixin"; import { haStyle } from "../../resources/styles"; import { HomeAssistant } from "../../types"; import { fileDownload } from "../../util/file_download"; +import { showAlertDialog } from "../../dialogs/generic/show-dialog-box"; class HaPanelHistory extends SubscribeMixin(LitElement) { @property({ attribute: false }) hass!: HomeAssistant; @@ -176,7 +177,7 @@ class HaPanelHistory extends SubscribeMixin(LitElement) { @@ -371,14 +372,12 @@ class HaPanelHistory extends SubscribeMixin(LitElement) { } private async _getStats() { - const entityIds = this._getEntityIds(); - if (!entityIds) { + const statisticIds = this._getEntityIds(); + if (!statisticIds) { + this._statisticsHistory = undefined; return; } - this._getStatistics(entityIds); - } - private async _getStatistics(statisticIds: string[]): Promise { try { const statistics = await fetchStatistics( this.hass!, @@ -433,23 +432,14 @@ class HaPanelHistory extends SubscribeMixin(LitElement) { } private async _getHistory() { - if (!this._targetPickerValue) { - return; - } - this._isLoading = true; const entityIds = this._getEntityIds(); - if (entityIds === undefined) { - this._isLoading = false; + if (!entityIds) { this._stateHistory = undefined; return; } - if (entityIds.length === 0) { - this._isLoading = false; - this._stateHistory = { line: [], timeline: [] }; - return; - } + this._isLoading = true; if (this._subscribed) { this._unsubscribeHistory(); @@ -639,10 +629,20 @@ class HaPanelHistory extends SubscribeMixin(LitElement) { } private _downloadHistory() { + const entities = this._getEntityIds(); + if (!entities || !this._mungedStateHistory) { + showAlertDialog(this, { + title: this.hass.localize("ui.panel.history.download_data_error"), + text: this.hass.localize("ui.panel.history.error_no_data"), + warning: true, + }); + return; + } + const csv: string[] = ["entity_id,state,last_changed\n"]; const formatDate = (number) => new Date(number).toISOString(); - for (const line of this._mungedStateHistory!.line) { + for (const line of this._mungedStateHistory.line) { for (const entity of line.data) { const entityId = entity.entity_id; for (const data of [entity.states, entity.statistics]) { @@ -655,7 +655,7 @@ class HaPanelHistory extends SubscribeMixin(LitElement) { } } } - for (const timeline of this._mungedStateHistory!.timeline) { + for (const timeline of this._mungedStateHistory.timeline) { const entityId = timeline.entity_id; for (const s of timeline.data) { csv.push(`${entityId},${s.state},${formatDate(s.last_changed)}\n`); diff --git a/src/translations/en.json b/src/translations/en.json index 5a887448f7..7cd85fb275 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -6505,7 +6505,9 @@ "start_search": "Start by selecting an area, device or entity above", "add_all": "Add all entities", "remove_all": "Remove all selections", - "download_data": "Download data" + "download_data": "Download data", + "download_data_error": "Unable to download data", + "error_no_data": "You need to select data first." } }, "tips": {