Mock history stream for demo (#15886)

* Mock history stream for demo

* Fix type error
This commit is contained in:
Paul Bottein 2023-03-20 20:39:01 +01:00 committed by GitHub
parent 65d3af6fd6
commit d272783258
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,39 +1,19 @@
import { HassEntity } from "home-assistant-js-websocket"; import { HassEntity } from "home-assistant-js-websocket";
import { HistoryStates } from "../../../src/data/history";
import { MockHomeAssistant } from "../../../src/fake_data/provide_hass"; import { MockHomeAssistant } from "../../../src/fake_data/provide_hass";
interface HistoryQueryParams { const generateStateHistory = (
filter_entity_id: string; state: HassEntity,
end_time: string; deltas,
} start_date: Date,
end_date: Date
const parseQuery = <T>(queryString: string) => { ) => {
const query: any = {};
const items = queryString.split("&");
for (const item of items) {
const parts = item.split("=");
const key = decodeURIComponent(parts[0]);
const value = parts.length > 1 ? decodeURIComponent(parts[1]) : undefined;
query[key] = value;
}
return query as T;
};
const getTime = (minutesAgo) => {
const ts = new Date(Date.now() - minutesAgo * 60 * 1000);
return ts.toISOString();
};
const randomTimeAdjustment = (diff) => Math.random() * diff - diff / 2;
const maxTime = 1440;
const generateHistory = (state, deltas) => {
const changes = const changes =
typeof deltas[0] === "object" typeof deltas[0] === "object"
? deltas ? deltas
: deltas.map((st) => ({ state: st })); : deltas.map((st) => ({ state: st }));
const timeDiff = 900 / changes.length; const timeDiff = (end_date.getTime() - start_date.getTime()) / changes.length;
return changes.map((change, index) => { return changes.map((change, index) => {
let attributes; let attributes;
@ -47,17 +27,13 @@ const generateHistory = (state, deltas) => {
attributes = { ...state.attributes, ...change.attributes }; attributes = { ...state.attributes, ...change.attributes };
} }
const time = const time = start_date.getTime() + timeDiff * index;
index === 0
? getTime(maxTime)
: getTime(maxTime - index * timeDiff + randomTimeAdjustment(timeDiff));
return { return {
attributes, a: attributes,
entity_id: state.entity_id, s: change.state || state.state,
state: change.state || state.state, lc: time / 1000,
last_changed: time, lu: time / 1000,
last_updated: time,
}; };
}); });
}; };
@ -65,15 +41,29 @@ const generateHistory = (state, deltas) => {
const incrementalUnits = ["clients", "queries", "ads"]; const incrementalUnits = ["clients", "queries", "ads"];
export const mockHistory = (mockHass: MockHomeAssistant) => { export const mockHistory = (mockHass: MockHomeAssistant) => {
mockHass.mockAPI( mockHass.mockWS(
/history\/period\/.+/, "history/stream",
(hass, _method, path, _parameters) => { (
const params = parseQuery<HistoryQueryParams>(path.split("?")[1]); {
const entities = params.filter_entity_id.split(","); entity_ids,
start_time,
end_time,
}: {
entity_ids: string[];
start_time: string;
end_time?: string;
},
hass,
onChange
) => {
const states: HistoryStates = {};
const results: HassEntity[][] = []; const start = new Date(start_time);
const end = end_time ? new Date(end_time) : new Date();
for (const entityId of entity_ids) {
states[entityId] = [];
for (const entityId of entities) {
const state = hass.states[entityId]; const state = hass.states[entityId];
if (!state) { if (!state) {
@ -81,7 +71,12 @@ export const mockHistory = (mockHass: MockHomeAssistant) => {
} }
if (!state.attributes.unit_of_measurement) { if (!state.attributes.unit_of_measurement) {
results.push(generateHistory(state, [state.state])); states[entityId] = generateStateHistory(
state,
[state.state],
start,
end
);
continue; continue;
} }
@ -120,17 +115,23 @@ export const mockHistory = (mockHass: MockHomeAssistant) => {
numberState - diff + Math.floor(Math.random() * 2 * diff); numberState - diff + Math.floor(Math.random() * 2 * diff);
} }
results.push( states[entityId] = generateStateHistory(
generateHistory( state,
{ Array.from({ length: statesToGenerate }, genFunc),
entity_id: state.entity_id, start,
attributes: state.attributes, end
},
Array.from({ length: statesToGenerate }, genFunc)
)
); );
} }
return results;
setTimeout(() => {
onChange?.({
states,
start_time: start,
end_time: end,
});
}, 1);
return () => {};
} }
); );
}; };