From 5aa8cc9dc13ee3acf5864fa40a6697907e2d51b9 Mon Sep 17 00:00:00 2001 From: karwosts Date: Sun, 5 May 2024 05:19:55 -0700 Subject: [PATCH 1/2] Minor improvements to service call descriptions. --- src/data/script_i18n.ts | 16 ++++++++++------ src/translations/en.json | 6 +++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/data/script_i18n.ts b/src/data/script_i18n.ts index b08cb49c49ad..059fef7f0c9b 100644 --- a/src/data/script_i18n.ts +++ b/src/data/script_i18n.ts @@ -89,7 +89,8 @@ const tryDescribeAction = ( const config = action as ActionTypes["service"]; const targets: string[] = []; - if (config.target) { + const targetOrData = config.target || config.data; + if (targetOrData) { for (const [key, name] of Object.entries({ area_id: "areas", device_id: "devices", @@ -97,12 +98,10 @@ const tryDescribeAction = ( floor_id: "floors", label_id: "labels", })) { - if (!(key in config.target)) { + if (!(key in targetOrData)) { continue; } - const keyConf: string[] = Array.isArray(config.target[key]) - ? config.target[key] - : [config.target[key]]; + const keyConf: string[] = ensureArray(targetOrData[key]) || []; for (const targetThing of keyConf) { if (isTemplate(targetThing)) { @@ -196,7 +195,10 @@ const tryDescribeAction = ( ) { return hass.localize( `${actionTranslationBaseKey}.service.description.service_based_on_template`, - { targets: formatListWithAnds(hass.locale, targets) } + { + hasTargets: targets.length ? "true" : "false", + targets: formatListWithAnds(hass.locale, targets), + } ); } @@ -212,6 +214,7 @@ const tryDescribeAction = ( { domain: domainToName(hass.localize, domain), name: service || config.service, + hasTargets: targets.length ? "true" : "false", targets: formatListWithAnds(hass.locale, targets), } ); @@ -223,6 +226,7 @@ const tryDescribeAction = ( name: service ? `${domainToName(hass.localize, domain)}: ${service}` : config.service, + hasTargets: targets.length ? "true" : "false", targets: formatListWithAnds(hass.locale, targets), } ); diff --git a/src/translations/en.json b/src/translations/en.json index f28231447e1e..1a8f665d2535 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -3248,9 +3248,9 @@ "has_optional_response": "This service can return a response, if you want to use the response, enter the name of a variable the response will be saved in", "has_response": "This service returns a response, enter the name of a variable the response will be saved in", "description": { - "service_based_on_template": "Call a service based on a template on {targets}", - "service_based_on_name": "Call a service ''{name}'' on {targets}", - "service_name": "{domain} ''{name}'' on {targets}", + "service_based_on_template": "Call a service based on a template{hasTargets, select, \n true { on {targets}} \n other {} \n }", + "service_based_on_name": "Call a service ''{name}''{hasTargets, select, \n true { on {targets}} \n other {} \n }", + "service_name": "{domain} ''{name}''{hasTargets, select, \n true { on {targets}} \n other {} \n }", "service": "Call a service", "target_template": "templated {name}", "target_unknown_entity": "unknown entity", From 03866553a36fa79bb422ea21ee83d63125871127 Mon Sep 17 00:00:00 2001 From: karwosts Date: Wed, 8 May 2024 06:52:33 -0700 Subject: [PATCH 2/2] updates from feedback --- src/data/script_i18n.ts | 15 +++++++++------ src/translations/en.json | 9 ++++++--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/data/script_i18n.ts b/src/data/script_i18n.ts index 059fef7f0c9b..686b52438e48 100644 --- a/src/data/script_i18n.ts +++ b/src/data/script_i18n.ts @@ -194,9 +194,10 @@ const tryDescribeAction = ( (config.service && isTemplate(config.service)) ) { return hass.localize( - `${actionTranslationBaseKey}.service.description.service_based_on_template`, + targets.length + ? `${actionTranslationBaseKey}.service.description.service_based_on_template` + : `${actionTranslationBaseKey}.service.description.service_based_on_template_no_targets`, { - hasTargets: targets.length ? "true" : "false", targets: formatListWithAnds(hass.locale, targets), } ); @@ -210,23 +211,25 @@ const tryDescribeAction = ( if (config.metadata) { return hass.localize( - `${actionTranslationBaseKey}.service.description.service_name`, + targets.length + ? `${actionTranslationBaseKey}.service.description.service_name` + : `${actionTranslationBaseKey}.service.description.service_name_no_targets`, { domain: domainToName(hass.localize, domain), name: service || config.service, - hasTargets: targets.length ? "true" : "false", targets: formatListWithAnds(hass.locale, targets), } ); } return hass.localize( - `${actionTranslationBaseKey}.service.description.service_based_on_name`, + targets.length + ? `${actionTranslationBaseKey}.service.description.service_based_on_name` + : `${actionTranslationBaseKey}.service.description.service_based_on_name_no_targets`, { name: service ? `${domainToName(hass.localize, domain)}: ${service}` : config.service, - hasTargets: targets.length ? "true" : "false", targets: formatListWithAnds(hass.locale, targets), } ); diff --git a/src/translations/en.json b/src/translations/en.json index 1a8f665d2535..1bab20677982 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -3248,9 +3248,12 @@ "has_optional_response": "This service can return a response, if you want to use the response, enter the name of a variable the response will be saved in", "has_response": "This service returns a response, enter the name of a variable the response will be saved in", "description": { - "service_based_on_template": "Call a service based on a template{hasTargets, select, \n true { on {targets}} \n other {} \n }", - "service_based_on_name": "Call a service ''{name}''{hasTargets, select, \n true { on {targets}} \n other {} \n }", - "service_name": "{domain} ''{name}''{hasTargets, select, \n true { on {targets}} \n other {} \n }", + "service_based_on_template": "Call a service based on a template on {targets}", + "service_based_on_name": "Call a service ''{name}'' on {targets}", + "service_name": "{domain} ''{name}'' on {targets}", + "service_based_on_template_no_targets": "Call a service based on a template", + "service_based_on_name_no_targets": "Call a service ''{name}''", + "service_name_no_targets": "{domain} ''{name}''", "service": "Call a service", "target_template": "templated {name}", "target_unknown_entity": "unknown entity",