diff --git a/src/data/cached-history.ts b/src/data/cached-history.ts index dc7c9a98478a..a75f9fd2f0f4 100644 --- a/src/data/cached-history.ts +++ b/src/data/cached-history.ts @@ -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]; diff --git a/src/data/history.ts b/src/data/history.ts index 368dbcd3dcd2..c81c592637b0 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -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 => { 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) {