Compare commits

..

9 Commits

Author SHA1 Message Date
J. Nick Koston 7128872bcc Enable compression for development 2023-02-27 16:10:08 -06:00
J. Nick Koston 60f3fc85ae Enable compression on the dev server 2023-02-27 15:49:01 -06:00
J. Nick Koston 3f9df79849 wip 2023-02-27 15:04:46 -06:00
J. Nick Koston 3ce4e67083 wip 2023-02-27 15:04:28 -06:00
J. Nick Koston 77e05af465 wip 2023-02-27 15:04:16 -06:00
J. Nick Koston 97b6fc03b3 wip 2023-02-27 15:00:09 -06:00
J. Nick Koston cf142cee83 wip 2023-02-27 14:58:01 -06:00
J. Nick Koston 6347889fcb wip 2023-02-27 14:54:37 -06:00
J. Nick Koston 7f216699ac wip 2023-02-27 14:30:27 -06:00
2 changed files with 42 additions and 12 deletions
+41 -12
View File
@@ -193,6 +193,12 @@ export interface EnergyPreferencesValidation {
device_consumption: EnergyValidationIssue[][];
}
export interface EnergyInfoAndCO2Signal {
energyInfo: EnergyInfo;
co2SignalEntity: string | undefined;
co2SignalConfigEntry: ConfigEntry | undefined;
}
export const getEnergyInfo = (hass: HomeAssistant) =>
hass.callWS<EnergyInfo>({
type: "energy/info",
@@ -332,13 +338,9 @@ export const getReferencedStatisticIds = (
return statIDs;
};
const getEnergyData = async (
hass: HomeAssistant,
prefs: EnergyPreferences,
start: Date,
end?: Date,
compare?: boolean
): Promise<EnergyData> => {
const getEnergyInfoAndCO2Signal = async (
hass: HomeAssistant
): Promise<EnergyInfoAndCO2Signal> => {
const [configEntries, info] = await Promise.all([
getConfigEntries(hass, { domain: "co2signal" }),
getEnergyInfo(hass),
@@ -366,6 +368,24 @@ const getEnergyData = async (
}
}
return <EnergyInfoAndCO2Signal>{
energyInfo: info,
co2SignalEntity: co2SignalEntity,
co2SignalConfigEntry: co2SignalConfigEntry,
};
};
const getEnergyDataWithInfo = async (
hass: HomeAssistant,
energyInfoAndCO2Signal: EnergyInfoAndCO2Signal,
prefs: EnergyPreferences,
start: Date,
end?: Date,
compare?: boolean
): Promise<EnergyData> => {
const info = energyInfoAndCO2Signal.energyInfo;
const co2SignalEntity = energyInfoAndCO2Signal.co2SignalEntity;
const co2SignalConfigEntry = energyInfoAndCO2Signal.co2SignalConfigEntry;
const consumptionStatIDs: string[] = [];
for (const source of prefs.energy_sources) {
// grid source
@@ -595,6 +615,9 @@ export const getEnergyDataCollection = (
energyCollectionKeys.push(options.key);
let energyInfoAndCO2Signal: EnergyInfoAndCO2Signal | undefined;
let forceRefreshEnergyInfo = false;
const collection = getCollection<EnergyData>(
hass.connection,
key,
@@ -622,14 +645,20 @@ export const getEnergyDataCollection = (
}
nextFetch.setMinutes(20, 0, 0);
collection._refreshTimeout = window.setTimeout(
() => collection.refresh(),
nextFetch.getTime() - Date.now()
);
collection._refreshTimeout = window.setTimeout(() => {
forceRefreshEnergyInfo = true;
collection.refresh();
}, nextFetch.getTime() - Date.now());
}
return getEnergyData(
if (!energyInfoAndCO2Signal || forceRefreshEnergyInfo) {
energyInfoAndCO2Signal = await getEnergyInfoAndCO2Signal(hass);
forceRefreshEnergyInfo = false;
}
return getEnergyDataWithInfo(
hass,
energyInfoAndCO2Signal,
collection.prefs,
collection.start,
collection.end,
+1
View File
@@ -19,6 +19,7 @@ module.exports = {
"**/*.json": "js",
"**/*.css": "js",
},
compress: true,
middleware: [cors()],
plugins: rollupWDSPlugins,
};