Compare commits

...

8 Commits

Author SHA1 Message Date
Zack Arnett
1cd2aa891d Throttle? 2020-09-10 19:59:34 -05:00
Zack Arnett
d40acc227c comment 2020-09-10 16:37:54 -05:00
Zack Arnett
b802cf9274 Is this the best way 2020-09-09 19:19:11 -05:00
Zack Arnett
72e6c729c9 only get log book data from last time 2020-09-09 16:08:26 -05:00
Zack Arnett
cce5eb8dcf Merge branch 'dev' of https://github.com/home-assistant/frontend into more-info-light 2020-09-09 15:38:31 -05:00
Zack Barett
79d806c924 Update src/dialogs/more-info/controls/more-info-light.ts 2020-09-08 17:18:44 -05:00
Zack Arnett
9bcedcb4c5 Remove added code that is not needed 2020-09-08 08:58:59 -05:00
Zack Arnett
c934ab8d37 Just add Log book to minute refresh 2020-09-08 08:55:47 -05:00
2 changed files with 109 additions and 12 deletions

View File

@@ -0,0 +1,53 @@
export const throttle = (callback: Function, wait: number) => {
let isCalled = false;
return function (...args: any) {
if (!isCalled) {
callback(...args);
isCalled = true;
setTimeout(() => {
isCalled = false;
}, wait);
}
};
};
export const throttleAndQueue = (
callback: Function,
wait: number,
delay?: number
) => {
let isCalled = false;
let timer: number | undefined;
let delaying = false;
const processQueue = () => {
if (isCalled) {
callback();
}
clearInterval(timer);
timer = undefined;
isCalled = false;
};
const setUpThrottle = () => {
delaying = false;
processQueue(); // start immediately on the first invocation
timer = window.setInterval(processQueue, wait);
};
return () => {
isCalled = true;
if (timer !== undefined) {
return;
}
if (delay === undefined) {
setUpThrottle();
} else if (!delaying) {
delaying = true;
setTimeout(setUpThrottle, delay);
}
};
};

View File

@@ -18,6 +18,7 @@ import { getLogbookData, LogbookEntry } from "../../data/logbook";
import "../../panels/logbook/ha-logbook"; import "../../panels/logbook/ha-logbook";
import { haStyle } from "../../resources/styles"; import { haStyle } from "../../resources/styles";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
import { throttleAndQueue } from "../../common/util/throttle";
@customElement("ha-more-info-history") @customElement("ha-more-info-history")
export class MoreInfoHistory extends LitElement { export class MoreInfoHistory extends LitElement {
@@ -33,6 +34,10 @@ export class MoreInfoHistory extends LitElement {
private _historyRefreshInterval?: number; private _historyRefreshInterval?: number;
private _lastLogbookDate?: Date;
private _throttleHistoryFunction?: Function;
protected render(): TemplateResult { protected render(): TemplateResult {
if (!this.entityId) { if (!this.entityId) {
return html``; return html``;
@@ -43,7 +48,8 @@ export class MoreInfoHistory extends LitElement {
return html``; return html``;
} }
return html`<state-history-charts return html`
<state-history-charts
up-to-now up-to-now
.hass=${this.hass} .hass=${this.hass}
.historyData=${this._stateHistory} .historyData=${this._stateHistory}
@@ -70,9 +76,12 @@ export class MoreInfoHistory extends LitElement {
.userIdToName=${this._persons} .userIdToName=${this._persons}
></ha-logbook> ></ha-logbook>
` `
: html`<div class="no-entries"> : html`
${this.hass.localize("ui.components.logbook.entries_not_found")} <div class="no-entries">
</div>`}`; ${this.hass.localize("ui.components.logbook.entries_not_found")}
</div>
`}
`;
} }
protected firstUpdated(): void { protected firstUpdated(): void {
@@ -83,19 +92,43 @@ export class MoreInfoHistory extends LitElement {
super.updated(changedProps); super.updated(changedProps);
if (!this.entityId) { if (!this.entityId) {
clearInterval(this._historyRefreshInterval); clearInterval(this._historyRefreshInterval);
this._stateHistory = undefined;
this._entries = undefined;
this._lastLogbookDate = undefined;
this._throttleHistoryFunction = undefined;
return;
} }
if (changedProps.has("entityId")) { if (changedProps.has("entityId")) {
this._stateHistory = undefined; this._stateHistory = undefined;
this._entries = undefined; this._entries = undefined;
this._lastLogbookDate = undefined;
this._throttleHistoryFunction = undefined;
this._getStateHistory(); this._getStateHistory();
this._getLogBookData(); this._getLogBookData();
clearInterval(this._historyRefreshInterval); this._throttleHistoryFunction = throttleAndQueue(
this._historyRefreshInterval = window.setInterval(() => { () => {
this._getStateHistory(); this._getStateHistory();
}, 60 * 1000); this._getLogBookData();
},
10000,
5000
);
return;
}
if (!changedProps.has("hass")) {
return;
}
const stateObj = this.hass.states[this.entityId];
const oldHass = changedProps.get("hass") as HomeAssistant;
const oldStateObj = oldHass.states[this.entityId];
if (stateObj !== oldStateObj && this._throttleHistoryFunction) {
this._throttleHistoryFunction();
} }
} }
@@ -104,7 +137,7 @@ export class MoreInfoHistory extends LitElement {
this.hass!, this.hass!,
this.entityId, this.entityId,
{ {
refresh: 60, refresh: 10,
cacheKey: `more_info.${this.entityId}`, cacheKey: `more_info.${this.entityId}`,
hoursToShow: 24, hoursToShow: 24,
}, },
@@ -114,15 +147,26 @@ export class MoreInfoHistory extends LitElement {
} }
private async _getLogBookData() { private async _getLogBookData() {
const yesterday = new Date(new Date().getTime() - 24 * 60 * 60 * 1000); const lastDate =
this._lastLogbookDate ||
new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
const now = new Date(); const now = new Date();
this._entries = await getLogbookData(
const newEntries = await getLogbookData(
this.hass, this.hass,
yesterday.toISOString(), lastDate.toISOString(),
now.toISOString(), now.toISOString(),
this.entityId, this.entityId,
true true
); );
if (this._entries) {
this._entries = [...newEntries, ...this._entries];
} else {
this._entries = newEntries;
}
this._lastLogbookDate = now;
} }
private _fetchPersonNames() { private _fetchPersonNames() {