Always recalculate state display

This commit is contained in:
Adam Mills 2017-11-18 19:48:43 -05:00
parent 7d19027875
commit 27c24e2694
No known key found for this signature in database
GPG Key ID: 7733DCD6D0428689
2 changed files with 54 additions and 64 deletions

View File

@ -4,56 +4,56 @@ import formatDate from './format_date';
import formatTime from './format_time';
export default function computeStateDisplay(haLocalize, stateObj, language) {
if (!stateObj._stateDisplay) {
const domain = computeDomain(stateObj);
if (domain === 'binary_sensor') {
// Try device class translation, then default binary sensor translation
if (stateObj.attributes.device_class) {
stateObj._stateDisplay =
haLocalize(`state.${domain}.${stateObj.attributes.device_class}`, stateObj.state);
}
if (!stateObj._stateDisplay) {
stateObj._stateDisplay = haLocalize(`state.${domain}.default`, stateObj.state);
}
} else if (stateObj.attributes.unit_of_measurement) {
stateObj._stateDisplay = stateObj.state + ' ' + stateObj.attributes.unit_of_measurement;
} else if (domain === 'input_datetime') {
let date;
if (!stateObj.attributes.has_time) {
date = new Date(
stateObj.attributes.year,
stateObj.attributes.month - 1,
stateObj.attributes.day
);
stateObj._stateDisplay = formatDate(date, language);
} else if (!stateObj.attributes.has_date) {
date = new Date(
1970, 0, 1,
stateObj.attributes.hour,
stateObj.attributes.minute
);
stateObj._stateDisplay = formatTime(date, language);
} else {
date = new Date(
stateObj.attributes.year, stateObj.attributes.month - 1,
stateObj.attributes.day, stateObj.attributes.hour,
stateObj.attributes.minute
);
stateObj._stateDisplay = formatDateTime(date, language);
}
} else if (domain === 'zwave') {
if (['initializing', 'dead'].includes(stateObj.state)) {
stateObj._stateDisplay = haLocalize('state.zwave.query_stage', stateObj.state, 'query_stage', stateObj.attributes.query_stage);
} else {
stateObj._stateDisplay = haLocalize('state.zwave.default', stateObj.state);
}
} else {
stateObj._stateDisplay = haLocalize(`state.${domain}`, stateObj.state);
}
// Fall back to default or raw state if nothing else matches.
stateObj._stateDisplay = stateObj._stateDisplay
|| haLocalize('state.default', stateObj.state) || stateObj.state;
}
let stateDisplay = null;
return stateObj._stateDisplay;
const domain = computeDomain(stateObj);
if (domain === 'binary_sensor') {
// Try device class translation, then default binary sensor translation
if (stateObj.attributes.device_class) {
stateDisplay =
haLocalize(`state.${domain}.${stateObj.attributes.device_class}`, stateObj.state);
}
if (!stateDisplay) {
stateDisplay = haLocalize(`state.${domain}.default`, stateObj.state);
}
} else if (stateObj.attributes.unit_of_measurement) {
stateDisplay = stateObj.state + ' ' + stateObj.attributes.unit_of_measurement;
} else if (domain === 'input_datetime') {
let date;
if (!stateObj.attributes.has_time) {
date = new Date(
stateObj.attributes.year,
stateObj.attributes.month - 1,
stateObj.attributes.day
);
stateDisplay = formatDate(date, language);
} else if (!stateObj.attributes.has_date) {
date = new Date(
1970, 0, 1,
stateObj.attributes.hour,
stateObj.attributes.minute
);
stateDisplay = formatTime(date, language);
} else {
date = new Date(
stateObj.attributes.year, stateObj.attributes.month - 1,
stateObj.attributes.day, stateObj.attributes.hour,
stateObj.attributes.minute
);
stateDisplay = formatDateTime(date, language);
}
} else if (domain === 'zwave') {
if (['initializing', 'dead'].includes(stateObj.state)) {
stateDisplay = haLocalize('state.zwave.query_stage', stateObj.state, 'query_stage', stateObj.attributes.query_stage);
} else {
stateDisplay = haLocalize('state.zwave.default', stateObj.state);
}
} else {
stateDisplay = haLocalize(`state.${domain}`, stateObj.state);
}
// Fall back to default or raw state if nothing else matches.
stateDisplay = stateDisplay
|| haLocalize('state.default', stateObj.state) || stateObj.state;
return stateDisplay;
}

View File

@ -71,6 +71,9 @@ describe('computeStateDisplay', function() {
},
};
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() {
@ -168,17 +171,4 @@ describe('computeStateDisplay', function() {
};
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');
});
});