mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-12 20:06:33 +00:00
Properly localize unknown/unavailable sensors (#752)
This commit is contained in:
parent
38088acf14
commit
b3ded276b5
@ -15,7 +15,7 @@ export default function computeStateDisplay(localize, stateObj, language) {
|
|||||||
if (!stateObj._stateDisplay) {
|
if (!stateObj._stateDisplay) {
|
||||||
stateObj._stateDisplay = localize(`state.${domain}.default.${stateObj.state}`);
|
stateObj._stateDisplay = localize(`state.${domain}.default.${stateObj.state}`);
|
||||||
}
|
}
|
||||||
} else if (stateObj.attributes.unit_of_measurement) {
|
} else if (stateObj.attributes.unit_of_measurement && !['unknown', 'unavailable'].includes(stateObj.state)) {
|
||||||
stateObj._stateDisplay = stateObj.state + ' ' + stateObj.attributes.unit_of_measurement;
|
stateObj._stateDisplay = stateObj.state + ' ' + stateObj.attributes.unit_of_measurement;
|
||||||
} else if (domain === 'input_datetime') {
|
} else if (domain === 'input_datetime') {
|
||||||
let date;
|
let date;
|
||||||
|
@ -55,6 +55,36 @@ describe('computeStateDisplay', () => {
|
|||||||
assert.strictEqual(computeStateDisplay(localize, stateObj, 'en'), '123 m');
|
assert.strictEqual(computeStateDisplay(localize, stateObj, 'en'), '123 m');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Localizes unknown sensor value with units', () => {
|
||||||
|
const altLocalize = function (message, ...args) {
|
||||||
|
if (message === 'state.sensor.unknown') return null;
|
||||||
|
return localize(message, ...args);
|
||||||
|
};
|
||||||
|
const stateObj = {
|
||||||
|
entity_id: 'sensor.test',
|
||||||
|
state: 'unknown',
|
||||||
|
attributes: {
|
||||||
|
unit_of_measurement: 'm',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
assert.strictEqual(computeStateDisplay(altLocalize, stateObj, 'en'), 'state.default.unknown');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Localizes unavailable sensor value with units', () => {
|
||||||
|
const altLocalize = function (message, ...args) {
|
||||||
|
if (message === 'state.sensor.unavailable') return null;
|
||||||
|
return localize(message, ...args);
|
||||||
|
};
|
||||||
|
const stateObj = {
|
||||||
|
entity_id: 'sensor.test',
|
||||||
|
state: 'unavailable',
|
||||||
|
attributes: {
|
||||||
|
unit_of_measurement: 'm',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
assert.strictEqual(computeStateDisplay(altLocalize, stateObj, 'en'), 'state.default.unavailable');
|
||||||
|
});
|
||||||
|
|
||||||
it('Localizes input_datetime with full date time', () => {
|
it('Localizes input_datetime with full date time', () => {
|
||||||
const stateObj = {
|
const stateObj = {
|
||||||
entity_id: 'input_datetime.test',
|
entity_id: 'input_datetime.test',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user