From 5f25b727a272a7d722009b9ccff8a5f5df567dda Mon Sep 17 00:00:00 2001 From: farmio Date: Sat, 20 Apr 2024 16:43:51 +0200 Subject: [PATCH 1/4] Translation support for device trigger extra fields --- .../types/ha-automation-trigger-device.ts | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts b/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts index ad3b9877b0dc..efbda760c975 100644 --- a/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts +++ b/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts @@ -89,7 +89,12 @@ export class HaDeviceTrigger extends LitElement { .schema=${this._capabilities.extra_fields} .disabled=${this.disabled} .computeLabel=${this._extraFieldsComputeLabelCallback( - this.hass.localize + this.hass.localize, + this.trigger + )} + .computeHelper=${this._extraFieldsComputeHelperCallback( + this.hass.localize, + this.trigger )} @value-changed=${this._extraFieldsChanged} > @@ -177,12 +182,27 @@ export class HaDeviceTrigger extends LitElement { }); } - private _extraFieldsComputeLabelCallback(localize) { + private _extraFieldsComputeLabelCallback(localize, trigger: DeviceTrigger) { // Returns a callback for ha-form to calculate labels per schema object - return (schema) => + return (schema): string => localize( `ui.panel.config.automation.editor.triggers.type.device.extra_fields.${schema.name}` - ) || schema.name; + ) || + localize( + `component.${trigger.domain}.device_automation.extra_fields.${schema.name}` + ) || + schema.name; + } + + private _extraFieldsComputeHelperCallback(localize, trigger: DeviceTrigger) { + // Returns a callback for ha-form to calculate helper texts per schema object + return (schema): string | undefined => + localize( + `ui.panel.config.automation.editor.triggers.type.device.extra_fields_descriptions.${schema.name}` + ) || + localize( + `component.${trigger.domain}.device_automation.extra_fields_descriptions.${schema.name}` + ); } static styles = css` From 83c55d12c58af2ee32a3a47e4914440cfcbb0156 Mon Sep 17 00:00:00 2001 From: farmio Date: Mon, 22 Apr 2024 14:36:13 +0200 Subject: [PATCH 2/4] Prefer component translation over default --- .../trigger/types/ha-automation-trigger-device.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts b/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts index efbda760c975..71673e552bd8 100644 --- a/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts +++ b/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts @@ -186,10 +186,10 @@ export class HaDeviceTrigger extends LitElement { // Returns a callback for ha-form to calculate labels per schema object return (schema): string => localize( - `ui.panel.config.automation.editor.triggers.type.device.extra_fields.${schema.name}` + `component.${trigger.domain}.device_automation.extra_fields.${schema.name}` ) || localize( - `component.${trigger.domain}.device_automation.extra_fields.${schema.name}` + `ui.panel.config.automation.editor.triggers.type.device.extra_fields.${schema.name}` ) || schema.name; } @@ -198,10 +198,10 @@ export class HaDeviceTrigger extends LitElement { // Returns a callback for ha-form to calculate helper texts per schema object return (schema): string | undefined => localize( - `ui.panel.config.automation.editor.triggers.type.device.extra_fields_descriptions.${schema.name}` + `component.${trigger.domain}.device_automation.extra_fields_descriptions.${schema.name}` ) || localize( - `component.${trigger.domain}.device_automation.extra_fields_descriptions.${schema.name}` + `ui.panel.config.automation.editor.triggers.type.device.extra_fields_descriptions.${schema.name}` ); } From 9d3ebe133dad29d462c0e03c7973b65d43baffbf Mon Sep 17 00:00:00 2001 From: farmio Date: Mon, 22 Apr 2024 16:40:47 +0200 Subject: [PATCH 3/4] Move device trigger extra_fields translations to backend --- .../trigger/types/ha-automation-trigger-device.ts | 9 +-------- src/translations/en.json | 6 ------ 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts b/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts index 71673e552bd8..8b49ccf869c2 100644 --- a/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts +++ b/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts @@ -187,11 +187,7 @@ export class HaDeviceTrigger extends LitElement { return (schema): string => localize( `component.${trigger.domain}.device_automation.extra_fields.${schema.name}` - ) || - localize( - `ui.panel.config.automation.editor.triggers.type.device.extra_fields.${schema.name}` - ) || - schema.name; + ) || schema.name; } private _extraFieldsComputeHelperCallback(localize, trigger: DeviceTrigger) { @@ -199,9 +195,6 @@ export class HaDeviceTrigger extends LitElement { return (schema): string | undefined => localize( `component.${trigger.domain}.device_automation.extra_fields_descriptions.${schema.name}` - ) || - localize( - `ui.panel.config.automation.editor.triggers.type.device.extra_fields_descriptions.${schema.name}` ); } diff --git a/src/translations/en.json b/src/translations/en.json index 6147da19d24c..73f55f090e51 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -2862,12 +2862,6 @@ "device": { "label": "Device", "trigger": "Trigger", - "extra_fields": { - "above": "Above", - "below": "Below", - "for": "Duration (optional)", - "zone": "[%key:ui::panel::config::automation::editor::triggers::type::zone::label%]" - }, "description": { "picker": "When something happens to a device. Great way to start." } From c64fe6f6530d5baa06960472f7c883d84ca4afce Mon Sep 17 00:00:00 2001 From: farmio Date: Mon, 22 Apr 2024 17:01:35 +0200 Subject: [PATCH 4/4] move translations for extra_fields of conditions and actions too --- src/data/device_automation.ts | 16 ++++++++++++ .../types/ha-automation-action-device_id.ts | 19 +++++++------- .../types/ha-automation-condition-device.ts | 19 +++++++------- .../types/ha-automation-trigger-device.ts | 26 +++++-------------- src/translations/en.json | 18 ------------- 5 files changed, 40 insertions(+), 58 deletions(-) diff --git a/src/data/device_automation.ts b/src/data/device_automation.ts index b32355e833ed..eb0ec9ebdc88 100644 --- a/src/data/device_automation.ts +++ b/src/data/device_automation.ts @@ -249,6 +249,22 @@ export const localizeDeviceAutomationTrigger = ( ) || (trigger.subtype ? `"${trigger.subtype}" ${trigger.type}` : trigger.type!); +export const localizeExtraFieldsComputeLabelCallback = + (hass: HomeAssistant, deviceAutomation: DeviceAutomation) => + // Returns a callback for ha-form to calculate labels per schema object + (schema): string => + hass.localize( + `component.${deviceAutomation.domain}.device_automation.extra_fields.${schema.name}` + ) || schema.name; + +export const localizeExtraFieldsComputeHelperCallback = + (hass: HomeAssistant, deviceAutomation: DeviceAutomation) => + // Returns a callback for ha-form to calculate helper texts per schema object + (schema): string | undefined => + hass.localize( + `component.${deviceAutomation.domain}.device_automation.extra_fields_descriptions.${schema.name}` + ); + export const sortDeviceAutomations = ( automationA: DeviceAutomation, automationB: DeviceAutomation diff --git a/src/panels/config/automation/action/types/ha-automation-action-device_id.ts b/src/panels/config/automation/action/types/ha-automation-action-device_id.ts index f0e782d42323..cf5a09d68db6 100644 --- a/src/panels/config/automation/action/types/ha-automation-action-device_id.ts +++ b/src/panels/config/automation/action/types/ha-automation-action-device_id.ts @@ -12,6 +12,8 @@ import { deviceAutomationsEqual, DeviceCapabilities, fetchDeviceActionCapabilities, + localizeExtraFieldsComputeLabelCallback, + localizeExtraFieldsComputeHelperCallback, } from "../../../../../data/device_automation"; import { EntityRegistryEntry } from "../../../../../data/entity_registry"; import { HomeAssistant } from "../../../../../types"; @@ -84,8 +86,13 @@ export class HaDeviceAction extends LitElement { .data=${this._extraFieldsData(this.action, this._capabilities)} .schema=${this._capabilities.extra_fields} .disabled=${this.disabled} - .computeLabel=${this._extraFieldsComputeLabelCallback( - this.hass.localize + .computeLabel=${localizeExtraFieldsComputeLabelCallback( + this.hass, + this.action + )} + .computeHelper=${localizeExtraFieldsComputeHelperCallback( + this.hass, + this.action )} @value-changed=${this._extraFieldsChanged} > @@ -152,14 +159,6 @@ export class HaDeviceAction extends LitElement { }); } - private _extraFieldsComputeLabelCallback(localize) { - // Returns a callback for ha-form to calculate labels per schema object - return (schema) => - localize( - `ui.panel.config.automation.editor.actions.type.device_id.extra_fields.${schema.name}` - ) || schema.name; - } - static styles = css` ha-device-picker { display: block; diff --git a/src/panels/config/automation/condition/types/ha-automation-condition-device.ts b/src/panels/config/automation/condition/types/ha-automation-condition-device.ts index 4593a5f70123..2e6faadaf92d 100644 --- a/src/panels/config/automation/condition/types/ha-automation-condition-device.ts +++ b/src/panels/config/automation/condition/types/ha-automation-condition-device.ts @@ -12,6 +12,8 @@ import { DeviceCapabilities, DeviceCondition, fetchDeviceConditionCapabilities, + localizeExtraFieldsComputeLabelCallback, + localizeExtraFieldsComputeHelperCallback, } from "../../../../../data/device_automation"; import { EntityRegistryEntry } from "../../../../../data/entity_registry"; import type { HomeAssistant } from "../../../../../types"; @@ -84,8 +86,13 @@ export class HaDeviceCondition extends LitElement { .data=${this._extraFieldsData(this.condition, this._capabilities)} .schema=${this._capabilities.extra_fields} .disabled=${this.disabled} - .computeLabel=${this._extraFieldsComputeLabelCallback( - this.hass.localize + .computeLabel=${localizeExtraFieldsComputeLabelCallback( + this.hass, + this.condition + )} + .computeHelper=${localizeExtraFieldsComputeHelperCallback( + this.hass, + this.condition )} @value-changed=${this._extraFieldsChanged} > @@ -153,14 +160,6 @@ export class HaDeviceCondition extends LitElement { }); } - private _extraFieldsComputeLabelCallback(localize) { - // Returns a callback for ha-form to calculate labels per schema object - return (schema) => - localize( - `ui.panel.config.automation.editor.conditions.type.device.extra_fields.${schema.name}` - ) || schema.name; - } - static styles = css` ha-device-picker { display: block; diff --git a/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts b/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts index 8b49ccf869c2..c69c21b68c2f 100644 --- a/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts +++ b/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts @@ -14,6 +14,8 @@ import { DeviceCapabilities, DeviceTrigger, fetchDeviceTriggerCapabilities, + localizeExtraFieldsComputeLabelCallback, + localizeExtraFieldsComputeHelperCallback, } from "../../../../../data/device_automation"; import { EntityRegistryEntry } from "../../../../../data/entity_registry"; import { HomeAssistant } from "../../../../../types"; @@ -88,12 +90,12 @@ export class HaDeviceTrigger extends LitElement { .data=${this._extraFieldsData(this.trigger, this._capabilities)} .schema=${this._capabilities.extra_fields} .disabled=${this.disabled} - .computeLabel=${this._extraFieldsComputeLabelCallback( - this.hass.localize, + .computeLabel=${localizeExtraFieldsComputeLabelCallback( + this.hass, this.trigger )} - .computeHelper=${this._extraFieldsComputeHelperCallback( - this.hass.localize, + .computeHelper=${localizeExtraFieldsComputeHelperCallback( + this.hass, this.trigger )} @value-changed=${this._extraFieldsChanged} @@ -182,22 +184,6 @@ export class HaDeviceTrigger extends LitElement { }); } - private _extraFieldsComputeLabelCallback(localize, trigger: DeviceTrigger) { - // Returns a callback for ha-form to calculate labels per schema object - return (schema): string => - localize( - `component.${trigger.domain}.device_automation.extra_fields.${schema.name}` - ) || schema.name; - } - - private _extraFieldsComputeHelperCallback(localize, trigger: DeviceTrigger) { - // Returns a callback for ha-form to calculate helper texts per schema object - return (schema): string | undefined => - localize( - `component.${trigger.domain}.device_automation.extra_fields_descriptions.${schema.name}` - ); - } - static styles = css` ha-device-picker { display: block; diff --git a/src/translations/en.json b/src/translations/en.json index 73f55f090e51..bce805d19aa2 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -3099,13 +3099,6 @@ "device": { "label": "Device", "condition": "Condition", - "extra_fields": { - "above": "Above", - "below": "Below", - "for": "Duration", - "hvac_mode": "HVAC mode", - "preset_mode": "Preset mode" - }, "description": { "picker": "Set of conditions provided by your device. Great way to start." } @@ -3336,17 +3329,6 @@ "device_id": { "label": "Device", "action": "Action", - "extra_fields": { - "code": "Code", - "message": "Message", - "title": "Title", - "position": "[%key:ui::card::cover::position%]", - "mode": "Mode", - "humidity": "Humidity", - "value": "Value", - "brightness_pct": "[%key:ui::card::light::brightness%]", - "flash": "Flash" - }, "description": { "picker": "Do something on a device. Great way to start.", "no_device": "Device action"