diff --git a/gallery/src/pages/automation/describe-action.ts b/gallery/src/pages/automation/describe-action.ts index 6b252c87f7..49fa3dc7f9 100644 --- a/gallery/src/pages/automation/describe-action.ts +++ b/gallery/src/pages/automation/describe-action.ts @@ -77,9 +77,29 @@ const ACTIONS = [ stop: "No one is home!", }, { repeat: { count: 3, sequence: [{ delay: "00:00:01" }] } }, + { + repeat: { + for_each: ["bread", "butter", "cheese"], + sequence: [{ delay: "00:00:01" }], + }, + }, { if: [{ condition: "state" }], then: [{ delay: "00:00:01" }], + else: [{ delay: "00:00:05" }], + }, + { + choose: [ + { + conditions: [{ condition: "state" }], + sequence: [{ delay: "00:00:01" }], + }, + { + conditions: [{ condition: "sun" }], + sequence: [{ delay: "00:00:05" }], + }, + ], + default: [{ delay: "00:00:03" }], }, ]; diff --git a/src/data/script.ts b/src/data/script.ts index 0624095c5d..ada068d25e 100644 --- a/src/data/script.ts +++ b/src/data/script.ts @@ -165,7 +165,7 @@ export interface PlayMediaAction extends BaseAction { } export interface RepeatAction extends BaseAction { - repeat: CountRepeat | WhileRepeat | UntilRepeat; + repeat: CountRepeat | WhileRepeat | UntilRepeat | ForEachRepeat; } interface BaseRepeat extends BaseAction { @@ -184,6 +184,10 @@ export interface UntilRepeat extends BaseRepeat { until: Condition[]; } +export interface ForEachRepeat extends BaseRepeat { + for_each: string | any[]; +} + export interface ChooseActionChoice extends BaseAction { conditions: string | Condition[]; sequence: Action | Action[]; diff --git a/src/data/script_i18n.ts b/src/data/script_i18n.ts index f6c1931cf9..a66c9529e0 100644 --- a/src/data/script_i18n.ts +++ b/src/data/script_i18n.ts @@ -8,12 +8,17 @@ import { describeCondition, describeTrigger } from "./automation_i18n"; import { ActionType, ActionTypes, + ChooseAction, DelayAction, DeviceAction, EventAction, getActionType, + IfAction, + ParallelAction, PlayMediaAction, + RepeatAction, SceneAction, + StopAction, VariablesAction, WaitForTriggerAction, } from "./script"; @@ -161,6 +166,81 @@ export const describeAction = ( return `Test ${describeCondition(action as Condition)}`; } + if (actionType === "stop") { + const config = action as StopAction; + return `Stopped${config.stop ? ` because: ${config.stop}` : ""}`; + } + + if (actionType === "if") { + const config = action as IfAction; + return `If ${ + typeof config.if === "string" + ? config.if + : ensureArray(config.if) + .map((condition) => describeCondition(condition)) + .join(", ") + } then ${ensureArray(config.then).map((thenAction) => + describeAction(hass, thenAction) + )}${ + config.else + ? ` else ${ensureArray(config.else).map((elseAction) => + describeAction(hass, elseAction) + )}` + : "" + }`; + } + + if (actionType === "choose") { + const config = action as ChooseAction; + return config.choose + ? `If ${ensureArray(config.choose) + .map( + (chooseAction) => + `${ + typeof chooseAction.conditions === "string" + ? chooseAction.conditions + : ensureArray(chooseAction.conditions) + .map((condition) => describeCondition(condition)) + .join(", ") + } then ${ensureArray(chooseAction.sequence) + .map((chooseSeq) => describeAction(hass, chooseSeq)) + .join(", ")}` + ) + .join(", else if ")}${ + config.default + ? `. If none match: ${ensureArray(config.default) + .map((dAction) => describeAction(hass, dAction)) + .join(", ")}` + : "" + }` + : "Choose"; + } + + if (actionType === "repeat") { + const config = action as RepeatAction; + return `Repeat ${ensureArray(config.repeat.sequence).map((repeatAction) => + describeAction(hass, repeatAction) + )} ${"count" in config.repeat ? `${config.repeat.count} times` : ""}${ + "while" in config.repeat + ? `while ${ensureArray(config.repeat.while) + .map((condition) => describeCondition(condition)) + .join(", ")} is true` + : "until" in config.repeat + ? `until ${ensureArray(config.repeat.until) + .map((condition) => describeCondition(condition)) + .join(", ")} is true` + : "for_each" in config.repeat + ? `for every item: ${ensureArray(config.repeat.for_each) + .map((item) => JSON.stringify(item)) + .join(", ")}` + : "" + }`; + } + + if (actionType === "check_condition") { + return `Test ${describeCondition(action as Condition)}`; + } + if (actionType === "device_action") { const config = action as DeviceAction; const stateObj = hass.states[config.entity_id as string]; @@ -170,7 +250,10 @@ export const describeAction = ( } if (actionType === "parallel") { - return "Run in parallel"; + const config = action as ParallelAction; + return `Run in parallel: ${ensureArray(config.parallel) + .map((pAction) => describeAction(hass, pAction)) + .join(", ")}`; } return actionType;