1
- console . info ( `%cCHECK-BUTTON-CARD\n%cVersion: 1.2 .0` , 'color: green; font-weight: bold;' , '' ) ;
1
+ console . info ( `%cCHECK-BUTTON-CARD\n%cVersion: 1.3 .0` , 'color: green; font-weight: bold;' , '' ) ;
2
2
3
3
export interface config {
4
4
due : boolean ;
@@ -23,7 +23,7 @@ class CheckButtonCard extends HTMLElement {
23
23
_counter : number = 0 ;
24
24
_entityState : number = 0 ;
25
25
_configSet : boolean = false ;
26
- _undoEntityState : number = 0 ;
26
+ _undoTimestamp : number = 0 ;
27
27
_currentTimestamp : number = 0 ;
28
28
_clearUndo : any ;
29
29
_showInputTimeout : any ;
@@ -527,15 +527,24 @@ class CheckButtonCard extends HTMLElement {
527
527
return output ;
528
528
}
529
529
530
- // Converts seconds into text string.
531
- _convertToText ( entityState : number ) {
530
+ // Converts timestamp into text string.
531
+ _convertToText ( entityState : number | string ) {
532
+ // Try to convert from old timestamp format if present
533
+ if ( entityState === "unknown" ) {
534
+ const timestampAttribute = this . _hass . states [ this . _config . entity ] . attributes . timestamp ;
535
+
536
+ if ( ! isNaN ( timestampAttribute ) ) {
537
+ entityState = new Date ( timestampAttribute * 1000 ) . toISOString ( ) ;
538
+ }
539
+ }
540
+ const timestamp = Date . parse ( entityState . toString ( ) ) / 1000 ;
532
541
const config = this . _config ;
533
542
534
543
const timeout = this . _convertToSeconds ( config . timeout ) ;
535
- const dueTime = Number ( entityState ) + timeout ;
544
+ const dueTime = Number ( timestamp ) + timeout ;
536
545
const remainingTime = dueTime - Math . trunc ( Date . now ( ) / 1000 ) ;
537
546
538
- const elapsedTime = Date . now ( ) / 1000 - Number ( entityState ) ;
547
+ const elapsedTime = Date . now ( ) / 1000 - Number ( timestamp ) ;
539
548
540
549
let displayTime : null | number = null ;
541
550
let displayText ;
@@ -604,8 +613,10 @@ class CheckButtonCard extends HTMLElement {
604
613
605
614
_buildPayload ( timestamp : number ) {
606
615
const config = this . _config ;
616
+ const timestampDate = new Date ( timestamp * 1000 ) ;
607
617
let payload : any = { } ;
608
- payload . timestamp = timestamp ;
618
+ payload . timestamp = timestampDate . toISOString ( ) ;
619
+ payload . timestamp_unix = timestamp ;
609
620
payload . timestamp_friendly = new Date ( timestamp * 1000 ) . toLocaleString ( config . locale ) ;
610
621
payload . timeout = config . timeout ;
611
622
if ( config . timeout ) {
@@ -624,7 +635,7 @@ class CheckButtonCard extends HTMLElement {
624
635
const root = this . shadowRoot ;
625
636
root . getElementById ( 'undo' ) . style . removeProperty ( 'visibility' ) ;
626
637
root . getElementById ( 'buttonBlocker' ) . style . removeProperty ( 'visibility' ) ;
627
- this . _undoEntityState = this . _entityState ;
638
+ this . _undoTimestamp = Date . parse ( this . _entityState . toString ( ) ) / 1000 ;
628
639
this . _currentTimestamp = Math . trunc ( Date . now ( ) / 1000 ) ;
629
640
this . _clearUndo = this . _showUndo ( ) ;
630
641
let payload : any = this . _buildPayload ( this . _currentTimestamp ) ;
@@ -651,7 +662,7 @@ class CheckButtonCard extends HTMLElement {
651
662
root . getElementById ( 'undo' ) . style . setProperty ( 'visibility' , 'hidden' ) ;
652
663
root . getElementById ( 'buttonBlocker' ) . style . setProperty ( 'visibility' , 'hidden' ) ;
653
664
654
- let payload : any = this . _buildPayload ( this . _undoEntityState ) ;
665
+ let payload : any = this . _buildPayload ( this . _undoTimestamp ) ;
655
666
656
667
this . _publish ( payload ) ;
657
668
clearTimeout ( this . _clearUndo ) ;
@@ -675,7 +686,7 @@ class CheckButtonCard extends HTMLElement {
675
686
root . getElementById ( 'undo' ) . style . removeProperty ( 'visibility' ) ;
676
687
root . getElementById ( 'buttonBlocker' ) . style . removeProperty ( 'visibility' ) ;
677
688
this . _currentTimestamp = timestamp ;
678
- this . _undoEntityState = this . _entityState ;
689
+ this . _undoTimestamp = Date . parse ( this . _entityState . toString ( ) ) / 1000 ;
679
690
this . _clearUndo = this . _showUndo ( ) ;
680
691
}
681
692
0 commit comments