-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Infer a limited history chart from state object #19176
Changes from 4 commits
6ed95a6
6ad768d
3a089c2
1af1b67
4768a20
00ede04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,7 +81,7 @@ export interface EntityHistoryState { | |
/** attributes */ | ||
a: { [key: string]: any }; | ||
/** last_changed; if set, also applies to lu */ | ||
lc: number; | ||
lc?: number; | ||
/** last_updated */ | ||
lu: number; | ||
} | ||
|
@@ -419,17 +419,41 @@ const BLANK_UNIT = " "; | |
export const computeHistory = ( | ||
hass: HomeAssistant, | ||
stateHistory: HistoryStates, | ||
entityIds: string[], | ||
localize: LocalizeFunc, | ||
sensorNumericalDeviceClasses: string[], | ||
splitDeviceClasses = false | ||
): HistoryResult => { | ||
const lineChartDevices: { [unit: string]: HistoryStates } = {}; | ||
const timelineDevices: TimelineEntity[] = []; | ||
if (!stateHistory) { | ||
|
||
const localStateHistory: HistoryStates = {}; | ||
|
||
// Create a limited history from stateObj if entity has no recorded history. | ||
entityIds.forEach((entity) => { | ||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I convinced myself that last_updated would be more correct, in case we were displaying a chart with attributes (like climate). If we use last_changed, then we would render that the attributes were constant all the way back to the time of the last state change, but that would be possibly inaccurate, as the attributes may have been changing after that point. If we cutoff the graph at the time of last attribute change, then we display potentially less data, but guarantee that what is shown is actually correct. |
||
}, | ||
]; | ||
} | ||
}); | ||
Object.keys(stateHistory).forEach((entityId) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we combine |
||
if (!(entityId in localStateHistory)) { | ||
localStateHistory[entityId] = stateHistory[entityId]; | ||
} | ||
}); | ||
|
||
if (!localStateHistory) { | ||
return { line: [], timeline: [] }; | ||
} | ||
Object.keys(stateHistory).forEach((entityId) => { | ||
const stateInfo = stateHistory[entityId]; | ||
Object.keys(localStateHistory).forEach((entityId) => { | ||
const stateInfo = localStateHistory[entityId]; | ||
if (stateInfo.length === 0) { | ||
return; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need
getTime
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Struggling to remember exactly, I think maybe at one time the custom timeline controller didn't handle
Date
correctly, but I might have fixed that at some point. I'll see if it works just as well with removing it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't find any issue, removed it 🤷