Fix history type device class (#14851)

This commit is contained in:
Paul Bottein 2022-12-22 12:59:00 +01:00 committed by GitHub
parent 9be6a47d88
commit 40cf15c1f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 67 deletions

View File

@ -23,63 +23,8 @@ interface CachedResults {
data: HistoryResult;
}
// This is a different interface, a different cache :(
interface RecentCacheResults {
created: number;
language: string;
data: Promise<HistoryResult>;
}
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,

View File

@ -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<HistoryStates>({ ...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,

View File

@ -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,

View File

@ -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