mirror of
https://github.com/home-assistant/frontend.git
synced 2026-04-02 00:27:49 +01:00
Fix trend and sensor graph when no history (#30323)
This commit is contained in:
@@ -464,6 +464,15 @@ export const convertStatisticsToHistory = (
|
||||
return statisticsHistory;
|
||||
};
|
||||
|
||||
export const limitedHistoryFromStateObj = (
|
||||
state: HassEntity
|
||||
): EntityHistoryState[] => [
|
||||
{
|
||||
s: state.state,
|
||||
a: state.attributes,
|
||||
lu: new Date(state.last_updated).getTime() / 1000,
|
||||
},
|
||||
];
|
||||
export const computeHistory = (
|
||||
hass: HomeAssistant,
|
||||
stateHistory: HistoryStates,
|
||||
@@ -484,13 +493,9 @@ export const computeHistory = (
|
||||
if (entity in stateHistory) {
|
||||
localStateHistory[entity] = stateHistory[entity];
|
||||
} else if (hass.states[entity]) {
|
||||
localStateHistory[entity] = [
|
||||
{
|
||||
s: hass.states[entity].state,
|
||||
a: hass.states[entity].attributes,
|
||||
lu: new Date(hass.states[entity].last_updated).getTime() / 1000,
|
||||
},
|
||||
];
|
||||
localStateHistory[entity] = limitedHistoryFromStateObj(
|
||||
hass.states[entity]
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -4,7 +4,10 @@ import { isComponentLoaded } from "../../../common/config/is_component_loaded";
|
||||
import { computeDomain } from "../../../common/entity/compute_domain";
|
||||
import { isNumericFromAttributes } from "../../../common/number/format_number";
|
||||
import "../../../components/ha-spinner";
|
||||
import { subscribeHistoryStatesTimeWindow } from "../../../data/history";
|
||||
import {
|
||||
limitedHistoryFromStateObj,
|
||||
subscribeHistoryStatesTimeWindow,
|
||||
} from "../../../data/history";
|
||||
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
|
||||
import type { HomeAssistant } from "../../../types";
|
||||
import { coordinatesMinimalResponseCompressedState } from "../common/graph/coordinates";
|
||||
@@ -127,6 +130,14 @@ class HuiHistoryChartCardFeature
|
||||
return subscribeHistoryStatesTimeWindow(
|
||||
this.hass!,
|
||||
(historyStates) => {
|
||||
const entityId = this.context!.entity_id!;
|
||||
let history = historyStates[entityId];
|
||||
if (!history?.length) {
|
||||
const stateObj = this.hass!.states[entityId];
|
||||
if (stateObj) {
|
||||
history = limitedHistoryFromStateObj(stateObj);
|
||||
}
|
||||
}
|
||||
// sample to 1 point per hour for low detail or 1 point per 5 pixels for high detail
|
||||
const maxDetails = detail
|
||||
? Math.max(10, this.clientWidth / 5, hourToShow)
|
||||
@@ -134,7 +145,7 @@ class HuiHistoryChartCardFeature
|
||||
const useMean = !detail;
|
||||
const { points, yAxisOrigin } =
|
||||
coordinatesMinimalResponseCompressedState(
|
||||
historyStates[this.context!.entity_id!],
|
||||
history,
|
||||
this.clientWidth,
|
||||
this.clientHeight,
|
||||
maxDetails,
|
||||
|
||||
@@ -7,7 +7,10 @@ import { fireEvent } from "../../../common/dom/fire_event";
|
||||
import { computeDomain } from "../../../common/entity/compute_domain";
|
||||
import "../../../components/ha-spinner";
|
||||
import type { HistoryStates } from "../../../data/history";
|
||||
import { subscribeHistoryStatesTimeWindow } from "../../../data/history";
|
||||
import {
|
||||
limitedHistoryFromStateObj,
|
||||
subscribeHistoryStatesTimeWindow,
|
||||
} from "../../../data/history";
|
||||
import type { HomeAssistant } from "../../../types";
|
||||
import { findEntities } from "../common/find-entities";
|
||||
import { coordinatesMinimalResponseCompressedState } from "../common/graph/coordinates";
|
||||
@@ -165,6 +168,13 @@ export class HuiGraphHeaderFooter
|
||||
return;
|
||||
}
|
||||
this._history = combinedHistory;
|
||||
if (!this._history[this._config.entity]?.length) {
|
||||
const stateObj = this.hass!.states[this._config.entity];
|
||||
if (stateObj) {
|
||||
this._history[this._config.entity] =
|
||||
limitedHistoryFromStateObj(stateObj);
|
||||
}
|
||||
}
|
||||
this._computeCoordinates();
|
||||
},
|
||||
this._config.hours_to_show!,
|
||||
|
||||
Reference in New Issue
Block a user