From 7dfa1b0942834b798f8c8ee930702e920c4877c0 Mon Sep 17 00:00:00 2001 From: Philip Allgaier Date: Wed, 27 Jan 2021 13:13:40 +0100 Subject: [PATCH] Convert state history chart to LitElement + add warning if history is disabled (#7994) Co-authored-by: Ian Richardson Co-authored-by: Bram Kragten --- .github/ISSUE_TEMPLATE/BUG_REPORT.md | 4 +- src/components/ha-sidebar.ts | 5 +- src/components/state-history-charts.js | 111 ----------------------- src/components/state-history-charts.ts | 116 +++++++++++++++++++++++++ src/translations/en.json | 1 + 5 files changed, 123 insertions(+), 114 deletions(-) delete mode 100644 src/components/state-history-charts.js create mode 100644 src/components/state-history-charts.ts diff --git a/.github/ISSUE_TEMPLATE/BUG_REPORT.md b/.github/ISSUE_TEMPLATE/BUG_REPORT.md index d39c915ccd..c42de86e68 100644 --- a/.github/ISSUE_TEMPLATE/BUG_REPORT.md +++ b/.github/ISSUE_TEMPLATE/BUG_REPORT.md @@ -77,8 +77,8 @@ DO NOT DELETE ANY TEXT from this template! Otherwise, your issue may be closed w ## Problem-relevant frontend configuration diff --git a/src/components/ha-sidebar.ts b/src/components/ha-sidebar.ts index 9f90b8da62..ffc5d57090 100644 --- a/src/components/ha-sidebar.ts +++ b/src/components/ha-sidebar.ts @@ -774,7 +774,10 @@ class HaSidebar extends LitElement { font-weight: 400; color: var(--sidebar-menu-button-text-color, --primary-text-color); border-bottom: 1px solid var(--divider-color); - background-color: var(--sidebar-menu-button-background-color, --primary-background-color); + background-color: var( + --sidebar-menu-button-background-color, + --primary-background-color + ); font-size: 20px; align-items: center; padding-left: calc(4px + env(safe-area-inset-left)); diff --git a/src/components/state-history-charts.js b/src/components/state-history-charts.js deleted file mode 100644 index 2f3a2780c2..0000000000 --- a/src/components/state-history-charts.js +++ /dev/null @@ -1,111 +0,0 @@ -import { html } from "@polymer/polymer/lib/utils/html-tag"; -/* eslint-plugin-disable lit */ -import { PolymerElement } from "@polymer/polymer/polymer-element"; -import LocalizeMixin from "../mixins/localize-mixin"; -import "./ha-circular-progress"; -import "./state-history-chart-line"; -import "./state-history-chart-timeline"; - -class StateHistoryCharts extends LocalizeMixin(PolymerElement) { - static get template() { - return html` - - - - - - - - - `; - } - - static get properties() { - return { - hass: Object, - historyData: { - type: Object, - value: null, - }, - names: Object, - - isLoadingData: Boolean, - - endTime: { - type: Object, - }, - - upToNow: Boolean, - noSingle: Boolean, - }; - } - - _computeIsSingleLineChart(data, noSingle) { - return !noSingle && data && data.length === 1; - } - - _computeIsEmpty(isLoadingData, historyData) { - const historyDataEmpty = - !historyData || - !historyData.timeline || - !historyData.line || - (historyData.timeline.length === 0 && historyData.line.length === 0); - return !isLoadingData && historyDataEmpty; - } - - _computeIsLoading(isLoading) { - return isLoading && !this.historyData; - } - - _computeEndTime(endTime, upToNow) { - // We don't really care about the value of historyData, but if it change we want to update - // endTime. - return upToNow ? new Date() : endTime; - } -} -customElements.define("state-history-charts", StateHistoryCharts); diff --git a/src/components/state-history-charts.ts b/src/components/state-history-charts.ts new file mode 100644 index 0000000000..a8faeb80d9 --- /dev/null +++ b/src/components/state-history-charts.ts @@ -0,0 +1,116 @@ +import "./ha-circular-progress"; +import { + css, + CSSResult, + customElement, + html, + LitElement, + property, + TemplateResult, +} from "lit-element"; +import "./state-history-chart-line"; +import "./state-history-chart-timeline"; +import { isComponentLoaded } from "../common/config/is_component_loaded"; +import type { HomeAssistant } from "../types"; +import { HistoryResult } from "../data/history"; + +@customElement("state-history-charts") +class StateHistoryCharts extends LitElement { + @property({ attribute: false }) public hass!: HomeAssistant; + + @property({ attribute: false }) public historyData!: HistoryResult; + + @property({ type: Boolean }) public names = false; + + @property({ attribute: false }) public endTime?: Date; + + @property({ type: Boolean }) public upToNow = false; + + @property({ type: Boolean, attribute: "no-single" }) public noSingle = false; + + @property({ type: Boolean }) public isLoadingData = false; + + protected render(): TemplateResult { + if (!isComponentLoaded(this.hass, "history")) { + return html`
+ ${this.hass.localize("ui.components.history_charts.history_disabled")} +
`; + } + + if (this.isLoadingData && !this.historyData) { + return html`
+ ${this.hass.localize("ui.components.history_charts.loading_history")} +
`; + } + + if (this._isHistoryEmpty()) { + return html`
+ ${this.hass.localize("ui.components.history_charts.no_history_found")} +
`; + } + + const computedEndTime = this.upToNow + ? new Date() + : this.endTime || new Date(); + + return html` + ${this.historyData.timeline.length + ? html` + + ` + : html``} + ${this.historyData.line.map( + (line) => html` + + ` + )} + `; + } + + private _isHistoryEmpty(): boolean { + const historyDataEmpty = + !this.historyData || + !this.historyData.timeline || + !this.historyData.line || + (this.historyData.timeline.length === 0 && + this.historyData.line.length === 0); + return !this.isLoadingData && historyDataEmpty; + } + + static get styles(): CSSResult { + return css` + :host { + display: block; + /* height of single timeline chart = 58px */ + min-height: 58px; + } + .info { + text-align: center; + line-height: 58px; + color: var(--secondary-text-color); + } + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "state-history-charts": StateHistoryCharts; + } +} diff --git a/src/translations/en.json b/src/translations/en.json index d0ec25bbdb..d969ca9c94 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -416,6 +416,7 @@ } }, "history_charts": { + "history_disabled": "History integration disabled", "loading_history": "Loading state history...", "no_history_found": "No state history found." },