Skip to content

Commit

Permalink
Use localization on attribute values (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
benct committed Mar 29, 2021
1 parent 6b596f4 commit a58005d
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,23 @@ 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');
}

if (config.unit !== false && (config.unit || stateObj.attributes.unit_of_measurement)) {
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) {
Expand Down Expand Up @@ -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) =>
Expand Down

0 comments on commit a58005d

Please sign in to comment.