Fetch history with no_attributes for entities that do not need them (#12082)

This commit is contained in:
J. Nick Koston 2022-03-20 15:47:13 -10:00 committed by GitHub
parent 9c1d1cb6f6
commit ddf1cc0733
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 12 deletions

View File

@ -7,6 +7,7 @@ import {
HistoryResult,
LineChartUnit,
TimelineEntity,
entityIdHistoryNeedsAttributes,
} from "./history";
export interface CacheConfig {
@ -53,7 +54,17 @@ export const getRecent = (
return cache.data;
}
const prom = fetchRecent(hass, entityId, startTime, endTime).then(
const noAttributes = !entityIdHistoryNeedsAttributes(hass, entityId);
const prom = fetchRecent(
hass,
entityId,
startTime,
endTime,
false,
undefined,
true,
noAttributes
).then(
(stateHistory) => computeHistory(hass, stateHistory, localize),
(err) => {
delete RECENT_CACHE[entityId];
@ -120,6 +131,7 @@ export const getRecentWithCache = (
}
const curCacheProm = cache.prom;
const noAttributes = !entityIdHistoryNeedsAttributes(hass, entityId);
const genProm = async () => {
let fetchedHistory: HassEntity[][];
@ -132,7 +144,10 @@ export const getRecentWithCache = (
entityId,
toFetchStartTime,
endTime,
appendingToCache
appendingToCache,
undefined,
true,
noAttributes
),
]);
fetchedHistory = results[1];

View File

@ -1,4 +1,5 @@
import { HassEntity } from "home-assistant-js-websocket";
import { computeDomain } from "../common/entity/compute_domain";
import { computeStateDisplay } from "../common/entity/compute_state_display";
import { computeStateDomain } from "../common/entity/compute_state_domain";
import { computeStateName } from "../common/entity/compute_state_name";
@ -7,6 +8,13 @@ import { HomeAssistant } from "../types";
import { FrontendLocaleData } from "./translation";
const DOMAINS_USE_LAST_UPDATED = ["climate", "humidifier", "water_heater"];
const NEED_ATTRIBUTE_DOMAINS = [
"climate",
"humidifier",
"input_datetime",
"thermostat",
"water_heater",
];
const LINE_ATTRIBUTES_TO_KEEP = [
"temperature",
"current_temperature",
@ -131,6 +139,13 @@ export interface StatisticsValidationResults {
[statisticId: string]: StatisticsValidationResult[];
}
export const entityIdHistoryNeedsAttributes = (
hass: HomeAssistant,
entityId: string
) =>
!hass.states[entityId] ||
NEED_ATTRIBUTE_DOMAINS.includes(computeDomain(entityId));
export const fetchRecent = (
hass: HomeAssistant,
entityId: string,
@ -138,7 +153,8 @@ export const fetchRecent = (
endTime: Date,
skipInitialState = false,
significantChangesOnly?: boolean,
minimalResponse = true
minimalResponse = true,
noAttributes?: boolean
): Promise<HassEntity[][]> => {
let url = "history/period";
if (startTime) {
@ -157,7 +173,9 @@ export const fetchRecent = (
if (minimalResponse) {
url += "&minimal_response";
}
if (noAttributes) {
url += "&no_attributes";
}
return hass.callApi("GET", url);
};
@ -171,6 +189,10 @@ export const fetchDate = (
"GET",
`history/period/${startTime.toISOString()}?end_time=${endTime.toISOString()}&minimal_response${
entityId ? `&filter_entity_id=${entityId}` : ``
}${
entityId && !entityIdHistoryNeedsAttributes(hass, entityId)
? `&no_attributes`
: ``
}`
);
@ -278,6 +300,10 @@ const processLineChartEntities = (
};
};
const stateUsesUnits = (state: HassEntity) =>
"unit_of_measurement" in state.attributes ||
"state_class" in state.attributes;
export const computeHistory = (
hass: HomeAssistant,
stateHistory: HassEntity[][],
@ -294,16 +320,18 @@ export const computeHistory = (
return;
}
const stateWithUnitorStateClass = stateInfo.find(
(state) =>
state.attributes &&
("unit_of_measurement" in state.attributes ||
"state_class" in state.attributes)
);
const entityId = stateInfo[0].entity_id;
const currentState =
entityId in hass.states ? hass.states[entityId] : undefined;
const stateWithUnitorStateClass =
!currentState &&
stateInfo.find((state) => state.attributes && stateUsesUnits(state));
let unit: string | undefined;
if (stateWithUnitorStateClass) {
if (currentState && stateUsesUnits(currentState)) {
unit = currentState.attributes.unit_of_measurement || " ";
} else if (stateWithUnitorStateClass) {
unit = stateWithUnitorStateClass.attributes.unit_of_measurement || " ";
} else {
unit = {
@ -313,7 +341,7 @@ export const computeHistory = (
input_number: "#",
number: "#",
water_heater: hass.config.unit_system.temperature,
}[computeStateDomain(stateInfo[0])];
}[computeDomain(entityId)];
}
if (!unit) {