mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-23 09:16:38 +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 { formatDuration } from "../common/datetime/format_duration";
|
||||||
import secondsToDuration from "../common/datetime/seconds_to_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 { computeStateName } from "../common/entity/compute_state_name";
|
||||||
|
import { formatListWithAnds } from "../common/string/format-list";
|
||||||
import { isTemplate } from "../common/string/has-template";
|
import { isTemplate } from "../common/string/has-template";
|
||||||
import { HomeAssistant } from "../types";
|
import { HomeAssistant } from "../types";
|
||||||
import { Condition } from "./automation";
|
import { Condition } from "./automation";
|
||||||
import { describeCondition, describeTrigger } from "./automation_i18n";
|
import { describeCondition } from "./automation_i18n";
|
||||||
import { localizeDeviceAutomationAction } from "./device_automation";
|
import { localizeDeviceAutomationAction } from "./device_automation";
|
||||||
import { computeDeviceName } from "./device_registry";
|
import { computeDeviceName } from "./device_registry";
|
||||||
import {
|
import {
|
||||||
|
EntityRegistryEntry,
|
||||||
computeEntityRegistryName,
|
computeEntityRegistryName,
|
||||||
entityRegistryById,
|
entityRegistryById,
|
||||||
EntityRegistryEntry,
|
|
||||||
} from "./entity_registry";
|
} from "./entity_registry";
|
||||||
import { domainToName } from "./integration";
|
import { domainToName } from "./integration";
|
||||||
import {
|
import {
|
||||||
@ -21,7 +22,6 @@ import {
|
|||||||
DelayAction,
|
DelayAction,
|
||||||
DeviceAction,
|
DeviceAction,
|
||||||
EventAction,
|
EventAction,
|
||||||
getActionType,
|
|
||||||
IfAction,
|
IfAction,
|
||||||
ParallelAction,
|
ParallelAction,
|
||||||
PlayMediaAction,
|
PlayMediaAction,
|
||||||
@ -30,8 +30,8 @@ import {
|
|||||||
StopAction,
|
StopAction,
|
||||||
VariablesAction,
|
VariablesAction,
|
||||||
WaitForTriggerAction,
|
WaitForTriggerAction,
|
||||||
|
getActionType,
|
||||||
} from "./script";
|
} from "./script";
|
||||||
import { formatListWithAnds } from "../common/string/format-list";
|
|
||||||
|
|
||||||
const actionTranslationBaseKey =
|
const actionTranslationBaseKey =
|
||||||
"ui.panel.config.automation.editor.actions.type";
|
"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`
|
`${actionTranslationBaseKey}.wait_for_trigger.description.wait_for_a_trigger`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const triggerNames = triggers.map((trigger) =>
|
|
||||||
describeTrigger(trigger, hass, entityRegistry)
|
|
||||||
);
|
|
||||||
return hass.localize(
|
return hass.localize(
|
||||||
`${actionTranslationBaseKey}.wait_for_trigger.description.wait_for_triggers_with_name`,
|
`${actionTranslationBaseKey}.wait_for_trigger.description.wait_for_triggers`,
|
||||||
{ triggers: formatListWithAnds(hass.locale, triggerNames) }
|
{ count: triggers.length }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -326,52 +323,13 @@ const tryDescribeAction = <T extends ActionType>(
|
|||||||
if (actionType === "if") {
|
if (actionType === "if") {
|
||||||
const config = action as IfAction;
|
const config = action as IfAction;
|
||||||
|
|
||||||
let ifConditions: string[] = [];
|
if (config.else !== undefined) {
|
||||||
if (Array.isArray(config.if)) {
|
return hass.localize(
|
||||||
const conditions = ensureArray(config.if);
|
`${actionTranslationBaseKey}.if.description.if_else`
|
||||||
conditions.forEach((condition) => {
|
|
||||||
ifConditions.push(describeCondition(condition, hass, entityRegistry));
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
ifConditions = [config.if];
|
|
||||||
}
|
|
||||||
|
|
||||||
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[] = [];
|
return hass.localize(`${actionTranslationBaseKey}.if.description.if`);
|
||||||
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),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (actionType === "choose") {
|
if (actionType === "choose") {
|
||||||
@ -400,20 +358,16 @@ const tryDescribeAction = <T extends ActionType>(
|
|||||||
{ count: count }
|
{ count: count }
|
||||||
);
|
);
|
||||||
} else if ("while" in config.repeat) {
|
} else if ("while" in config.repeat) {
|
||||||
const conditions = ensureArray(config.repeat.while).map((condition) =>
|
const conditions = ensureArray(config.repeat.while);
|
||||||
describeCondition(condition, hass, entityRegistry)
|
|
||||||
);
|
|
||||||
chosenAction = hass.localize(
|
chosenAction = hass.localize(
|
||||||
`${actionTranslationBaseKey}.repeat.description.while`,
|
`${actionTranslationBaseKey}.repeat.description.while_count`,
|
||||||
{ conditions: formatListWithAnds(hass.locale, conditions) }
|
{ count: conditions.length }
|
||||||
);
|
);
|
||||||
} else if ("until" in config.repeat) {
|
} else if ("until" in config.repeat) {
|
||||||
const conditions = ensureArray(config.repeat.until).map((condition) =>
|
const conditions = ensureArray(config.repeat.until);
|
||||||
describeCondition(condition, hass, entityRegistry)
|
|
||||||
);
|
|
||||||
chosenAction = hass.localize(
|
chosenAction = hass.localize(
|
||||||
`${actionTranslationBaseKey}.repeat.description.until`,
|
`${actionTranslationBaseKey}.repeat.description.until_count`,
|
||||||
{ conditions: formatListWithAnds(hass.locale, conditions) }
|
{ count: conditions.length }
|
||||||
);
|
);
|
||||||
} else if ("for_each" in config.repeat) {
|
} else if ("for_each" in config.repeat) {
|
||||||
const items = ensureArray(config.repeat.for_each).map((item) =>
|
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%]",
|
"continue_timeout": "[%key:ui::panel::config::automation::editor::actions::type::wait_template::continue_timeout%]",
|
||||||
"description": {
|
"description": {
|
||||||
"wait_for_a_trigger": "Wait for a trigger",
|
"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": {
|
"condition": {
|
||||||
@ -2679,8 +2679,8 @@
|
|||||||
"description": {
|
"description": {
|
||||||
"full": "Repeat an action {chosenAction}",
|
"full": "Repeat an action {chosenAction}",
|
||||||
"count": "{count} {count, plural,\n one {time}\n other {times}\n}",
|
"count": "{count} {count, plural,\n one {time}\n other {times}\n}",
|
||||||
"while": "while ''{conditions}'' is true",
|
"while_count": "while {count} {count, plural,\n one {condition matches}\n other {conditions match}\n} ",
|
||||||
"until": "until ''{conditions}'' is true",
|
"until_count": "until {count} {count, plural,\n one {condition matches}\n other {conditions match}\n} ",
|
||||||
"for_each": "for every item: {items}"
|
"for_each": "for every item: {items}"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -2705,7 +2705,8 @@
|
|||||||
"else": "Else",
|
"else": "Else",
|
||||||
"add_else": "Add else",
|
"add_else": "Add else",
|
||||||
"description": {
|
"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": {
|
"stop": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user