Correctly handle "hours to show" for footer graph (#8071)

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
Philip Allgaier 2021-01-26 16:15:32 +01:00 committed by GitHub
parent 0dd9b21c2d
commit 9b7d8934da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,7 +21,7 @@ import { LovelaceHeaderFooter, LovelaceHeaderFooterEditor } from "../types";
import { GraphHeaderFooterConfig } from "./types"; import { GraphHeaderFooterConfig } from "./types";
const MINUTE = 60000; const MINUTE = 60000;
const DAY = 86400000; const HOUR = MINUTE * 60;
@customElement("hui-graph-header-footer") @customElement("hui-graph-header-footer")
export class HuiGraphHeaderFooter extends LitElement export class HuiGraphHeaderFooter extends LitElement
@ -162,10 +162,24 @@ export class HuiGraphHeaderFooter extends LitElement
: this._date; : this._date;
if (this._stateHistory!.length) { if (this._stateHistory!.length) {
this._stateHistory = this._stateHistory!.filter( const inHoursToShow: HassEntity[] = [];
(entity) => const outHoursToShow: HassEntity[] = [];
endTime.getTime() - new Date(entity.last_changed).getTime() <= DAY // Split into inside and outside of "hours to show".
this._stateHistory!.forEach((entity) =>
(endTime.getTime() - new Date(entity.last_changed).getTime() <=
this._config!.hours_to_show! * HOUR
? inHoursToShow
: outHoursToShow
).push(entity)
); );
if (outHoursToShow.length) {
// If we have values that are now outside of "hours to show", re-add the last entry. This could e.g. be
// the "initial state" from the history backend. Without it, it would look like there is no history data
// at the start at all in the database = graph would start suddenly instead of on the left side of the card.
inHoursToShow.push(outHoursToShow[outHoursToShow.length - 1]);
}
this._stateHistory = inHoursToShow;
} }
const stateHistory = await fetchRecent( const stateHistory = await fetchRecent(