Skip to content

Commit d8350ca

Browse files
authored
Merge pull request #24 from ChrisRomp/2021-12-timestamp-format
Convert timestamp format from unix to ISO8601 for HA 2021.12.x compatibility
2 parents d25d871 + a35972a commit d8350ca

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/check-button-card.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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;', '');
22

33
export interface config {
44
due: boolean;
@@ -23,7 +23,7 @@ class CheckButtonCard extends HTMLElement {
2323
_counter: number = 0;
2424
_entityState: number = 0;
2525
_configSet: boolean = false;
26-
_undoEntityState: number = 0;
26+
_undoTimestamp: number = 0;
2727
_currentTimestamp: number = 0;
2828
_clearUndo: any;
2929
_showInputTimeout: any;
@@ -527,15 +527,24 @@ class CheckButtonCard extends HTMLElement {
527527
return output;
528528
}
529529

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;
532541
const config = this._config;
533542

534543
const timeout = this._convertToSeconds(config.timeout);
535-
const dueTime = Number(entityState) + timeout;
544+
const dueTime = Number(timestamp) + timeout;
536545
const remainingTime = dueTime - Math.trunc(Date.now() / 1000);
537546

538-
const elapsedTime = Date.now() / 1000 - Number(entityState);
547+
const elapsedTime = Date.now() / 1000 - Number(timestamp);
539548

540549
let displayTime: null | number = null;
541550
let displayText;
@@ -604,8 +613,10 @@ class CheckButtonCard extends HTMLElement {
604613

605614
_buildPayload(timestamp: number) {
606615
const config = this._config;
616+
const timestampDate = new Date(timestamp * 1000);
607617
let payload: any = {};
608-
payload.timestamp = timestamp;
618+
payload.timestamp = timestampDate.toISOString();
619+
payload.timestamp_unix = timestamp;
609620
payload.timestamp_friendly = new Date(timestamp*1000).toLocaleString(config.locale);
610621
payload.timeout = config.timeout;
611622
if (config.timeout) {
@@ -624,7 +635,7 @@ class CheckButtonCard extends HTMLElement {
624635
const root = this.shadowRoot;
625636
root.getElementById('undo').style.removeProperty('visibility');
626637
root.getElementById('buttonBlocker').style.removeProperty('visibility');
627-
this._undoEntityState = this._entityState;
638+
this._undoTimestamp = Date.parse(this._entityState.toString()) / 1000;
628639
this._currentTimestamp = Math.trunc(Date.now() / 1000);
629640
this._clearUndo = this._showUndo();
630641
let payload: any = this._buildPayload(this._currentTimestamp);
@@ -651,7 +662,7 @@ class CheckButtonCard extends HTMLElement {
651662
root.getElementById('undo').style.setProperty('visibility', 'hidden');
652663
root.getElementById('buttonBlocker').style.setProperty('visibility', 'hidden');
653664

654-
let payload: any = this._buildPayload(this._undoEntityState);
665+
let payload: any = this._buildPayload(this._undoTimestamp);
655666

656667
this._publish(payload);
657668
clearTimeout(this._clearUndo);
@@ -675,7 +686,7 @@ class CheckButtonCard extends HTMLElement {
675686
root.getElementById('undo').style.removeProperty('visibility');
676687
root.getElementById('buttonBlocker').style.removeProperty('visibility');
677688
this._currentTimestamp = timestamp;
678-
this._undoEntityState = this._entityState;
689+
this._undoTimestamp = Date.parse(this._entityState.toString()) / 1000;
679690
this._clearUndo = this._showUndo();
680691
}
681692

0 commit comments

Comments
 (0)