iobroker.hass 2.0.4 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -56
- package/admin/hass.png +0 -0
- package/admin/hass.svg +4 -0
- package/admin/i18n/de.json +8 -2
- package/admin/i18n/en.json +8 -2
- package/admin/i18n/es.json +8 -2
- package/admin/i18n/fr.json +11 -5
- package/admin/i18n/it.json +8 -2
- package/admin/i18n/nl.json +8 -2
- package/admin/i18n/pl.json +8 -2
- package/admin/i18n/pt.json +8 -2
- package/admin/i18n/ru.json +8 -2
- package/admin/i18n/uk.json +8 -2
- package/admin/i18n/zh-cn.json +8 -2
- package/admin/jsonConfig.json +22 -0
- package/build/lib/entityFilter.js +80 -0
- package/build/lib/entityFilter.js.map +1 -0
- package/build/lib/hass.js +4 -3
- package/build/lib/hass.js.map +1 -1
- package/build/main.js +329 -75
- package/build/main.js.map +1 -1
- package/io-package.json +52 -46
- package/package.json +19 -11
package/build/main.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const adapter_core_1 = require("@iobroker/adapter-core");
|
|
7
7
|
const hass_1 = __importDefault(require("./lib/hass"));
|
|
8
|
+
const entityFilter_1 = require("./lib/entityFilter");
|
|
8
9
|
const knownAttributes = {
|
|
9
10
|
azimuth: { write: false, read: true, unit: '°' },
|
|
10
11
|
elevation: { write: false, read: true, unit: '°' },
|
|
@@ -149,6 +150,11 @@ function getRoleForAttribute(attr, value, type) {
|
|
|
149
150
|
return 'state';
|
|
150
151
|
}
|
|
151
152
|
}
|
|
153
|
+
/** True if the object holds user configuration like common.custom (history/influxdb/sql adapter settings) */
|
|
154
|
+
function hasCustomConfig(obj) {
|
|
155
|
+
const custom = obj?.common?.custom;
|
|
156
|
+
return !!custom && Object.keys(custom).length > 0;
|
|
157
|
+
}
|
|
152
158
|
class HassAdapter extends adapter_core_1.Adapter {
|
|
153
159
|
hassConnected = false;
|
|
154
160
|
hass = null;
|
|
@@ -156,6 +162,10 @@ class HassAdapter extends adapter_core_1.Adapter {
|
|
|
156
162
|
delayTimeout = null;
|
|
157
163
|
syncDebounceTimeout = null;
|
|
158
164
|
stopped = false;
|
|
165
|
+
excludeFilter = { entityPatterns: [], objectPatterns: [] };
|
|
166
|
+
initialSyncCompleted = false;
|
|
167
|
+
/** Latest state_changed event per entity_id, received before the initial sync completed */
|
|
168
|
+
pendingStateChanges = new Map();
|
|
159
169
|
constructor(options = {}) {
|
|
160
170
|
super({
|
|
161
171
|
...options,
|
|
@@ -187,6 +197,13 @@ class HassAdapter extends adapter_core_1.Adapter {
|
|
|
187
197
|
});
|
|
188
198
|
}, 3000);
|
|
189
199
|
}
|
|
200
|
+
isEntityExcluded(entityId) {
|
|
201
|
+
return (0, entityFilter_1.isEntityIdExcluded)(entityId, this.excludeFilter);
|
|
202
|
+
}
|
|
203
|
+
isObjectIdExcluded(id) {
|
|
204
|
+
const path = id.startsWith(`${this.namespace}.`) ? id.substring(this.namespace.length + 1) : id;
|
|
205
|
+
return (0, entityFilter_1.isObjectPathExcluded)(path, this.excludeFilter);
|
|
206
|
+
}
|
|
190
207
|
onStateChange(id, state) {
|
|
191
208
|
if (!state || state.ack) {
|
|
192
209
|
return;
|
|
@@ -296,6 +313,10 @@ class HassAdapter extends adapter_core_1.Adapter {
|
|
|
296
313
|
for (const state of states) {
|
|
297
314
|
const id = state.id;
|
|
298
315
|
delete state.id;
|
|
316
|
+
if (!this.hassObjects[id]) {
|
|
317
|
+
this.log.debug(`Skip state sync for "${id}": object does not exist`);
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
299
320
|
try {
|
|
300
321
|
await this.setForeignStateAsync(id, state);
|
|
301
322
|
}
|
|
@@ -309,7 +330,6 @@ class HassAdapter extends adapter_core_1.Adapter {
|
|
|
309
330
|
const stats = { newCount: 0, updatedCount: 0 };
|
|
310
331
|
if (objects?.length) {
|
|
311
332
|
for (const obj of objects) {
|
|
312
|
-
this.hassObjects[obj._id] = obj;
|
|
313
333
|
try {
|
|
314
334
|
const oldObj = await this.getForeignObjectAsync(obj._id);
|
|
315
335
|
if (!oldObj) {
|
|
@@ -329,6 +349,7 @@ class HassAdapter extends adapter_core_1.Adapter {
|
|
|
329
349
|
}
|
|
330
350
|
}
|
|
331
351
|
catch (err) {
|
|
352
|
+
delete this.hassObjects[obj._id];
|
|
332
353
|
this.log.error(err.toString());
|
|
333
354
|
}
|
|
334
355
|
}
|
|
@@ -362,8 +383,7 @@ class HassAdapter extends adapter_core_1.Adapter {
|
|
|
362
383
|
// those silently on a transient HASS hiccup would force the user
|
|
363
384
|
// to recreate them. See issue #165.
|
|
364
385
|
const existing = await this.getForeignObjectAsync(id);
|
|
365
|
-
|
|
366
|
-
if (custom && Object.keys(custom).length) {
|
|
386
|
+
if (hasCustomConfig(existing)) {
|
|
367
387
|
this.log.debug(`Keeping "${id}" despite being stale: object holds custom adapter configuration`);
|
|
368
388
|
continue;
|
|
369
389
|
}
|
|
@@ -381,11 +401,21 @@ class HassAdapter extends adapter_core_1.Adapter {
|
|
|
381
401
|
const objs = [];
|
|
382
402
|
const states = [];
|
|
383
403
|
const expectedObjects = new Set();
|
|
404
|
+
let excludedCount = 0;
|
|
405
|
+
let excludedObjectCount = 0;
|
|
406
|
+
const excludedIds = [];
|
|
384
407
|
for (let e = 0; e < entities.length; e++) {
|
|
385
408
|
const entity = entities[e];
|
|
386
409
|
if (!entity) {
|
|
387
410
|
continue;
|
|
388
411
|
}
|
|
412
|
+
if (this.isEntityExcluded(entity.entity_id)) {
|
|
413
|
+
excludedCount++;
|
|
414
|
+
if (this.config.verboseFilterLog && !this.initialSyncCompleted) {
|
|
415
|
+
excludedIds.push(entity.entity_id);
|
|
416
|
+
}
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
389
419
|
const name = entity.name || entity.attributes?.friendly_name || entity.entity_id;
|
|
390
420
|
const desc = entity.attributes?.attribution || undefined;
|
|
391
421
|
const channelId = `${this.namespace}.entities.${entity.entity_id}`;
|
|
@@ -409,36 +439,44 @@ class HassAdapter extends adapter_core_1.Adapter {
|
|
|
409
439
|
const ts = entity.last_updated ? new Date(entity.last_updated).getTime() : undefined;
|
|
410
440
|
if (entity.state !== undefined) {
|
|
411
441
|
const stateId = `${channelId}.state`;
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
_id: stateId,
|
|
415
|
-
type: 'state',
|
|
416
|
-
common: {
|
|
417
|
-
name: `${name} STATE`,
|
|
418
|
-
type: typeof entity.state,
|
|
419
|
-
role: getRoleForState(entity),
|
|
420
|
-
read: true,
|
|
421
|
-
write: false,
|
|
422
|
-
},
|
|
423
|
-
native: {
|
|
424
|
-
object_id: entity.object_id,
|
|
425
|
-
domain: entity.domain,
|
|
426
|
-
entity_id: entity.entity_id,
|
|
427
|
-
},
|
|
428
|
-
};
|
|
429
|
-
if (entity.attributes?.unit_of_measurement) {
|
|
430
|
-
obj.common.unit = entity.attributes.unit_of_measurement;
|
|
442
|
+
if (this.isObjectIdExcluded(stateId)) {
|
|
443
|
+
excludedObjectCount++;
|
|
431
444
|
}
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
445
|
+
else {
|
|
446
|
+
expectedObjects.add(stateId);
|
|
447
|
+
const obj = {
|
|
448
|
+
_id: stateId,
|
|
449
|
+
type: 'state',
|
|
450
|
+
common: {
|
|
451
|
+
name: `${name} STATE`,
|
|
452
|
+
type: typeof entity.state,
|
|
453
|
+
role: getRoleForState(entity),
|
|
454
|
+
read: true,
|
|
455
|
+
write: false,
|
|
456
|
+
},
|
|
457
|
+
native: {
|
|
458
|
+
object_id: entity.object_id,
|
|
459
|
+
domain: entity.domain,
|
|
460
|
+
entity_id: entity.entity_id,
|
|
461
|
+
},
|
|
462
|
+
};
|
|
463
|
+
if (entity.attributes?.unit_of_measurement) {
|
|
464
|
+
obj.common.unit = entity.attributes.unit_of_measurement;
|
|
465
|
+
}
|
|
466
|
+
objs.push(obj);
|
|
467
|
+
let val = entity.state;
|
|
468
|
+
if ((typeof val === 'object' && val !== null) || Array.isArray(val)) {
|
|
469
|
+
val = JSON.stringify(val);
|
|
470
|
+
}
|
|
471
|
+
states.push({ id: obj._id, lc, ts, val, ack: true });
|
|
436
472
|
}
|
|
437
|
-
states.push({ id: obj._id, lc, ts, val, ack: true });
|
|
438
473
|
// Create boolean state for on/off entities
|
|
439
474
|
const boolStateId = `${channelId}.state_boolean`;
|
|
440
|
-
|
|
441
|
-
|
|
475
|
+
if (this.isObjectIdExcluded(boolStateId)) {
|
|
476
|
+
excludedObjectCount++;
|
|
477
|
+
}
|
|
478
|
+
else {
|
|
479
|
+
expectedObjects.add(boolStateId);
|
|
442
480
|
const booleanObj = {
|
|
443
481
|
_id: boolStateId,
|
|
444
482
|
type: 'state',
|
|
@@ -485,6 +523,10 @@ class HassAdapter extends adapter_core_1.Adapter {
|
|
|
485
523
|
}
|
|
486
524
|
const attrId = attr.replace(this.FORBIDDEN_CHARS, '_').replace(/\.+$/, '_');
|
|
487
525
|
const fullAttrId = `${channelId}.${attrId}`;
|
|
526
|
+
if (this.isObjectIdExcluded(fullAttrId)) {
|
|
527
|
+
excludedObjectCount++;
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
488
530
|
expectedObjects.add(fullAttrId);
|
|
489
531
|
const obj = {
|
|
490
532
|
_id: fullAttrId,
|
|
@@ -516,6 +558,10 @@ class HassAdapter extends adapter_core_1.Adapter {
|
|
|
516
558
|
for (const s in service) {
|
|
517
559
|
if (Object.prototype.hasOwnProperty.call(service, s)) {
|
|
518
560
|
const serviceId = `${channelId}.${s}`;
|
|
561
|
+
if (this.isObjectIdExcluded(serviceId)) {
|
|
562
|
+
excludedObjectCount++;
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
519
565
|
expectedObjects.add(serviceId);
|
|
520
566
|
const obj = {
|
|
521
567
|
_id: serviceId,
|
|
@@ -555,68 +601,275 @@ class HassAdapter extends adapter_core_1.Adapter {
|
|
|
555
601
|
}
|
|
556
602
|
this.log.info(`Synchronization completed: ${changes.join(', ')}`);
|
|
557
603
|
}
|
|
604
|
+
if (excludedCount > 0) {
|
|
605
|
+
this.log.info(`Entity filter excluded ${excludedCount} entit${excludedCount === 1 ? 'y' : 'ies'} from sync`);
|
|
606
|
+
}
|
|
607
|
+
if (excludedObjectCount > 0) {
|
|
608
|
+
this.log.info(`Entity filter excluded ${excludedObjectCount} single object${excludedObjectCount === 1 ? '' : 's'} from sync`);
|
|
609
|
+
}
|
|
610
|
+
if (excludedIds.length > 0) {
|
|
611
|
+
for (const id of excludedIds) {
|
|
612
|
+
this.log.info(`Entity filter excluded: ${id}`);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
this.initialSyncCompleted = true;
|
|
616
|
+
}
|
|
617
|
+
async cleanupExcludedObjects() {
|
|
618
|
+
if (!this.config.cleanupExcludedOnStart) {
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
if (!this.excludeFilter.entityPatterns.length && !this.excludeFilter.objectPatterns.length) {
|
|
622
|
+
this.log.info('Cleanup skipped: no exclude patterns configured');
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
let allObjects;
|
|
626
|
+
try {
|
|
627
|
+
allObjects = await this.getAdapterObjectsAsync();
|
|
628
|
+
}
|
|
629
|
+
catch (err) {
|
|
630
|
+
this.log.error(`Cleanup: failed to load adapter objects: ${err}`);
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
const prefix = `${this.namespace}.entities.`;
|
|
634
|
+
// Group every object under entities.* of an excluded entity by its derived
|
|
635
|
+
// entity_id. We extract the entity_id from the object id (first two path
|
|
636
|
+
// components after the prefix) instead of native.entity_id — older objects
|
|
637
|
+
// from previous adapter versions may have been written as flat states without
|
|
638
|
+
// a parent channel and without native.entity_id, but their id still encodes
|
|
639
|
+
// the entity. Objects of not excluded entities that match an object path
|
|
640
|
+
// pattern (e.g. `entities.*.*.device_class`) are collected separately.
|
|
641
|
+
const matchedByEntity = new Map();
|
|
642
|
+
const matchedObjectIds = [];
|
|
643
|
+
for (const id in allObjects) {
|
|
644
|
+
if (!Object.prototype.hasOwnProperty.call(allObjects, id) || !id.startsWith(prefix)) {
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
647
|
+
const rest = id.substring(prefix.length);
|
|
648
|
+
const parts = rest.split('.');
|
|
649
|
+
if (parts.length < 2) {
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
const entityId = `${parts[0]}.${parts[1]}`;
|
|
653
|
+
if (this.isEntityExcluded(entityId)) {
|
|
654
|
+
const ids = matchedByEntity.get(entityId);
|
|
655
|
+
if (ids) {
|
|
656
|
+
ids.push(id);
|
|
657
|
+
}
|
|
658
|
+
else {
|
|
659
|
+
matchedByEntity.set(entityId, [id]);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
else if (this.isObjectIdExcluded(id)) {
|
|
663
|
+
matchedObjectIds.push(id);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
if (matchedByEntity.size === 0 && matchedObjectIds.length === 0) {
|
|
667
|
+
this.log.info('Cleanup: no existing objects matched exclude patterns');
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
let deletedEntityCount = 0;
|
|
671
|
+
let deletedIdCount = 0;
|
|
672
|
+
let keptForCustomCount = 0;
|
|
673
|
+
for (const [entityId, ids] of matchedByEntity) {
|
|
674
|
+
// Custom-config protection: if any object of the entity holds common.custom
|
|
675
|
+
// (history/influxdb/sql), keep the whole entity, so no state loses its channel.
|
|
676
|
+
if (ids.some(id => hasCustomConfig(allObjects[id]))) {
|
|
677
|
+
keptForCustomCount++;
|
|
678
|
+
this.log.warn(`Cleanup: keeping entity "${entityId}" — has custom adapter config (history/influxdb/sql); remove it manually if you really want to drop it`);
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
// Delete sub-states first (longest ids), then any parent channel last.
|
|
682
|
+
const sortedIds = [...ids].sort((a, b) => b.length - a.length);
|
|
683
|
+
let entityFullyDeleted = true;
|
|
684
|
+
for (const id of sortedIds) {
|
|
685
|
+
try {
|
|
686
|
+
await this.delObjectAsync(id);
|
|
687
|
+
delete this.hassObjects[id];
|
|
688
|
+
deletedIdCount++;
|
|
689
|
+
}
|
|
690
|
+
catch (err) {
|
|
691
|
+
entityFullyDeleted = false;
|
|
692
|
+
this.log.error(`Cleanup: failed to delete "${id}": ${err}`);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
if (entityFullyDeleted) {
|
|
696
|
+
deletedEntityCount++;
|
|
697
|
+
if (this.config.verboseFilterLog) {
|
|
698
|
+
this.log.info(`Cleanup: deleted entity "${entityId}" (${ids.length} object${ids.length === 1 ? '' : 's'})`);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
for (const id of matchedObjectIds) {
|
|
703
|
+
if (hasCustomConfig(allObjects[id])) {
|
|
704
|
+
keptForCustomCount++;
|
|
705
|
+
this.log.warn(`Cleanup: keeping object "${id}" — has custom adapter config (history/influxdb/sql); remove it manually if you really want to drop it`);
|
|
706
|
+
continue;
|
|
707
|
+
}
|
|
708
|
+
try {
|
|
709
|
+
await this.delObjectAsync(id);
|
|
710
|
+
delete this.hassObjects[id];
|
|
711
|
+
deletedIdCount++;
|
|
712
|
+
if (this.config.verboseFilterLog) {
|
|
713
|
+
this.log.info(`Cleanup: deleted object "${id}"`);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
catch (err) {
|
|
717
|
+
this.log.error(`Cleanup: failed to delete "${id}": ${err}`);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
this.log.info(`Cleanup: deleted ${deletedEntityCount} excluded entit${deletedEntityCount === 1 ? 'y' : 'ies'} (${deletedIdCount} object${deletedIdCount === 1 ? '' : 's'} total)${keptForCustomCount > 0
|
|
721
|
+
? `, kept ${keptForCustomCount} entit${keptForCustomCount === 1 ? 'y' : 'ies'}/object${keptForCustomCount === 1 ? '' : 's'} with custom config (see warnings above)`
|
|
722
|
+
: ''}`);
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Applies the state changes buffered while the initial sync was running. The initial
|
|
726
|
+
* sync can take minutes on large installations, and dropping these events would leave
|
|
727
|
+
* rarely changing states (e.g. door contacts) outdated until their next change.
|
|
728
|
+
*/
|
|
729
|
+
async replayPendingStateChanges() {
|
|
730
|
+
if (!this.pendingStateChanges.size) {
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
this.log.debug(`Applying ${this.pendingStateChanges.size} state change(s) received during initial sync`);
|
|
734
|
+
// Each entry is removed before it is applied. A live event arriving meanwhile removes
|
|
735
|
+
// its entity from the map, so it is never overwritten by an older buffered one.
|
|
736
|
+
for (const [entityId, entity] of this.pendingStateChanges) {
|
|
737
|
+
this.pendingStateChanges.delete(entityId);
|
|
738
|
+
try {
|
|
739
|
+
await this.onHassStateChanged(entity);
|
|
740
|
+
}
|
|
741
|
+
catch (err) {
|
|
742
|
+
this.log.error(`Cannot apply state change for ${entityId}: ${err}`);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
async onHassStateChanged(entity) {
|
|
747
|
+
if (this.isEntityExcluded(entity.entity_id)) {
|
|
748
|
+
this.log.debug(`Entity filter: ignored state_changed for ${entity.entity_id}`);
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
const id = `entities.${entity.entity_id}.`;
|
|
752
|
+
const lc = entity.last_changed ? new Date(entity.last_changed).getTime() : undefined;
|
|
753
|
+
const ts = entity.last_updated ? new Date(entity.last_updated).getTime() : undefined;
|
|
754
|
+
if (entity.state !== undefined) {
|
|
755
|
+
const stateObjectId = `${this.namespace}.${id}state`;
|
|
756
|
+
if (this.isObjectIdExcluded(stateObjectId)) {
|
|
757
|
+
this.log.debug(`Entity filter: ignored state_changed for ${id}state`);
|
|
758
|
+
}
|
|
759
|
+
else if (this.hassObjects[stateObjectId]) {
|
|
760
|
+
await this.setStateAsync(`${id}state`, { val: entity.state, ack: true, lc, ts });
|
|
761
|
+
}
|
|
762
|
+
else {
|
|
763
|
+
this.log.info(`State changed for unknown object ${id}state. Triggering synchronization to resync the objects.`);
|
|
764
|
+
this.debouncedSync();
|
|
765
|
+
}
|
|
766
|
+
// Update boolean state
|
|
767
|
+
const booleanObjectId = `${this.namespace}.${id}state_boolean`;
|
|
768
|
+
if (this.isObjectIdExcluded(booleanObjectId)) {
|
|
769
|
+
this.log.debug(`Entity filter: ignored state_changed for ${id}state_boolean`);
|
|
770
|
+
}
|
|
771
|
+
else if (this.hassObjects[booleanObjectId]) {
|
|
772
|
+
await this.setStateAsync(`${id}state_boolean`, {
|
|
773
|
+
val: entity.state === 'on',
|
|
774
|
+
ack: true,
|
|
775
|
+
lc: lc || Date.now(),
|
|
776
|
+
ts: ts || Date.now(),
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
if (entity.attributes) {
|
|
781
|
+
// Attributes belong to the entity channel. Do not check the `state` object here:
|
|
782
|
+
// it may be excluded by an object path pattern, and every attribute update would
|
|
783
|
+
// then trigger a full resync.
|
|
784
|
+
const channelObjectId = `${this.namespace}.entities.${entity.entity_id}`;
|
|
785
|
+
for (const attr in entity.attributes) {
|
|
786
|
+
if (!Object.prototype.hasOwnProperty.call(entity.attributes, attr) ||
|
|
787
|
+
attr === 'friendly_name' ||
|
|
788
|
+
attr === 'unit_of_measurement' ||
|
|
789
|
+
attr === 'icon' ||
|
|
790
|
+
!attr.length) {
|
|
791
|
+
continue;
|
|
792
|
+
}
|
|
793
|
+
let val = entity.attributes[attr];
|
|
794
|
+
if ((typeof val === 'object' && val !== null) || Array.isArray(val)) {
|
|
795
|
+
val = JSON.stringify(val);
|
|
796
|
+
}
|
|
797
|
+
const attrId = attr.replace(this.FORBIDDEN_CHARS, '_').replace(/\.+$/, '_');
|
|
798
|
+
const fullAttrId = `${this.namespace}.${id}${attrId}`;
|
|
799
|
+
if (this.isObjectIdExcluded(fullAttrId)) {
|
|
800
|
+
this.log.debug(`Entity filter: ignored state_changed for ${id}${attrId}`);
|
|
801
|
+
continue;
|
|
802
|
+
}
|
|
803
|
+
if (this.hassObjects[channelObjectId]) {
|
|
804
|
+
if (!this.hassObjects[fullAttrId]) {
|
|
805
|
+
// Attribute appeared after initial sync — create object dynamically
|
|
806
|
+
const common = {
|
|
807
|
+
...knownAttributes[attr],
|
|
808
|
+
name: attr.replace(/_/g, ' '),
|
|
809
|
+
read: true,
|
|
810
|
+
write: false,
|
|
811
|
+
role: 'state',
|
|
812
|
+
type: mapTypes[typeof entity.attributes[attr]] ?? 'mixed',
|
|
813
|
+
};
|
|
814
|
+
const newObj = {
|
|
815
|
+
_id: fullAttrId,
|
|
816
|
+
type: 'state',
|
|
817
|
+
common,
|
|
818
|
+
native: { entity_id: entity.entity_id, attr },
|
|
819
|
+
};
|
|
820
|
+
this.log.debug(`Creating missing attribute object ${fullAttrId}`);
|
|
821
|
+
await this.setForeignObjectAsync(fullAttrId, newObj);
|
|
822
|
+
this.hassObjects[fullAttrId] = newObj;
|
|
823
|
+
}
|
|
824
|
+
await this.setStateAsync(id + attrId, { val, ack: true, lc, ts });
|
|
825
|
+
}
|
|
826
|
+
else {
|
|
827
|
+
this.log.info(`State changed for unknown object ${id + attrId}. Triggering synchronization to resync the objects.`);
|
|
828
|
+
this.debouncedSync();
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
}
|
|
558
832
|
}
|
|
559
833
|
async main() {
|
|
560
834
|
this.config.host ||= '127.0.0.1';
|
|
561
835
|
this.config.port = parseInt(String(this.config.port), 10) || 8123;
|
|
836
|
+
const rawPatterns = (this.config.excludePatterns || '').toString();
|
|
837
|
+
const stringPatterns = rawPatterns
|
|
838
|
+
.split(/\r?\n/)
|
|
839
|
+
.map(s => s.trim())
|
|
840
|
+
.filter(line => line.length > 0 && !line.startsWith('#'));
|
|
841
|
+
this.excludeFilter = (0, entityFilter_1.buildExcludeFilter)(stringPatterns, this.namespace);
|
|
842
|
+
if (stringPatterns.length === 0) {
|
|
843
|
+
this.log.info('Entity filter inactive (no exclude patterns configured)');
|
|
844
|
+
}
|
|
845
|
+
else {
|
|
846
|
+
this.log.info(`Entity filter active: ${stringPatterns.length} pattern(s) loaded: ${stringPatterns.join(', ')}`);
|
|
847
|
+
}
|
|
848
|
+
await this.cleanupExcludedObjects();
|
|
562
849
|
await this.setStateAsync('info.connection', false, true);
|
|
563
850
|
this.hass = new hass_1.default(this.config, this.log);
|
|
564
851
|
this.hass.on('error', err => this.log.error(err));
|
|
565
|
-
this.hass.on('state_changed', entity => {
|
|
852
|
+
this.hass.on('state_changed', async (entity) => {
|
|
566
853
|
this.log.debug(`HASS-Message: State Changed: ${JSON.stringify(entity)}`);
|
|
567
854
|
if (!entity || typeof entity.entity_id !== 'string') {
|
|
568
855
|
return;
|
|
569
856
|
}
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
}
|
|
577
|
-
else {
|
|
578
|
-
this.log.info(`State changed for unknown object ${id}state. Triggering synchronization to resync the objects.`);
|
|
579
|
-
this.debouncedSync();
|
|
580
|
-
}
|
|
581
|
-
// Update boolean state
|
|
582
|
-
if (this.hassObjects[`${this.namespace}.${id}state_boolean`]) {
|
|
583
|
-
this.setState(`${id}state_boolean`, {
|
|
584
|
-
val: entity.state === 'on',
|
|
585
|
-
ack: true,
|
|
586
|
-
lc: lc || Date.now(),
|
|
587
|
-
ts: ts || Date.now(),
|
|
588
|
-
});
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
if (entity.attributes) {
|
|
592
|
-
for (const attr in entity.attributes) {
|
|
593
|
-
if (!Object.prototype.hasOwnProperty.call(entity.attributes, attr) ||
|
|
594
|
-
attr === 'friendly_name' ||
|
|
595
|
-
attr === 'unit_of_measurement' ||
|
|
596
|
-
attr === 'icon' ||
|
|
597
|
-
!attr.length) {
|
|
598
|
-
continue;
|
|
599
|
-
}
|
|
600
|
-
let val = entity.attributes[attr];
|
|
601
|
-
if ((typeof val === 'object' && val !== null) || Array.isArray(val)) {
|
|
602
|
-
val = JSON.stringify(val);
|
|
603
|
-
}
|
|
604
|
-
const attrId = attr.replace(this.FORBIDDEN_CHARS, '_').replace(/\.+$/, '_');
|
|
605
|
-
if (this.hassObjects[`${this.namespace}.${id}state`]) {
|
|
606
|
-
this.setState(id + attrId, { val, ack: true, lc, ts });
|
|
607
|
-
}
|
|
608
|
-
else {
|
|
609
|
-
this.log.info(`State changed for unknown object ${id + attrId}. Triggering synchronization to resync the objects.`);
|
|
610
|
-
this.debouncedSync();
|
|
611
|
-
}
|
|
612
|
-
}
|
|
857
|
+
if (!this.initialSyncCompleted) {
|
|
858
|
+
// Objects are just being created, so keep the latest change per entity
|
|
859
|
+
// and apply it after the initial sync instead of triggering a resync.
|
|
860
|
+
this.log.debug(`Deferring state_changed for ${entity.entity_id} until initial sync completed`);
|
|
861
|
+
this.pendingStateChanges.set(entity.entity_id, entity);
|
|
862
|
+
return;
|
|
613
863
|
}
|
|
864
|
+
// This live event supersedes a buffered one that was not replayed yet
|
|
865
|
+
this.pendingStateChanges.delete(entity.entity_id);
|
|
866
|
+
await this.onHassStateChanged(entity);
|
|
614
867
|
});
|
|
615
868
|
this.hass.on('connected', () => {
|
|
616
869
|
if (!this.hassConnected) {
|
|
617
870
|
this.log.debug('Connected');
|
|
618
871
|
this.hassConnected = true;
|
|
619
|
-
this.setState('info.connection', true, true);
|
|
872
|
+
void this.setState('info.connection', true, true);
|
|
620
873
|
this.hass.getConfig(err => {
|
|
621
874
|
if (err) {
|
|
622
875
|
this.log.error(`Cannot read config: ${err}`);
|
|
@@ -645,6 +898,7 @@ class HassAdapter extends adapter_core_1.Adapter {
|
|
|
645
898
|
}
|
|
646
899
|
else {
|
|
647
900
|
await this.parseStates(states, services);
|
|
901
|
+
await this.replayPendingStateChanges();
|
|
648
902
|
this.log.info('Initialization completed');
|
|
649
903
|
await this.subscribeStatesAsync('*');
|
|
650
904
|
}
|
|
@@ -661,7 +915,7 @@ class HassAdapter extends adapter_core_1.Adapter {
|
|
|
661
915
|
if (this.hassConnected) {
|
|
662
916
|
this.log.debug('Disconnected');
|
|
663
917
|
this.hassConnected = false;
|
|
664
|
-
this.setState('info.connection', false, true);
|
|
918
|
+
void this.setState('info.connection', false, true);
|
|
665
919
|
}
|
|
666
920
|
});
|
|
667
921
|
this.hass.connect();
|