mirror of
https://github.com/home-assistant/frontend.git
synced 2025-12-20 02:38:53 +00:00
Always recalculate state display
This commit is contained in:
@@ -4,19 +4,20 @@ import formatDate from './format_date';
|
|||||||
import formatTime from './format_time';
|
import formatTime from './format_time';
|
||||||
|
|
||||||
export default function computeStateDisplay(haLocalize, stateObj, language) {
|
export default function computeStateDisplay(haLocalize, stateObj, language) {
|
||||||
if (!stateObj._stateDisplay) {
|
let stateDisplay = null;
|
||||||
|
|
||||||
const domain = computeDomain(stateObj);
|
const domain = computeDomain(stateObj);
|
||||||
if (domain === 'binary_sensor') {
|
if (domain === 'binary_sensor') {
|
||||||
// Try device class translation, then default binary sensor translation
|
// Try device class translation, then default binary sensor translation
|
||||||
if (stateObj.attributes.device_class) {
|
if (stateObj.attributes.device_class) {
|
||||||
stateObj._stateDisplay =
|
stateDisplay =
|
||||||
haLocalize(`state.${domain}.${stateObj.attributes.device_class}`, stateObj.state);
|
haLocalize(`state.${domain}.${stateObj.attributes.device_class}`, stateObj.state);
|
||||||
}
|
}
|
||||||
if (!stateObj._stateDisplay) {
|
if (!stateDisplay) {
|
||||||
stateObj._stateDisplay = haLocalize(`state.${domain}.default`, stateObj.state);
|
stateDisplay = haLocalize(`state.${domain}.default`, stateObj.state);
|
||||||
}
|
}
|
||||||
} else if (stateObj.attributes.unit_of_measurement) {
|
} else if (stateObj.attributes.unit_of_measurement) {
|
||||||
stateObj._stateDisplay = stateObj.state + ' ' + stateObj.attributes.unit_of_measurement;
|
stateDisplay = stateObj.state + ' ' + stateObj.attributes.unit_of_measurement;
|
||||||
} else if (domain === 'input_datetime') {
|
} else if (domain === 'input_datetime') {
|
||||||
let date;
|
let date;
|
||||||
if (!stateObj.attributes.has_time) {
|
if (!stateObj.attributes.has_time) {
|
||||||
@@ -25,35 +26,34 @@ export default function computeStateDisplay(haLocalize, stateObj, language) {
|
|||||||
stateObj.attributes.month - 1,
|
stateObj.attributes.month - 1,
|
||||||
stateObj.attributes.day
|
stateObj.attributes.day
|
||||||
);
|
);
|
||||||
stateObj._stateDisplay = formatDate(date, language);
|
stateDisplay = formatDate(date, language);
|
||||||
} else if (!stateObj.attributes.has_date) {
|
} else if (!stateObj.attributes.has_date) {
|
||||||
date = new Date(
|
date = new Date(
|
||||||
1970, 0, 1,
|
1970, 0, 1,
|
||||||
stateObj.attributes.hour,
|
stateObj.attributes.hour,
|
||||||
stateObj.attributes.minute
|
stateObj.attributes.minute
|
||||||
);
|
);
|
||||||
stateObj._stateDisplay = formatTime(date, language);
|
stateDisplay = formatTime(date, language);
|
||||||
} else {
|
} else {
|
||||||
date = new Date(
|
date = new Date(
|
||||||
stateObj.attributes.year, stateObj.attributes.month - 1,
|
stateObj.attributes.year, stateObj.attributes.month - 1,
|
||||||
stateObj.attributes.day, stateObj.attributes.hour,
|
stateObj.attributes.day, stateObj.attributes.hour,
|
||||||
stateObj.attributes.minute
|
stateObj.attributes.minute
|
||||||
);
|
);
|
||||||
stateObj._stateDisplay = formatDateTime(date, language);
|
stateDisplay = formatDateTime(date, language);
|
||||||
}
|
}
|
||||||
} else if (domain === 'zwave') {
|
} else if (domain === 'zwave') {
|
||||||
if (['initializing', 'dead'].includes(stateObj.state)) {
|
if (['initializing', 'dead'].includes(stateObj.state)) {
|
||||||
stateObj._stateDisplay = haLocalize('state.zwave.query_stage', stateObj.state, 'query_stage', stateObj.attributes.query_stage);
|
stateDisplay = haLocalize('state.zwave.query_stage', stateObj.state, 'query_stage', stateObj.attributes.query_stage);
|
||||||
} else {
|
} else {
|
||||||
stateObj._stateDisplay = haLocalize('state.zwave.default', stateObj.state);
|
stateDisplay = haLocalize('state.zwave.default', stateObj.state);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
stateObj._stateDisplay = haLocalize(`state.${domain}`, stateObj.state);
|
stateDisplay = haLocalize(`state.${domain}`, stateObj.state);
|
||||||
}
|
}
|
||||||
// Fall back to default or raw state if nothing else matches.
|
// Fall back to default or raw state if nothing else matches.
|
||||||
stateObj._stateDisplay = stateObj._stateDisplay
|
stateDisplay = stateDisplay
|
||||||
|| haLocalize('state.default', stateObj.state) || stateObj.state;
|
|| haLocalize('state.default', stateObj.state) || stateObj.state;
|
||||||
}
|
|
||||||
|
|
||||||
return stateObj._stateDisplay;
|
return stateDisplay;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,6 +71,9 @@ describe('computeStateDisplay', function() {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'November 18, 2017, 11:12 AM');
|
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'November 18, 2017, 11:12 AM');
|
||||||
|
|
||||||
|
// Test changing locales
|
||||||
|
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'de'), '2017 M11 18 11:12');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Localizes input_datetime with date', function() {
|
it('Localizes input_datetime with date', function() {
|
||||||
@@ -168,17 +171,4 @@ describe('computeStateDisplay', function() {
|
|||||||
};
|
};
|
||||||
assert.strictEqual(computeStateDisplay(altHaLocalize, stateObj, 'en'), 'My Custom State');
|
assert.strictEqual(computeStateDisplay(altHaLocalize, stateObj, 'en'), 'My Custom State');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Only calculates state display once per immutable state object', function() {
|
|
||||||
const stateObj = {
|
|
||||||
entity_id: 'cover.test',
|
|
||||||
state: 'open',
|
|
||||||
attributes: {
|
|
||||||
},
|
|
||||||
};
|
|
||||||
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.cover.open');
|
|
||||||
|
|
||||||
stateObj.state = 'closing';
|
|
||||||
assert.strictEqual(computeStateDisplay(haLocalize, stateObj, 'en'), 'state.cover.open');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user