From e51e3e79d51b93068c4e3e1c85f7751e80c84a6e Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Mon, 2 May 2022 19:16:32 +0200 Subject: [PATCH] Add repeat to trace timeline (#12547) --- src/components/trace/hat-trace-timeline.ts | 64 +++++++++++++++++++--- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/src/components/trace/hat-trace-timeline.ts b/src/components/trace/hat-trace-timeline.ts index 291f188691..62895b3976 100644 --- a/src/components/trace/hat-trace-timeline.ts +++ b/src/components/trace/hat-trace-timeline.ts @@ -25,9 +25,11 @@ import { ChooseAction, ChooseActionChoice, getActionType, + RepeatAction, } from "../../data/script"; import { describeAction } from "../../data/script_i18n"; import { + ActionTraceStep, AutomationTraceExtended, ChooseActionTraceStep, getDataFromPath, @@ -105,7 +107,7 @@ class LogbookRenderer { } get hasNext() { - return this.curIndex !== this.logbookEntries.length; + return this.curIndex < this.logbookEntries.length; } maybeRenderItem() { @@ -201,7 +203,7 @@ class ActionRenderer { } get hasNext() { - return this.curIndex !== this.keys.length; + return this.curIndex < this.keys.length; } renderItem() { @@ -214,15 +216,31 @@ class ActionRenderer { private _renderItem( index: number, - actionType?: ReturnType + actionType?: ReturnType, + renderAllIterations?: boolean ): number { const value = this._getItem(index); - if (isTriggerPath(value[0].path)) { - return this._handleTrigger(index, value[0] as TriggerTraceStep); + if (renderAllIterations) { + let i; + value.forEach((item) => { + i = this._renderIteration(index, item, actionType); + }); + return i; + } + return this._renderIteration(index, value[0], actionType); + } + + private _renderIteration( + index: number, + value: ActionTraceStep, + actionType?: ReturnType + ) { + if (isTriggerPath(value.path)) { + return this._handleTrigger(index, value as TriggerTraceStep); } - const timestamp = new Date(value[0].timestamp); + const timestamp = new Date(value.timestamp); // Render all logbook items that are in front of this item. while ( @@ -235,7 +253,7 @@ class ActionRenderer { this.logbookRenderer.flush(); this.timeTracker.maybeRenderTime(timestamp); - const path = value[0].path; + const path = value.path; let data; try { data = getDataFromPath(this.trace.config, path); @@ -263,6 +281,10 @@ class ActionRenderer { return this._handleChoose(index); } + if (actionType === "repeat") { + return this._handleRepeat(index); + } + this._renderEntry(path, describeAction(this.hass, data, actionType)); let i = index + 1; @@ -374,6 +396,34 @@ class ActionRenderer { return i; } + private _handleRepeat(index: number): number { + const repeatPath = this.keys[index]; + const startLevel = repeatPath.split("/").length; + + const repeatConfig = this._getDataFromPath( + this.keys[index] + ) as RepeatAction; + const name = repeatConfig.alias || describeAction(this.hass, repeatConfig); + + this._renderEntry(repeatPath, name); + + let i; + + for (i = index + 1; i < this.keys.length; i++) { + const path = this.keys[i]; + const parts = path.split("/"); + + // We're done if no more sequence in current level + if (parts.length <= startLevel) { + return i; + } + + i = this._renderItem(i, getActionType(this._getDataFromPath(path)), true); + } + + return i; + } + private _renderEntry( path: string, description: string,