Add support for streaming history

This commit is contained in:
J. Nick Koston
2023-01-15 14:33:56 -10:00
parent 1d20d6979e
commit 1ec5172370

View File

@@ -79,6 +79,16 @@ interface EntityHistoryState {
lu: number;
}
interface StateDict<T> {
[Key: string]: T;
}
export interface HistoryStreamMessage {
states: StateDict<EntityHistoryState[]>;
start_time?: number; // Start time of this historical chunk
end_time?: number; // End time of this historical chunk
}
export const entityIdHistoryNeedsAttributes = (
hass: HomeAssistant,
entityId: string
@@ -174,6 +184,29 @@ export const fetchDateWS = (
return hass.callWS<HistoryStates>(params);
};
export const subscribeHistory = (
hass: HomeAssistant,
callbackFunction: (message: HistoryStreamMessage) => void,
startTime: Date,
endTime: Date,
entityIds: string[]
): Promise<() => Promise<void>> => {
// If all specified filters are empty lists, we can return an empty list.
const params = {
type: "history/history_during_period",
start_time: startTime.toISOString(),
end_time: endTime.toISOString(),
minimal_response: true,
no_attributes: !entityIds.some((entityId) =>
entityIdHistoryNeedsAttributes(hass, entityId)
),
};
return hass.connection.subscribeMessage<HistoryStreamMessage>(
(message) => callbackFunction(message),
params
);
};
const equalState = (obj1: LineChartState, obj2: LineChartState) =>
obj1.state === obj2.state &&
// Only compare attributes if both states have an attributes object.