From 28c21b1041f0004c4fee4fb5ab37a913af939465 Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Tue, 30 Jan 2024 23:36:39 +0100 Subject: [PATCH] Localize trigger state in automation editor (#19554) * trigger state * Lokalize * don't change existing trigger * space * Fixes --- src/data/automation.ts | 4 +- src/data/automation_i18n.ts | 110 +++++++++++++++--------------------- src/translations/en.json | 3 +- 3 files changed, 49 insertions(+), 68 deletions(-) diff --git a/src/data/automation.ts b/src/data/automation.ts index 1527822e71..31fd6d6e61 100644 --- a/src/data/automation.ts +++ b/src/data/automation.ts @@ -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; } diff --git a/src/data/automation_i18n.ts b/src/data/automation_i18n.ts index e01c04de14..68fe6c24e2 100644 --- a/src/data/automation_i18n.ts +++ b/src/data/automation_i18n.ts @@ -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 diff --git a/src/translations/en.json b/src/translations/en.json index d7eddeb140..0a4f6378a3 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -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": {