From 79de8e0f3218a8879516a50c07c392c59335cb7b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 27 May 2020 09:45:39 -0500 Subject: [PATCH] Request less data when making history api calls where possible (#5934) --- src/data/history.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/data/history.ts b/src/data/history.ts index adfe9eba00..ee244a2e7b 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -72,6 +72,7 @@ export const fetchRecent = ( if (significantChangesOnly !== undefined) { url += `&significant_changes_only=${Number(significantChangesOnly)}`; } + url += "&minimal_response"; return hass.callApi("GET", url); }; @@ -83,14 +84,17 @@ export const fetchDate = ( ): Promise => { return hass.callApi( "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) => 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 || + !obj2.attributes || LINE_ATTRIBUTES_TO_KEEP.every( (attr) => obj1.attributes![attr] === obj2.attributes![attr] )); @@ -101,12 +105,20 @@ const processTimelineEntity = ( states: HassEntity[] ): TimelineEntity => { const data: TimelineState[] = []; + const last_element = states.length - 1; for (const state of states) { if (data.length > 0 && state.state === data[data.length - 1].state) { 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({ state_localize: computeStateDisplay(localize, state, language), state: state.state, @@ -198,7 +210,7 @@ export const computeHistory = ( } const stateWithUnit = stateInfo.find( - (state) => "unit_of_measurement" in state.attributes + (state) => state.attributes && "unit_of_measurement" in state.attributes ); let unit: string | undefined;