Localize trigger state in automation editor (#19554)

* trigger state

* Lokalize

* don't change existing trigger

* space

* Fixes
This commit is contained in:
Simon Lamon 2024-01-30 23:36:39 +01:00 committed by GitHub
parent 0919f0e89e
commit 28c21b1041
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 49 additions and 68 deletions

View File

@ -74,8 +74,8 @@ export interface StateTrigger extends BaseTrigger {
platform: "state";
entity_id: string | string[];
attribute?: string;
from?: string | number;
to?: string | string[] | number;
from?: string | string[];
to?: string | string[];
for?: string | number | ForDict;
}

View File

@ -199,57 +199,46 @@ const tryDescribeTrigger = (
// State Trigger
if (trigger.platform === "state") {
let base = "When";
const entities: string[] = [];
const states = hass.states;
let attribute = "";
if (trigger.attribute) {
const stateObj = Array.isArray(trigger.entity_id)
? hass.states[trigger.entity_id[0]]
: hass.states[trigger.entity_id];
base += ` ${computeAttributeNameDisplay(
attribute = computeAttributeNameDisplay(
hass.localize,
stateObj,
hass.entities,
trigger.attribute
)} of`;
);
}
if (Array.isArray(trigger.entity_id)) {
for (const entity of trigger.entity_id.values()) {
const entityArray: string[] = ensureArray(trigger.entity_id);
if (entityArray) {
for (const entity of entityArray) {
if (states[entity]) {
entities.push(computeStateName(states[entity]) || entity);
}
}
} else if (trigger.entity_id) {
entities.push(
states[trigger.entity_id]
? computeStateName(states[trigger.entity_id])
: trigger.entity_id
);
}
if (entities.length === 0) {
// no entity_id or empty array
entities.push("something");
}
const stateObj = hass.states[entityArray[0]];
base += ` ${entities} changes`;
const stateObj =
hass.states[
Array.isArray(trigger.entity_id)
? trigger.entity_id[0]
: trigger.entity_id
];
let fromChoice = "other";
let fromString = "";
if (trigger.from !== undefined) {
let fromArray: string[] = [];
if (trigger.from === null) {
if (!trigger.attribute) {
base += " from any state";
fromChoice = "null";
}
} else if (Array.isArray(trigger.from)) {
} else {
fromArray = ensureArray(trigger.from);
const from: string[] = [];
for (const state of trigger.from.values()) {
for (const state of fromArray) {
from.push(
trigger.attribute
? hass
@ -263,34 +252,25 @@ const tryDescribeTrigger = (
);
}
if (from.length !== 0) {
const fromString = formatListWithOrs(hass.locale, from);
base += ` from ${fromString}`;
fromString = formatListWithOrs(hass.locale, from);
fromChoice = "fromUsed";
}
} else {
base += ` from ${
trigger.attribute
? hass
.formatEntityAttributeValue(
stateObj,
trigger.attribute,
trigger.from
)
.toString()
: hass
.formatEntityState(stateObj, trigger.from.toString())
.toString()
}`;
}
}
let toChoice = "other";
let toString = "";
if (trigger.to !== undefined) {
let toArray: string[] = [];
if (trigger.to === null) {
if (!trigger.attribute) {
base += " to any state";
toChoice = "null";
}
} else if (Array.isArray(trigger.to)) {
} else {
toArray = ensureArray(trigger.to);
const to: string[] = [];
for (const state of trigger.to.values()) {
for (const state of toArray) {
to.push(
trigger.attribute
? hass
@ -304,21 +284,9 @@ const tryDescribeTrigger = (
);
}
if (to.length !== 0) {
const toString = formatListWithOrs(hass.locale, to);
base += ` to ${toString}`;
toString = formatListWithOrs(hass.locale, to);
toChoice = "toUsed";
}
} else {
base += ` to ${
trigger.attribute
? hass
.formatEntityAttributeValue(
stateObj,
trigger.attribute,
trigger.to
)
.toString()
: hass.formatEntityState(stateObj, trigger.to.toString())
}`;
}
}
@ -327,17 +295,29 @@ const tryDescribeTrigger = (
trigger.from === undefined &&
trigger.to === undefined
) {
base += " state or any attributes";
toChoice = "special";
}
let duration = "";
if (trigger.for) {
const duration = describeDuration(hass.locale, trigger.for);
if (duration) {
base += ` for ${duration}`;
}
duration = describeDuration(hass.locale, trigger.for) ?? "";
}
return base;
return hass.localize(
`${triggerTranslationBaseKey}.state.description.full`,
{
hasAttribute: attribute !== "" ? "true" : "false",
attribute: attribute,
hasEntity: entities.length !== 0 ? "true" : "false",
entity: formatListWithOrs(hass.locale, entities),
fromChoice: fromChoice,
fromString: fromString,
toChoice: toChoice,
toString: toString,
hasDuration: duration !== "" ? "true" : "false",
duration: duration,
}
);
}
// Sun Trigger

View File

@ -2569,7 +2569,8 @@
"to": "To (optional)",
"any_state_ignore_attributes": "Any state (ignoring attribute changes)",
"description": {
"picker": "When the state of an entity (or attribute) changes."
"picker": "When the state of an entity (or attribute) changes.",
"full": "When{hasAttribute, select, \n true { {attribute} of} \n other {}\n} {hasEntity, select, \n true {{entity}} \n other {something}\n} changes{fromChoice, select, \n fromUsed { from {fromString}}\n null { from any state} \n other {}\n}{toChoice, select, \n toUsed { to {toString}} \n null { to any state} \n special { state or any attributes} \n other {}\n}{hasDuration, select, \n true { for {duration}} \n other {}\n}"
}
},
"homeassistant": {