Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions src/panels/lovelace/common/graph/get-history-coordinates.ts

This file was deleted.

60 changes: 54 additions & 6 deletions src/panels/lovelace/header-footer/hui-graph-header-footer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import {
TemplateResult,
} from "lit-element";
import { HomeAssistant } from "../../../types";
import { getHistoryCoordinates } from "../common/graph/get-history-coordinates";
import { HassEntity } from "home-assistant-js-websocket";

import "@polymer/paper-spinner/paper-spinner";
import "../components/hui-graph-base";
import { LovelaceHeaderFooter } from "../types";
import { GraphHeaderFooterConfig } from "./types";
import { hasConfigOrEntityChanged } from "../common/has-changed";
import { fetchRecent } from "../../../data/history";
import { coordinates } from "../common/graph/coordinates";

const MINUTE = 60000;
const DAY = 86400000;

@customElement("hui-graph-header-footer")
export class HuiGraphHeaderFooter extends LitElement
Expand All @@ -33,6 +37,10 @@ export class HuiGraphHeaderFooter extends LitElement

private _date?: Date;

private _stateHistory?: HassEntity[];

private _fetching = false;

public setConfig(config: GraphHeaderFooterConfig): void {
if (!config?.entity || config.entity.split(".")[0] !== "sensor") {
throw new Error(
Expand Down Expand Up @@ -83,31 +91,71 @@ export class HuiGraphHeaderFooter extends LitElement
`;
}

protected firstUpdated(): void {
this._date = new Date();
protected shouldUpdate(changedProps: PropertyValues): boolean {
return hasConfigOrEntityChanged(this, changedProps);
}

protected updated(changedProps: PropertyValues) {
if (!this._config || !this.hass) {
if (
!this._config ||
!this.hass ||
(this._fetching && !changedProps.has("_config"))
) {
return;
}

if (changedProps.has("_config")) {
const oldConfig = changedProps.get("_config") as GraphHeaderFooterConfig;
if (!oldConfig || oldConfig.entity !== this._config.entity) {
this._stateHistory = [];
}

this._getCoordinates();
} else if (Date.now() - this._date!.getTime() >= MINUTE) {
this._getCoordinates();
Comment thread
zsarnett marked this conversation as resolved.
}
}

private async _getCoordinates(): Promise<void> {
this._coordinates = await getHistoryCoordinates(
this._fetching = true;
const endTime = new Date();
const startTime =
!this._date || !this._stateHistory?.length
? new Date(
new Date().setHours(
endTime.getHours() - this._config!.hours_to_show!
)
)
: this._date;

if (this._stateHistory!.length) {
this._stateHistory = this._stateHistory!.filter(
(entity) =>
endTime.getTime() - new Date(entity.last_changed).getTime() <= DAY
);
}

const stateHistory = await fetchRecent(
Comment thread
zsarnett marked this conversation as resolved.
this.hass!,
this._config!.entity,
startTime,
endTime,
Boolean(this._stateHistory!.length)
);

if (stateHistory.length && stateHistory[0].length) {
this._stateHistory!.push(...stateHistory[0]);
}

this._coordinates = coordinates(
this._stateHistory,
this._config!.hours_to_show!,
500,
this._config!.detail!
);

this._date = new Date();
this._date = endTime;
this._fetching = false;
}

static get styles(): CSSResult {
Expand Down