mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 03:06:41 +00:00
Fix for energy cards not refreshing hourly (#18854)
This commit is contained in:
parent
b28a4e6b50
commit
4c4fddee94
@ -581,6 +581,28 @@ const clearEnergyCollectionPreferences = (hass: HomeAssistant) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const scheduleHourlyRefresh = (collection: EnergyCollection) => {
|
||||||
|
if (collection._refreshTimeout) {
|
||||||
|
clearTimeout(collection._refreshTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (collection._active && (!collection.end || collection.end > new Date())) {
|
||||||
|
// The stats are created every hour
|
||||||
|
// Schedule a refresh for 20 minutes past the hour
|
||||||
|
// If the end is larger than the current time.
|
||||||
|
const nextFetch = new Date();
|
||||||
|
if (nextFetch.getMinutes() >= 20) {
|
||||||
|
nextFetch.setHours(nextFetch.getHours() + 1);
|
||||||
|
}
|
||||||
|
nextFetch.setMinutes(20, 0, 0);
|
||||||
|
|
||||||
|
collection._refreshTimeout = window.setTimeout(
|
||||||
|
() => collection.refresh(),
|
||||||
|
nextFetch.getTime() - Date.now()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const getEnergyDataCollection = (
|
export const getEnergyDataCollection = (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
options: { prefs?: EnergyPreferences; key?: string } = {}
|
options: { prefs?: EnergyPreferences; key?: string } = {}
|
||||||
@ -609,28 +631,7 @@ export const getEnergyDataCollection = (
|
|||||||
collection.prefs = await getEnergyPreferences(hass);
|
collection.prefs = await getEnergyPreferences(hass);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (collection._refreshTimeout) {
|
scheduleHourlyRefresh(collection);
|
||||||
clearTimeout(collection._refreshTimeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
collection._active &&
|
|
||||||
(!collection.end || collection.end > new Date())
|
|
||||||
) {
|
|
||||||
// The stats are created every hour
|
|
||||||
// Schedule a refresh for 20 minutes past the hour
|
|
||||||
// If the end is larger than the current time.
|
|
||||||
const nextFetch = new Date();
|
|
||||||
if (nextFetch.getMinutes() >= 20) {
|
|
||||||
nextFetch.setHours(nextFetch.getHours() + 1);
|
|
||||||
}
|
|
||||||
nextFetch.setMinutes(20, 0, 0);
|
|
||||||
|
|
||||||
collection._refreshTimeout = window.setTimeout(
|
|
||||||
() => collection.refresh(),
|
|
||||||
nextFetch.getTime() - Date.now()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return getEnergyData(
|
return getEnergyData(
|
||||||
hass,
|
hass,
|
||||||
@ -647,6 +648,11 @@ export const getEnergyDataCollection = (
|
|||||||
collection.subscribe = (subscriber: (data: EnergyData) => void) => {
|
collection.subscribe = (subscriber: (data: EnergyData) => void) => {
|
||||||
const unsub = origSubscribe(subscriber);
|
const unsub = origSubscribe(subscriber);
|
||||||
collection._active++;
|
collection._active++;
|
||||||
|
|
||||||
|
if (collection._refreshTimeout === undefined) {
|
||||||
|
scheduleHourlyRefresh(collection);
|
||||||
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
collection._active--;
|
collection._active--;
|
||||||
if (collection._active < 1) {
|
if (collection._active < 1) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user