import { HassEntity } from "home-assistant-js-websocket" ; import { formatDateTime } from "./datetime/format_date_time" ; import { formatDate } from "./datetime/format_date" ; import { formatTime } from "./datetime/format_time" ; import { LocalizeFunc } from "./translations/localize" ; import { computeStateDomain } from "./compute-state-domain" ; import { FrontendLocaleData } from "./types" ; import { formatNumber , isNumericState } from "./format-number" ; export const computeStateDisplay = ( localize , stateObj , locale , state ? ) : string => { const compareState = state !== undefined ? state : stateObj . state ; if ( compareState === "unknown" || compareState === "unavailable" ) { return localize ( `state.default.${ compareState }` ) ; } if ( isNumericState ( stateObj ) ) { if ( stateObj . attributes . device_class === "monetary" ) { try { return formatNumber ( compareState , locale , { style : "currency" , currency : stateObj . attributes . unit_of_measurement , } ) ; } catch ( _err ) { } } return `${ formatNumber ( compareState , locale ) }${ stateObj . attributes . unit_of_measurement ? " " + stateObj . attributes . unit_of_measurement : "" }` ; } const domain = computeStateDomain ( stateObj ) ; if ( domain === "input_datetime" ) { if ( state !== undefined ) { try { const components = state . split ( " " ) ; if ( components . length === 2 ) { return formatDateTime ( new Date ( components . join ( "T" ) ) , locale ) ; } if ( components . length === 1 ) { if ( state . includes ( "-" ) ) { return formatDate ( new Date ( `${ state }T00:00` ) , locale ) ; } if ( state . includes ( ":" ) ) { const now = new Date ( ) ; return formatTime ( new Date ( `${ now . toISOString ( ) . split ( "T" ) [ 0 ] }T${ state }` ) , locale ) ; } } return state ; } catch ( _e ) { return state ; } } else { let date ; if ( stateObj . attributes . has_date && stateObj . attributes . has_time ) { date = new Date ( stateObj . attributes . year , stateObj . attributes . month - 1 , stateObj . attributes . day , stateObj . attributes . hour , stateObj . attributes . minute ) ; return formatDateTime ( date , locale ) ; } if ( stateObj . attributes . has_date ) { date = new Date ( stateObj . attributes . year , stateObj . attributes . month - 1 , stateObj . attributes . day ) ; return formatDate ( date , locale ) ; } if ( stateObj . attributes . has_time ) { date = new Date ( ) ; date . setHours ( stateObj . attributes . hour , stateObj . attributes . minute ) ; return formatTime ( date , locale ) ; } return stateObj . state ; } } if ( domain === "humidifier" ) { if ( compareState === "on" && stateObj . attributes . humidity ) { return `${ stateObj . attributes . humidity } %` ; } } if ( domain === "counter" || domain === "number" || domain === "input_number" ) { return formatNumber ( compareState , locale ) ; } return ( ( stateObj . attributes . device_class && localize ( `component.${ domain }.state.${ stateObj . attributes . device_class }.${ compareState }` ) ) || localize ( `component.${ domain }.state._.${ compareState }` ) || compareState ) ; } ;