Request less data when making history api calls where possible (#5934)

This commit is contained in:
J. Nick Koston 2020-05-27 09:45:39 -05:00 committed by GitHub
parent 70f59eeec6
commit 79de8e0f32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -72,6 +72,7 @@ export const fetchRecent = (
if (significantChangesOnly !== undefined) { if (significantChangesOnly !== undefined) {
url += `&significant_changes_only=${Number(significantChangesOnly)}`; url += `&significant_changes_only=${Number(significantChangesOnly)}`;
} }
url += "&minimal_response";
return hass.callApi("GET", url); return hass.callApi("GET", url);
}; };
@ -83,14 +84,17 @@ export const fetchDate = (
): Promise<HassEntity[][]> => { ): Promise<HassEntity[][]> => {
return hass.callApi( return hass.callApi(
"GET", "GET",
`history/period/${startTime.toISOString()}?end_time=${endTime.toISOString()}` `history/period/${startTime.toISOString()}?end_time=${endTime.toISOString()}&minimal_response`
); );
}; };
const equalState = (obj1: LineChartState, obj2: LineChartState) => const equalState = (obj1: LineChartState, obj2: LineChartState) =>
obj1.state === obj2.state && obj1.state === obj2.state &&
// They either both have an attributes object or not // Only compare attributes if both states have an attributes object.
// When `minimal_response` is sent, only the first and last state
// will have attributes except for domains in DOMAINS_USE_LAST_UPDATED.
(!obj1.attributes || (!obj1.attributes ||
!obj2.attributes ||
LINE_ATTRIBUTES_TO_KEEP.every( LINE_ATTRIBUTES_TO_KEEP.every(
(attr) => obj1.attributes![attr] === obj2.attributes![attr] (attr) => obj1.attributes![attr] === obj2.attributes![attr]
)); ));
@ -101,12 +105,20 @@ const processTimelineEntity = (
states: HassEntity[] states: HassEntity[]
): TimelineEntity => { ): TimelineEntity => {
const data: TimelineState[] = []; const data: TimelineState[] = [];
const last_element = states.length - 1;
for (const state of states) { for (const state of states) {
if (data.length > 0 && state.state === data[data.length - 1].state) { if (data.length > 0 && state.state === data[data.length - 1].state) {
continue; continue;
} }
// Copy the data from the last element as its the newest
// and is only needed to localize the data
if (!state.entity_id) {
state.attributes = states[last_element].attributes;
state.entity_id = states[last_element].entity_id;
}
data.push({ data.push({
state_localize: computeStateDisplay(localize, state, language), state_localize: computeStateDisplay(localize, state, language),
state: state.state, state: state.state,
@ -198,7 +210,7 @@ export const computeHistory = (
} }
const stateWithUnit = stateInfo.find( const stateWithUnit = stateInfo.find(
(state) => "unit_of_measurement" in state.attributes (state) => state.attributes && "unit_of_measurement" in state.attributes
); );
let unit: string | undefined; let unit: string | undefined;