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"; platform: "state";
entity_id: string | string[]; entity_id: string | string[];
attribute?: string; attribute?: string;
from?: string | number; from?: string | string[];
to?: string | string[] | number; to?: string | string[];
for?: string | number | ForDict; for?: string | number | ForDict;
} }

View File

@ -199,57 +199,46 @@ const tryDescribeTrigger = (
// State Trigger // State Trigger
if (trigger.platform === "state") { if (trigger.platform === "state") {
let base = "When";
const entities: string[] = []; const entities: string[] = [];
const states = hass.states; const states = hass.states;
let attribute = "";
if (trigger.attribute) { if (trigger.attribute) {
const stateObj = Array.isArray(trigger.entity_id) const stateObj = Array.isArray(trigger.entity_id)
? hass.states[trigger.entity_id[0]] ? hass.states[trigger.entity_id[0]]
: hass.states[trigger.entity_id]; : hass.states[trigger.entity_id];
base += ` ${computeAttributeNameDisplay( attribute = computeAttributeNameDisplay(
hass.localize, hass.localize,
stateObj, stateObj,
hass.entities, hass.entities,
trigger.attribute trigger.attribute
)} of`; );
} }
if (Array.isArray(trigger.entity_id)) { const entityArray: string[] = ensureArray(trigger.entity_id);
for (const entity of trigger.entity_id.values()) { if (entityArray) {
for (const entity of entityArray) {
if (states[entity]) { if (states[entity]) {
entities.push(computeStateName(states[entity]) || 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) { const stateObj = hass.states[entityArray[0]];
// no entity_id or empty array
entities.push("something");
}
base += ` ${entities} changes`; let fromChoice = "other";
let fromString = "";
const stateObj =
hass.states[
Array.isArray(trigger.entity_id)
? trigger.entity_id[0]
: trigger.entity_id
];
if (trigger.from !== undefined) { if (trigger.from !== undefined) {
let fromArray: string[] = [];
if (trigger.from === null) { if (trigger.from === null) {
if (!trigger.attribute) { if (!trigger.attribute) {
base += " from any state"; fromChoice = "null";
} }
} else if (Array.isArray(trigger.from)) { } else {
fromArray = ensureArray(trigger.from);
const from: string[] = []; const from: string[] = [];
for (const state of trigger.from.values()) { for (const state of fromArray) {
from.push( from.push(
trigger.attribute trigger.attribute
? hass ? hass
@ -263,34 +252,25 @@ const tryDescribeTrigger = (
); );
} }
if (from.length !== 0) { if (from.length !== 0) {
const fromString = formatListWithOrs(hass.locale, from); fromString = formatListWithOrs(hass.locale, from);
base += ` from ${fromString}`; 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) { if (trigger.to !== undefined) {
let toArray: string[] = [];
if (trigger.to === null) { if (trigger.to === null) {
if (!trigger.attribute) { if (!trigger.attribute) {
base += " to any state"; toChoice = "null";
} }
} else if (Array.isArray(trigger.to)) { } else {
toArray = ensureArray(trigger.to);
const to: string[] = []; const to: string[] = [];
for (const state of trigger.to.values()) { for (const state of toArray) {
to.push( to.push(
trigger.attribute trigger.attribute
? hass ? hass
@ -304,21 +284,9 @@ const tryDescribeTrigger = (
); );
} }
if (to.length !== 0) { if (to.length !== 0) {
const toString = formatListWithOrs(hass.locale, to); toString = formatListWithOrs(hass.locale, to);
base += ` to ${toString}`; 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.from === undefined &&
trigger.to === undefined trigger.to === undefined
) { ) {
base += " state or any attributes"; toChoice = "special";
} }
let duration = "";
if (trigger.for) { if (trigger.for) {
const duration = describeDuration(hass.locale, trigger.for); duration = describeDuration(hass.locale, trigger.for) ?? "";
if (duration) {
base += ` for ${duration}`;
}
} }
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 // Sun Trigger

View File

@ -2569,7 +2569,8 @@
"to": "To (optional)", "to": "To (optional)",
"any_state_ignore_attributes": "Any state (ignoring attribute changes)", "any_state_ignore_attributes": "Any state (ignoring attribute changes)",
"description": { "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": { "homeassistant": {