From a58005d72455c982e362048b489568e453ecc4a6 Mon Sep 17 00:00:00 2001 From: benct Date: Mon, 29 Mar 2021 14:48:59 +0200 Subject: [PATCH] Use localization on attribute values (#178) --- src/entity.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/entity.js b/src/entity.js index a9e7a76..f6ce7f2 100644 --- a/src/entity.js +++ b/src/entity.js @@ -35,9 +35,16 @@ export const entityStateDisplay = (hass, stateObj, config) => { return hass.localize(`state.default.${stateObj.state}`); } + const domain = computeStateDomain(stateObj); + const computeDisplay = (value) => + (stateObj.attributes.device_class && + hass.localize(`component.${domain}.state.${stateObj.attributes.device_class}.${value}`)) || + hass.localize(`component.${domain}.state._.${value}`) || + value; + if (config.attribute) { return config.attribute in stateObj.attributes - ? `${stateObj.attributes[config.attribute]}${config.unit ? ` ${config.unit}` : ''}` + ? `${computeDisplay(stateObj.attributes[config.attribute])}${config.unit ? ` ${config.unit}` : ''}` : hass.localize('state.default.unavailable'); } @@ -45,8 +52,6 @@ export const entityStateDisplay = (hass, stateObj, config) => { return `${stateObj.state} ${config.unit || stateObj.attributes.unit_of_measurement}`; } - const domain = computeStateDomain(stateObj); - if (domain === 'input_datetime') { let date; if (!stateObj.attributes.has_time) { @@ -75,12 +80,7 @@ export const entityStateDisplay = (hass, stateObj, config) => { return formatDateTime(date, hass.language); } - return ( - (stateObj.attributes.device_class && - hass.localize(`component.${domain}.state.${stateObj.attributes.device_class}.${stateObj.state}`)) || - hass.localize(`component.${domain}.state._.${stateObj.state}`) || - stateObj.state - ); + return computeDisplay(stateObj.state); }; export const entityStyles = (config) =>