1
mirror of https://github.com/home-assistant/frontend synced 2024-09-28 00:43:28 +02:00

Add error checking to download button (#19596)

Clean up history panel + add better error checking
This commit is contained in:
Paulus Schoutsen 2024-02-01 03:25:52 -05:00 committed by GitHub
parent b6bc88e460
commit 8a410d6c82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 20 deletions

View File

@ -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) {
<ha-icon-button
slot="actionItems"
@click=${this._downloadHistory}
.disabled=${this._isLoading || !this._targetPickerValue}
.disabled=${this._isLoading}
.path=${mdiDownload}
.label=${this.hass.localize("ui.panel.history.download_data")}
></ha-icon-button>
@ -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<void> {
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`);

View File

@ -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": {