|
| 1 | +// Source: https://github.com/home-assistant/frontend/blob/dev/src/common/entity/compute_state_display.ts |
| 2 | + |
| 3 | +import { UNAVAILABLE, UNKNOWN } from "./constants"; |
| 4 | +import { formatDate } from './format_date'; |
| 5 | +import { formatDateTime } from './format_date_time'; |
| 6 | +import { formatTime } from './format_time'; |
| 7 | +import { formatNumber, isNumericState } from './format_number'; |
| 8 | + |
| 9 | +export const computeStateDomain = (stateObj) => stateObj.entity_id.substr(0, stateObj.entity_id.indexOf('.')); |
| 10 | + |
| 11 | +export const computeStateDisplay = (localize, stateObj, locale, state) => { |
| 12 | + const compareState = state !== undefined ? state : stateObj.state; |
| 13 | + |
| 14 | + if (compareState === UNKNOWN || compareState === UNAVAILABLE) { |
| 15 | + return localize(`state.default.${compareState}`); |
| 16 | + } |
| 17 | + |
| 18 | + // Entities with a `unit_of_measurement` or `state_class` are numeric values and should use `formatNumber` |
| 19 | + if (isNumericState(stateObj)) { |
| 20 | + if (stateObj.attributes.device_class === 'monetary') { |
| 21 | + try { |
| 22 | + return formatNumber(compareState, locale, { |
| 23 | + style: 'currency', |
| 24 | + currency: stateObj.attributes.unit_of_measurement, |
| 25 | + }); |
| 26 | + } catch (_err) { |
| 27 | + // fallback to default |
| 28 | + } |
| 29 | + } |
| 30 | + return `${formatNumber(compareState, locale)}${ |
| 31 | + stateObj.attributes.unit_of_measurement ? ' ' + stateObj.attributes.unit_of_measurement : '' |
| 32 | + }`; |
| 33 | + } |
| 34 | + |
| 35 | + const domain = computeStateDomain(stateObj); |
| 36 | + |
| 37 | + if (domain === 'input_datetime') { |
| 38 | + if (state !== undefined) { |
| 39 | + // If trying to display an explicit state, need to parse the explict state to `Date` then format. |
| 40 | + // Attributes aren't available, we have to use `state`. |
| 41 | + try { |
| 42 | + const components = state.split(' '); |
| 43 | + if (components.length === 2) { |
| 44 | + // Date and time. |
| 45 | + return formatDateTime(new Date(components.join('T')), locale); |
| 46 | + } |
| 47 | + if (components.length === 1) { |
| 48 | + if (state.includes('-')) { |
| 49 | + // Date only. |
| 50 | + return formatDate(new Date(`${state}T00:00`), locale); |
| 51 | + } |
| 52 | + if (state.includes(':')) { |
| 53 | + // Time only. |
| 54 | + const now = new Date(); |
| 55 | + return formatTime(new Date(`${now.toISOString().split('T')[0]}T${state}`), locale); |
| 56 | + } |
| 57 | + } |
| 58 | + return state; |
| 59 | + } catch (_e) { |
| 60 | + // Formatting methods may throw error if date parsing doesn't go well, |
| 61 | + // just return the state string in that case. |
| 62 | + return state; |
| 63 | + } |
| 64 | + } else { |
| 65 | + // If not trying to display an explicit state, create `Date` object from `stateObj`'s attributes then format. |
| 66 | + let date; |
| 67 | + if (stateObj.attributes.has_date && stateObj.attributes.has_time) { |
| 68 | + date = new Date( |
| 69 | + stateObj.attributes.year, |
| 70 | + stateObj.attributes.month - 1, |
| 71 | + stateObj.attributes.day, |
| 72 | + stateObj.attributes.hour, |
| 73 | + stateObj.attributes.minute |
| 74 | + ); |
| 75 | + return formatDateTime(date, locale); |
| 76 | + } |
| 77 | + if (stateObj.attributes.has_date) { |
| 78 | + date = new Date(stateObj.attributes.year, stateObj.attributes.month - 1, stateObj.attributes.day); |
| 79 | + return formatDate(date, locale); |
| 80 | + } |
| 81 | + if (stateObj.attributes.has_time) { |
| 82 | + date = new Date(); |
| 83 | + date.setHours(stateObj.attributes.hour, stateObj.attributes.minute); |
| 84 | + return formatTime(date, locale); |
| 85 | + } |
| 86 | + return stateObj.state; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (domain === 'humidifier') { |
| 91 | + if (compareState === 'on' && stateObj.attributes.humidity) { |
| 92 | + return `${stateObj.attributes.humidity} %`; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // `counter` `number` and `input_number` domains do not have a unit of measurement but should still use `formatNumber` |
| 97 | + if (domain === 'counter' || domain === 'number' || domain === 'input_number') { |
| 98 | + return formatNumber(compareState, locale); |
| 99 | + } |
| 100 | + |
| 101 | + // state of button is a timestamp |
| 102 | + if (domain === 'button' || (domain === 'sensor' && stateObj.attributes.device_class === 'timestamp')) { |
| 103 | + return formatDateTime(new Date(compareState), locale); |
| 104 | + } |
| 105 | + |
| 106 | + return ( |
| 107 | + // Return device class translation |
| 108 | + (stateObj.attributes.device_class && |
| 109 | + localize(`component.${domain}.state.${stateObj.attributes.device_class}.${compareState}`)) || |
| 110 | + // Return default translation |
| 111 | + localize(`component.${domain}.state._.${compareState}`) || |
| 112 | + // We don't know! Return the raw state. |
| 113 | + compareState |
| 114 | + ); |
| 115 | +}; |
0 commit comments