mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-21 16:26:43 +00:00
Simplify script/automation action description with nested conditions/triggers (#17252)
This commit is contained in:
parent
35a427afad
commit
228b75ae83
@ -1,17 +1,18 @@
|
||||
import { ensureArray } from "../common/array/ensure-array";
|
||||
import { formatDuration } from "../common/datetime/format_duration";
|
||||
import secondsToDuration from "../common/datetime/seconds_to_duration";
|
||||
import { ensureArray } from "../common/array/ensure-array";
|
||||
import { computeStateName } from "../common/entity/compute_state_name";
|
||||
import { formatListWithAnds } from "../common/string/format-list";
|
||||
import { isTemplate } from "../common/string/has-template";
|
||||
import { HomeAssistant } from "../types";
|
||||
import { Condition } from "./automation";
|
||||
import { describeCondition, describeTrigger } from "./automation_i18n";
|
||||
import { describeCondition } from "./automation_i18n";
|
||||
import { localizeDeviceAutomationAction } from "./device_automation";
|
||||
import { computeDeviceName } from "./device_registry";
|
||||
import {
|
||||
EntityRegistryEntry,
|
||||
computeEntityRegistryName,
|
||||
entityRegistryById,
|
||||
EntityRegistryEntry,
|
||||
} from "./entity_registry";
|
||||
import { domainToName } from "./integration";
|
||||
import {
|
||||
@ -21,7 +22,6 @@ import {
|
||||
DelayAction,
|
||||
DeviceAction,
|
||||
EventAction,
|
||||
getActionType,
|
||||
IfAction,
|
||||
ParallelAction,
|
||||
PlayMediaAction,
|
||||
@ -30,8 +30,8 @@ import {
|
||||
StopAction,
|
||||
VariablesAction,
|
||||
WaitForTriggerAction,
|
||||
getActionType,
|
||||
} from "./script";
|
||||
import { formatListWithAnds } from "../common/string/format-list";
|
||||
|
||||
const actionTranslationBaseKey =
|
||||
"ui.panel.config.automation.editor.actions.type";
|
||||
@ -273,12 +273,9 @@ const tryDescribeAction = <T extends ActionType>(
|
||||
`${actionTranslationBaseKey}.wait_for_trigger.description.wait_for_a_trigger`
|
||||
);
|
||||
}
|
||||
const triggerNames = triggers.map((trigger) =>
|
||||
describeTrigger(trigger, hass, entityRegistry)
|
||||
);
|
||||
return hass.localize(
|
||||
`${actionTranslationBaseKey}.wait_for_trigger.description.wait_for_triggers_with_name`,
|
||||
{ triggers: formatListWithAnds(hass.locale, triggerNames) }
|
||||
`${actionTranslationBaseKey}.wait_for_trigger.description.wait_for_triggers`,
|
||||
{ count: triggers.length }
|
||||
);
|
||||
}
|
||||
|
||||
@ -326,52 +323,13 @@ const tryDescribeAction = <T extends ActionType>(
|
||||
if (actionType === "if") {
|
||||
const config = action as IfAction;
|
||||
|
||||
let ifConditions: string[] = [];
|
||||
if (Array.isArray(config.if)) {
|
||||
const conditions = ensureArray(config.if);
|
||||
conditions.forEach((condition) => {
|
||||
ifConditions.push(describeCondition(condition, hass, entityRegistry));
|
||||
});
|
||||
} else {
|
||||
ifConditions = [config.if];
|
||||
if (config.else !== undefined) {
|
||||
return hass.localize(
|
||||
`${actionTranslationBaseKey}.if.description.if_else`
|
||||
);
|
||||
}
|
||||
|
||||
let elseActions: string[] = [];
|
||||
if (config.else) {
|
||||
if (Array.isArray(config.else)) {
|
||||
const actions = ensureArray(config.else);
|
||||
actions.forEach((currentAction) => {
|
||||
elseActions.push(
|
||||
describeAction(hass, entityRegistry, currentAction, undefined)
|
||||
);
|
||||
});
|
||||
} else {
|
||||
elseActions = [
|
||||
describeAction(hass, entityRegistry, config.else, undefined),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
let thenActions: string[] = [];
|
||||
if (Array.isArray(config.then)) {
|
||||
const actions = ensureArray(config.then);
|
||||
actions.forEach((currentAction) => {
|
||||
thenActions.push(
|
||||
describeAction(hass, entityRegistry, currentAction, undefined)
|
||||
);
|
||||
});
|
||||
} else {
|
||||
thenActions = [
|
||||
describeAction(hass, entityRegistry, config.then, undefined),
|
||||
];
|
||||
}
|
||||
|
||||
return hass.localize(`${actionTranslationBaseKey}.if.description.full`, {
|
||||
hasElse: config.else !== undefined,
|
||||
action: formatListWithAnds(hass.locale, thenActions),
|
||||
conditions: formatListWithAnds(hass.locale, ifConditions),
|
||||
elseAction: formatListWithAnds(hass.locale, elseActions),
|
||||
});
|
||||
return hass.localize(`${actionTranslationBaseKey}.if.description.if`);
|
||||
}
|
||||
|
||||
if (actionType === "choose") {
|
||||
@ -400,20 +358,16 @@ const tryDescribeAction = <T extends ActionType>(
|
||||
{ count: count }
|
||||
);
|
||||
} else if ("while" in config.repeat) {
|
||||
const conditions = ensureArray(config.repeat.while).map((condition) =>
|
||||
describeCondition(condition, hass, entityRegistry)
|
||||
);
|
||||
const conditions = ensureArray(config.repeat.while);
|
||||
chosenAction = hass.localize(
|
||||
`${actionTranslationBaseKey}.repeat.description.while`,
|
||||
{ conditions: formatListWithAnds(hass.locale, conditions) }
|
||||
`${actionTranslationBaseKey}.repeat.description.while_count`,
|
||||
{ count: conditions.length }
|
||||
);
|
||||
} else if ("until" in config.repeat) {
|
||||
const conditions = ensureArray(config.repeat.until).map((condition) =>
|
||||
describeCondition(condition, hass, entityRegistry)
|
||||
);
|
||||
const conditions = ensureArray(config.repeat.until);
|
||||
chosenAction = hass.localize(
|
||||
`${actionTranslationBaseKey}.repeat.description.until`,
|
||||
{ conditions: formatListWithAnds(hass.locale, conditions) }
|
||||
`${actionTranslationBaseKey}.repeat.description.until_count`,
|
||||
{ count: conditions.length }
|
||||
);
|
||||
} else if ("for_each" in config.repeat) {
|
||||
const items = ensureArray(config.repeat.for_each).map((item) =>
|
||||
|
@ -2621,7 +2621,7 @@
|
||||
"continue_timeout": "[%key:ui::panel::config::automation::editor::actions::type::wait_template::continue_timeout%]",
|
||||
"description": {
|
||||
"wait_for_a_trigger": "Wait for a trigger",
|
||||
"wait_for_triggers_with_name": "Wait for ''{triggers}''"
|
||||
"wait_for_triggers": "Wait for {count} {count, plural,\n one {trigger}\n other {triggers}\n}"
|
||||
}
|
||||
},
|
||||
"condition": {
|
||||
@ -2679,8 +2679,8 @@
|
||||
"description": {
|
||||
"full": "Repeat an action {chosenAction}",
|
||||
"count": "{count} {count, plural,\n one {time}\n other {times}\n}",
|
||||
"while": "while ''{conditions}'' is true",
|
||||
"until": "until ''{conditions}'' is true",
|
||||
"while_count": "while {count} {count, plural,\n one {condition matches}\n other {conditions match}\n} ",
|
||||
"until_count": "until {count} {count, plural,\n one {condition matches}\n other {conditions match}\n} ",
|
||||
"for_each": "for every item: {items}"
|
||||
}
|
||||
},
|
||||
@ -2705,7 +2705,8 @@
|
||||
"else": "Else",
|
||||
"add_else": "Add else",
|
||||
"description": {
|
||||
"full": "Perform ''{action}'' if ''{conditions}''{hasElse, select, \n true { otherwise ''{elseAction}''} \n other {}\n } "
|
||||
"if": "Conditionally execute an action",
|
||||
"if_else": "Conditionally execute an action and default to another action"
|
||||
}
|
||||
},
|
||||
"stop": {
|
||||
|
Loading…
x
Reference in New Issue
Block a user