@@ -29,8 +29,8 @@ export const entityStateDisplay = (hass, stateObj, config) => {
29
29
return hass . localize ( `state.default.${ stateObj . state } ` ) ;
30
30
}
31
31
32
- const value = config . attribute ? stateObj . attributes [ config . attribute ] : stateObj . state ;
33
- const unit =
32
+ let value = config . attribute ? stateObj . attributes [ config . attribute ] : stateObj . state ;
33
+ let unit =
34
34
config . unit === false
35
35
? undefined
36
36
: config . attribute !== undefined
@@ -39,23 +39,27 @@ export const entityStateDisplay = (hass, stateObj, config) => {
39
39
40
40
if ( config . format ) {
41
41
if ( isNaN ( parseFloat ( value ) ) || ! isFinite ( value ) ) {
42
- return value ;
43
- }
44
- if ( config . format === 'brightness' ) {
45
- return `${ Math . round ( ( value / 255 ) * 100 ) } %` ;
46
- }
47
- if ( config . format === 'duration' ) {
48
- return secondsToDuration ( value ) ;
49
- }
50
- if ( config . format . startsWith ( 'precision' ) ) {
42
+ // do nothing if not a number
43
+ } else if ( config . format === 'brightness' ) {
44
+ value = Math . round ( ( value / 255 ) * 100 ) ;
45
+ unit = '%' ;
46
+ } else if ( config . format . startsWith ( 'duration' ) ) {
47
+ value = secondsToDuration ( config . format === 'duration-m' ? value / 1000 : value ) ;
48
+ unit = undefined ;
49
+ } else if ( config . format . startsWith ( 'precision' ) ) {
51
50
const precision = parseInt ( config . format . slice ( - 1 ) , 10 ) ;
52
- const formatted = formatNumber ( parseFloat ( value ) , hass . locale , {
51
+ value = formatNumber ( parseFloat ( value ) , hass . locale , {
53
52
minimumFractionDigits : precision ,
54
53
maximumFractionDigits : precision ,
55
54
} ) ;
56
- return `${ formatted } ${ unit ? ` ${ unit } ` : '' } ` ;
55
+ } else if ( config . format === 'kilo' ) {
56
+ value = formatNumber ( value / 1000 , hass . locale , { maximumFractionDigits : 2 } ) ;
57
+ } else if ( config . format === 'invert' ) {
58
+ value = value - value * 2 ;
59
+ } else if ( config . format === 'position' ) {
60
+ value = 100 - value ;
57
61
}
58
- return value ;
62
+ return ` ${ value } ${ unit ? ` ${ unit } ` : '' } ` ;
59
63
}
60
64
61
65
if ( config . attribute ) {
0 commit comments