mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Fix history type device class (#14851)
This commit is contained in:
parent
9be6a47d88
commit
40cf15c1f3
@ -23,63 +23,8 @@ interface CachedResults {
|
|||||||
data: HistoryResult;
|
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 } = {};
|
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
|
// Cache type 2 functionality
|
||||||
function getEmptyCache(
|
function getEmptyCache(
|
||||||
language: string,
|
language: string,
|
||||||
@ -97,7 +42,7 @@ function getEmptyCache(
|
|||||||
|
|
||||||
export const getRecentWithCache = (
|
export const getRecentWithCache = (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entityId: string,
|
entityIds: string[],
|
||||||
cacheConfig: CacheConfig,
|
cacheConfig: CacheConfig,
|
||||||
localize: LocalizeFunc,
|
localize: LocalizeFunc,
|
||||||
language: string
|
language: string
|
||||||
@ -132,7 +77,9 @@ export const getRecentWithCache = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const curCacheProm = cache.prom;
|
const curCacheProm = cache.prom;
|
||||||
const noAttributes = !entityIdHistoryNeedsAttributes(hass, entityId);
|
const noAttributes = !entityIds.some((entityId) =>
|
||||||
|
entityIdHistoryNeedsAttributes(hass, entityId)
|
||||||
|
);
|
||||||
|
|
||||||
const genProm = async () => {
|
const genProm = async () => {
|
||||||
let fetchedHistory: HistoryStates;
|
let fetchedHistory: HistoryStates;
|
||||||
@ -142,7 +89,7 @@ export const getRecentWithCache = (
|
|||||||
curCacheProm,
|
curCacheProm,
|
||||||
fetchRecentWS(
|
fetchRecentWS(
|
||||||
hass,
|
hass,
|
||||||
entityId,
|
entityIds,
|
||||||
toFetchStartTime,
|
toFetchStartTime,
|
||||||
endTime,
|
endTime,
|
||||||
appendingToCache,
|
appendingToCache,
|
||||||
|
@ -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 { computeDomain } from "../common/entity/compute_domain";
|
||||||
import { computeStateDisplayFromEntityAttributes } from "../common/entity/compute_state_display";
|
import { computeStateDisplayFromEntityAttributes } from "../common/entity/compute_state_display";
|
||||||
import { computeStateNameFromEntityAttributes } from "../common/entity/compute_state_name";
|
import { computeStateNameFromEntityAttributes } from "../common/entity/compute_state_name";
|
||||||
@ -117,7 +121,7 @@ export const fetchRecent = (
|
|||||||
|
|
||||||
export const fetchRecentWS = (
|
export const fetchRecentWS = (
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entityId: string, // This may be CSV
|
entityIds: string[],
|
||||||
startTime: Date,
|
startTime: Date,
|
||||||
endTime: Date,
|
endTime: Date,
|
||||||
skipInitialState = false,
|
skipInitialState = false,
|
||||||
@ -133,7 +137,7 @@ export const fetchRecentWS = (
|
|||||||
include_start_time_state: !skipInitialState,
|
include_start_time_state: !skipInitialState,
|
||||||
minimal_response: minimalResponse,
|
minimal_response: minimalResponse,
|
||||||
no_attributes: noAttributes || false,
|
no_attributes: noAttributes || false,
|
||||||
entity_ids: entityId.split(","),
|
entity_ids: entityIds,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const fetchDate = (
|
export const fetchDate = (
|
||||||
@ -160,9 +164,9 @@ export const fetchDateWS = (
|
|||||||
start_time: startTime.toISOString(),
|
start_time: startTime.toISOString(),
|
||||||
end_time: endTime.toISOString(),
|
end_time: endTime.toISOString(),
|
||||||
minimal_response: true,
|
minimal_response: true,
|
||||||
no_attributes: !entityIds
|
no_attributes: !entityIds.some((entityId) =>
|
||||||
.map((entityId) => entityIdHistoryNeedsAttributes(hass, entityId))
|
entityIdHistoryNeedsAttributes(hass, entityId)
|
||||||
.reduce((cur, next) => cur || next, false),
|
),
|
||||||
};
|
};
|
||||||
if (entityIds.length !== 0) {
|
if (entityIds.length !== 0) {
|
||||||
return hass.callWS<HistoryStates>({ ...params, entity_ids: entityIds });
|
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) {
|
if (data.length > 0 && state.s === data[data.length - 1].state) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const currentAttributes: HassEntityAttributeBase = {};
|
||||||
|
if (current_state?.attributes.device_class) {
|
||||||
|
currentAttributes.device_class = current_state?.attributes.device_class;
|
||||||
|
}
|
||||||
|
|
||||||
data.push({
|
data.push({
|
||||||
state_localize: computeStateDisplayFromEntityAttributes(
|
state_localize: computeStateDisplayFromEntityAttributes(
|
||||||
localize,
|
localize,
|
||||||
language,
|
language,
|
||||||
entities,
|
entities,
|
||||||
entityId,
|
entityId,
|
||||||
state.a || first.a,
|
{
|
||||||
|
...(state.a || first.a),
|
||||||
|
...currentAttributes,
|
||||||
|
},
|
||||||
state.s
|
state.s
|
||||||
),
|
),
|
||||||
state: state.s,
|
state: state.s,
|
||||||
|
@ -139,7 +139,7 @@ export class MoreInfoHistory extends LitElement {
|
|||||||
}
|
}
|
||||||
this._stateHistory = await getRecentWithCache(
|
this._stateHistory = await getRecentWithCache(
|
||||||
this.hass!,
|
this.hass!,
|
||||||
this.entityId,
|
[this.entityId],
|
||||||
{
|
{
|
||||||
cacheKey: `more_info.${this.entityId}`,
|
cacheKey: `more_info.${this.entityId}`,
|
||||||
hoursToShow: 24,
|
hoursToShow: 24,
|
||||||
|
@ -162,7 +162,7 @@ export class HuiHistoryGraphCard extends LitElement implements LovelaceCard {
|
|||||||
this._stateHistory = {
|
this._stateHistory = {
|
||||||
...(await getRecentWithCache(
|
...(await getRecentWithCache(
|
||||||
this.hass!,
|
this.hass!,
|
||||||
this._cacheConfig!.cacheKey,
|
this._configEntities!.map((config) => config.entity),
|
||||||
this._cacheConfig!,
|
this._cacheConfig!,
|
||||||
this.hass!.localize,
|
this.hass!.localize,
|
||||||
this.hass!.language
|
this.hass!.language
|
||||||
|
Loading…
x
Reference in New Issue
Block a user