diff --git a/src/data/cached-history.ts b/src/data/cached-history.ts index 30fa4956f9..2e4bf825f4 100644 --- a/src/data/cached-history.ts +++ b/src/data/cached-history.ts @@ -23,63 +23,8 @@ interface CachedResults { data: HistoryResult; } -// This is a different interface, a different cache :( -interface RecentCacheResults { - created: number; - language: string; - data: Promise; -} - -const RECENT_THRESHOLD = 60000; // 1 minute -const RECENT_CACHE: { [cacheKey: string]: RecentCacheResults } = {}; const stateHistoryCache: { [cacheKey: string]: CachedResults } = {}; -// Cached type 1 function. Without cache config. -export const getRecent = ( - hass: HomeAssistant, - entityId: string, - startTime: Date, - endTime: Date, - localize: LocalizeFunc, - language: string -) => { - const cacheKey = entityId; - const cache = RECENT_CACHE[cacheKey]; - - if ( - cache && - Date.now() - cache.created < RECENT_THRESHOLD && - cache.language === language - ) { - return cache.data; - } - - const noAttributes = !entityIdHistoryNeedsAttributes(hass, entityId); - const prom = fetchRecentWS( - hass, - entityId, - startTime, - endTime, - false, - undefined, - true, - noAttributes - ).then( - (stateHistory) => computeHistory(hass, stateHistory, localize), - (err) => { - delete RECENT_CACHE[entityId]; - throw err; - } - ); - - RECENT_CACHE[cacheKey] = { - created: Date.now(), - language, - data: prom, - }; - return prom; -}; - // Cache type 2 functionality function getEmptyCache( language: string, @@ -97,7 +42,7 @@ function getEmptyCache( export const getRecentWithCache = ( hass: HomeAssistant, - entityId: string, + entityIds: string[], cacheConfig: CacheConfig, localize: LocalizeFunc, language: string @@ -132,7 +77,9 @@ export const getRecentWithCache = ( } const curCacheProm = cache.prom; - const noAttributes = !entityIdHistoryNeedsAttributes(hass, entityId); + const noAttributes = !entityIds.some((entityId) => + entityIdHistoryNeedsAttributes(hass, entityId) + ); const genProm = async () => { let fetchedHistory: HistoryStates; @@ -142,7 +89,7 @@ export const getRecentWithCache = ( curCacheProm, fetchRecentWS( hass, - entityId, + entityIds, toFetchStartTime, endTime, appendingToCache, diff --git a/src/data/history.ts b/src/data/history.ts index 9940292b72..73688d4057 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -1,4 +1,8 @@ -import { HassEntities, HassEntity } from "home-assistant-js-websocket"; +import { + HassEntities, + HassEntity, + HassEntityAttributeBase, +} from "home-assistant-js-websocket"; import { computeDomain } from "../common/entity/compute_domain"; import { computeStateDisplayFromEntityAttributes } from "../common/entity/compute_state_display"; import { computeStateNameFromEntityAttributes } from "../common/entity/compute_state_name"; @@ -117,7 +121,7 @@ export const fetchRecent = ( export const fetchRecentWS = ( hass: HomeAssistant, - entityId: string, // This may be CSV + entityIds: string[], startTime: Date, endTime: Date, skipInitialState = false, @@ -133,7 +137,7 @@ export const fetchRecentWS = ( include_start_time_state: !skipInitialState, minimal_response: minimalResponse, no_attributes: noAttributes || false, - entity_ids: entityId.split(","), + entity_ids: entityIds, }); export const fetchDate = ( @@ -160,9 +164,9 @@ export const fetchDateWS = ( start_time: startTime.toISOString(), end_time: endTime.toISOString(), minimal_response: true, - no_attributes: !entityIds - .map((entityId) => entityIdHistoryNeedsAttributes(hass, entityId)) - .reduce((cur, next) => cur || next, false), + no_attributes: !entityIds.some((entityId) => + entityIdHistoryNeedsAttributes(hass, entityId) + ), }; if (entityIds.length !== 0) { return hass.callWS({ ...params, entity_ids: entityIds }); @@ -195,13 +199,22 @@ const processTimelineEntity = ( if (data.length > 0 && state.s === data[data.length - 1].state) { continue; } + + const currentAttributes: HassEntityAttributeBase = {}; + if (current_state?.attributes.device_class) { + currentAttributes.device_class = current_state?.attributes.device_class; + } + data.push({ state_localize: computeStateDisplayFromEntityAttributes( localize, language, entities, entityId, - state.a || first.a, + { + ...(state.a || first.a), + ...currentAttributes, + }, state.s ), state: state.s, diff --git a/src/dialogs/more-info/ha-more-info-history.ts b/src/dialogs/more-info/ha-more-info-history.ts index a9f30604f1..73061a09b9 100644 --- a/src/dialogs/more-info/ha-more-info-history.ts +++ b/src/dialogs/more-info/ha-more-info-history.ts @@ -139,7 +139,7 @@ export class MoreInfoHistory extends LitElement { } this._stateHistory = await getRecentWithCache( this.hass!, - this.entityId, + [this.entityId], { cacheKey: `more_info.${this.entityId}`, hoursToShow: 24, diff --git a/src/panels/lovelace/cards/hui-history-graph-card.ts b/src/panels/lovelace/cards/hui-history-graph-card.ts index c18864e4b3..c5bf22d9fb 100644 --- a/src/panels/lovelace/cards/hui-history-graph-card.ts +++ b/src/panels/lovelace/cards/hui-history-graph-card.ts @@ -162,7 +162,7 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard { this._stateHistory = { ...(await getRecentWithCache( this.hass!, - this._cacheConfig!.cacheKey, + this._configEntities!.map((config) => config.entity), this._cacheConfig!, this.hass!.localize, this.hass!.language