@@ -115,7 +115,7 @@ export class AlarmController {
115
115
enabled : true ,
116
116
snooze : true ,
117
117
time : nextAlarmTime . format ( 'HH:mm' ) ,
118
- dateTime : nextAlarmTime . format ( 'YYYY-MM-DD HH:mm' )
118
+ date_time : nextAlarmTime . format ( 'YYYY-MM-DD HH:mm' )
119
119
}
120
120
}
121
121
@@ -143,26 +143,26 @@ export class AlarmController {
143
143
static createNextAlarmNew ( alarm : TimeObject , forToday = false ) : NextAlarmObject {
144
144
let alarmDate = dayjs ( ) ;
145
145
// TODO: is forToday named correctly? should it be notToday?
146
- if ( ! ( ( alarm . time >= alarmDate . format ( 'HH:mm' ) ) && forToday ) ) {
146
+ if ( ! ( ( alarm . time >= alarmDate . format ( 'HH:mm:ss ' ) ) && forToday ) ) {
147
147
alarmDate = alarmDate . add ( 1 , 'day' ) ;
148
148
}
149
149
150
150
return {
151
151
...alarm ,
152
152
date : alarmDate . format ( 'YYYY-MM-DD' ) ,
153
- dateTime : `${ alarmDate . format ( 'YYYY-MM-DD' ) } ${ alarm . time } ` ,
153
+ date_time : `${ alarmDate . format ( 'YYYY-MM-DD' ) } ${ alarm . time } ` ,
154
154
}
155
155
}
156
156
157
157
get nextAlarm ( ) : NextAlarmObject {
158
158
// console.log('*** get nextAlarm on contoller');
159
159
// const nextAlarm = this.controllersAlarmConfig.nextAlarm;
160
- const nextAlarm = Object . assign ( { } , this . _config . nextAlarm ) ;
160
+ const nextAlarm = Object . assign ( { } , this . _config . next_alarm ) ;
161
161
// const nextAlarm = this._config.nextAlarm;
162
162
163
163
if ( ! nextAlarm ) {
164
- // return { enabled: false, time: '07:00', date: '', dateTime : '' };
165
- return Helpers . defaultConfig . nextAlarm ;
164
+ // return { enabled: false, time: '07:00', date: '', date_time : '' };
165
+ return Helpers . defaultConfig . next_alarm ;
166
166
}
167
167
return nextAlarm ;
168
168
}
@@ -219,7 +219,7 @@ export class AlarmController {
219
219
220
220
// const newConfig =
221
221
// console.log('*** newConfig: ', newConfig);
222
- this . _saveConfig ( 'nextAlarm ' , keyValue ) ;
222
+ this . _saveConfig ( 'next_alarm ' , keyValue ) ;
223
223
}
224
224
225
225
get isAlarmEnabled ( ) {
@@ -229,7 +229,7 @@ export class AlarmController {
229
229
return true ;
230
230
}
231
231
// return this.controllersAlarmConfig.alarmsEnabled && nextAlarm.enabled;
232
- return this . _config . alarmsEnabled && nextAlarm . enabled ; //create accessor on this?
232
+ return this . _config . alarms_enabled && nextAlarm . enabled ; //create accessor on this?
233
233
}
234
234
235
235
async _saveConfig ( key , value ) {
@@ -390,7 +390,7 @@ export class AlarmController {
390
390
391
391
const configurationWithLastUpdated = {
392
392
...actualConfiguration ,
393
- lastUpdated : dayjs ( ) . format ( 'YYYY-MM-DD HH:mm:ss' )
393
+ last_updated : dayjs ( ) . format ( 'YYYY-MM-DD HH:mm:ss' )
394
394
}
395
395
396
396
const alarmClockVariableEntityName = 'sensor.' + this . _config . name ;
@@ -416,7 +416,7 @@ export class AlarmController {
416
416
export class AlarmConfiguration {
417
417
418
418
public alarmsEnabled : boolean = false ;
419
- public nextAlarm : NextAlarmObject = { enabled : false , time : '08:00' , date : '' , dateTime : '' } ;
419
+ public nextAlarm : NextAlarmObject = { enabled : false , time : '08:00' , date : '' , date_time : '' } ;
420
420
public mo : TimeObject = { enabled : false , time : '07:00' } ;
421
421
public tu : TimeObject = { enabled : false , time : '07:00' } ;
422
422
public we : TimeObject = { enabled : false , time : '07:00' } ;
@@ -430,7 +430,7 @@ export class AlarmConfiguration {
430
430
public snoozeDurationDefault : TimeObject = { enabled : true , time : '00:15' } ;
431
431
public alarmDurationDefault : TimeObject = { enabled : true , time : '00:30' } ;
432
432
public napDurationDefault : TimeObject = { enabled : true , time : '00:30' } ;
433
- public lastUpdated : string ;
433
+ public last_updated : string ;
434
434
435
435
snooze ( snoozeTime : string ) {
436
436
const nextAlarmTime = dayjs ( this . nextAlarm . time , 'HH:mm' ) . add ( dayjs . duration ( Helpers . convertToMinutes ( snoozeTime ) ) ) ;
@@ -439,7 +439,7 @@ export class AlarmConfiguration {
439
439
enabled : true ,
440
440
snooze : true ,
441
441
time : nextAlarmTime . format ( 'HH:mm' ) ,
442
- dateTime : nextAlarmTime . format ( 'YYYY-MM-DD HH:mm' )
442
+ date_time : nextAlarmTime . format ( 'YYYY-MM-DD HH:mm' )
443
443
}
444
444
}
445
445
@@ -459,7 +459,7 @@ export class AlarmConfiguration {
459
459
return {
460
460
...alarm ,
461
461
date : alarmDate . format ( 'YYYY-MM-DD' ) ,
462
- dateTime : `${ alarmDate . format ( 'YYYY-MM-DD' ) } ${ alarm . time } ` ,
462
+ date_time : `${ alarmDate . format ( 'YYYY-MM-DD' ) } ${ alarm . time } ` ,
463
463
}
464
464
}
465
465
}
@@ -485,6 +485,103 @@ export class Helpers {
485
485
return result ;
486
486
}
487
487
488
+ // returns object containing all and only changed properties
489
+ static deepCompareObj ( original , current ) {
490
+ if ( original === current ) return null ;
491
+
492
+ // Handle non-object types (including null)
493
+ if (
494
+ typeof original !== 'object' ||
495
+ typeof current !== 'object' ||
496
+ original === null ||
497
+ current === null
498
+ ) {
499
+ // return {
500
+ // oldValue: original,
501
+ // newValue: current
502
+ // };
503
+ return current ;
504
+ }
505
+
506
+ // // Handle arrays
507
+ // if (Array.isArray(original) && Array.isArray(current)) {
508
+ // if (original.length !== current.length) {
509
+ // return {
510
+ // oldValue: original,
511
+ // newValue: current
512
+ // };
513
+ // }
514
+
515
+ // const arrayDiffs = {};
516
+ // let hasChanges = false;
517
+
518
+ // for (let i = 0; i < original.length; i++) {
519
+ // const diff = deepObjectDiff(original[i], current[i]);
520
+ // if (diff !== null) {
521
+ // arrayDiffs[i] = diff;
522
+ // hasChanges = true;
523
+ // }
524
+ // }
525
+
526
+ // return hasChanges ? arrayDiffs : null;
527
+ // }
528
+
529
+ const changes = { } ;
530
+ let hasChanges = false ;
531
+
532
+ // Check for changes in current object
533
+ for ( const key of Object . keys ( current ) ) {
534
+ if ( ! ( key in original ) ) {
535
+ // changes[key] = {
536
+ // oldValue: undefined,
537
+ // newValue: current[key]
538
+ // };
539
+ changes [ key ] = current [ key ] ;
540
+ hasChanges = true ;
541
+ continue ;
542
+ }
543
+
544
+ const diff = this . deepCompareObj ( original [ key ] , current [ key ] ) ;
545
+ if ( diff !== null ) {
546
+ changes [ key ] = diff ;
547
+ hasChanges = true ;
548
+ }
549
+ }
550
+
551
+ // Check for deleted keys
552
+ for ( const key of Object . keys ( original ) ) {
553
+ if ( ! ( key in current ) ) {
554
+ // changes[key] = {
555
+ // oldValue: original[key],
556
+ // newValue: undefined
557
+ // };
558
+ changes [ key ] = undefined ;
559
+ hasChanges = true ;
560
+ }
561
+ }
562
+
563
+ return hasChanges ? changes : null ;
564
+ }
565
+
566
+ // // identical or not? returns boolean
567
+ // static deepCompareBool(a: any, b: any) {
568
+ // if (a === b) return true;
569
+ // if (typeof a !== typeof b) return false;
570
+ // if (!(a instanceof Object && b instanceof Object)) return false;
571
+ // for (const x in a) {
572
+ // if (!a.hasOwnProperty(x)) continue;
573
+ // if (!b.hasOwnProperty(x)) return false;
574
+ // if (a[x] === b[x]) continue;
575
+ // if (typeof a[x] !== "object") return false;
576
+ // if (!this.deepCompare(a[x], b[x])) return false;
577
+ // }
578
+ // for (const x in b) {
579
+ // if (!b.hasOwnProperty(x)) continue;
580
+ // if (!a.hasOwnProperty(x)) return false;
581
+ // }
582
+ // return true;
583
+ // }
584
+
488
585
// static findNested(obj, key, value) {
489
586
// if (obj[key] === value) {
490
587
// return obj;
@@ -511,18 +608,18 @@ export class Helpers {
511
608
return found ;
512
609
} ;
513
610
514
- static getHA = ( ) => {
611
+ static getHa = ( ) => {
515
612
let root : any = document . querySelector ( 'home-assistant' ) ;
516
613
return root ;
517
614
}
518
615
519
- // static getEditor = () => {
520
- // let root: any = this.getHA ();
521
- // root = root && root.shadowRoot;
522
- // root = root && root.querySelector('hui-dialog-edit-card');
523
- // // console.log('*** getEditor(); root: ', root);
524
- // return root;
525
- // };
616
+ static getEditor = ( ) => {
617
+ let root : any = this . getHa ( ) ;
618
+ root = root && root . shadowRoot ;
619
+ root = root && root . querySelector ( 'hui-dialog-edit-card' ) ;
620
+ // console.log('*** getEditor(); root: ', root);
621
+ return root ;
622
+ } ;
526
623
527
624
// static getConfigContent = () => {
528
625
// let root: any = this.getEditor();
@@ -589,7 +686,7 @@ export class Helpers {
589
686
// };
590
687
591
688
static getLovelace = ( ) => {
592
- let root : any = this . getHA ( ) ;
689
+ let root : any = this . getHa ( ) ;
593
690
root = root && root . shadowRoot ;
594
691
root = root && root . querySelector ( 'home-assistant-main' ) ;
595
692
root = root && root . shadowRoot ;
@@ -623,7 +720,7 @@ export class Helpers {
623
720
static defaultConfig = {
624
721
name : 'alarm_clock' ,
625
722
alarms_enabled : false ,
626
- nextAlarm : { enabled : false , time : '07:00' , date : '' , dateTime : '' } ,
723
+ next_alarm : { enabled : false , time : '07:00' , date : '' , date_time : '' } ,
627
724
mo : { enabled : false , time : '07:00:00' } ,
628
725
tu : { enabled : false , time : '07:00:00' } ,
629
726
we : { enabled : false , time : '07:00:00' } ,
0 commit comments