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

View File

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