* added several debugs and store objects stringified

This commit is contained in:
Ingo Fischer
2021-09-02 18:52:17 +02:00
parent 59dc922c6f
commit 8f9d3feffd
+17 -7
View File
@@ -48,6 +48,7 @@ function startAdapter(options) {
} }
} }
adapter.log.debug(`Send to HASS for service ${hassObjects[id].native.attr} with ${hassObjects[id].native.domain || hassObjects[id].native.type} and data ${JSON.stringify(serviceData)}`)
hass.callService(hassObjects[id].native.attr, hassObjects[id].native.domain || hassObjects[id].native.type, serviceData, err => hass.callService(hassObjects[id].native.attr, hassObjects[id].native.domain || hassObjects[id].native.type, serviceData, err =>
err && adapter.log.error('Cannot control ' + id + ': ' + err)); err && adapter.log.error('Cannot control ' + id + ': ' + err));
} }
@@ -109,7 +110,7 @@ function syncObjects(objects, cb) {
err && adapter.log.error(err); err && adapter.log.error(err);
if (!oldObj) { if (!oldObj) {
adapter.log.debug('Create "' + obj._id + '"'); adapter.log.debug('Create "' + obj._id + '": ' + JSON.stringify(obj.common));
hassObjects[obj._id] = obj; hassObjects[obj._id] = obj;
adapter.setForeignObject(obj._id, obj, err => { adapter.setForeignObject(obj._id, obj, err => {
err && adapter.log.error(err); err && adapter.log.error(err);
@@ -120,7 +121,7 @@ function syncObjects(objects, cb) {
if (JSON.stringify(obj.native) !== JSON.stringify(oldObj.native)) { if (JSON.stringify(obj.native) !== JSON.stringify(oldObj.native)) {
oldObj.native = obj.native; oldObj.native = obj.native;
adapter.log.debug('Update "' + obj._id + '"'); adapter.log.debug('Update "' + obj._id + '": ' + JSON.stringify(obj.common));
adapter.setForeignObject(obj._id, oldObj, err => { adapter.setForeignObject(obj._id, oldObj, err => {
err => adapter.log.error(err); err => adapter.log.error(err);
setImmediate(syncObjects, objects, cb); setImmediate(syncObjects, objects, cb);
@@ -241,6 +242,7 @@ function parseStates(entities, services, callback) {
if (entity.attributes && entity.attributes.unit_of_measurement) { if (entity.attributes && entity.attributes.unit_of_measurement) {
obj.common.unit = entity.attributes.unit_of_measurement; obj.common.unit = entity.attributes.unit_of_measurement;
} }
adapter.log.debug(`Found Entity state ${obj._id}: ${JSON.stringify(obj.common)} / ${JSON.stringify(obj.native)}`)
objs.push(obj); objs.push(obj);
states.push({id: obj._id, lc: lc, ts: ts, val: entity.state, ack: true}) states.push({id: obj._id, lc: lc, ts: ts, val: entity.state, ack: true})
} }
@@ -283,6 +285,8 @@ function parseStates(entities, services, callback) {
common.type = mapTypes[typeof entity.attributes[attr]]; common.type = mapTypes[typeof entity.attributes[attr]];
} }
adapter.log.debug(`Found Entity attribute ${obj._id}: ${JSON.stringify(obj.common)} / ${JSON.stringify(obj.native)}`)
objs.push(obj); objs.push(obj);
states.push({id: obj._id, lc: lc, ts: ts, val: entity.attributes[attr], ack: true}); states.push({id: obj._id, lc: lc, ts: ts, val: entity.attributes[attr], ack: true});
} }
@@ -312,6 +316,9 @@ function parseStates(entities, services, callback) {
type: serviceType type: serviceType
} }
}; };
adapter.log.debug(`Found Entity service ${obj._id}: ${JSON.stringify(obj.common)} / ${JSON.stringify(obj.native)}`)
objs.push(obj); objs.push(obj);
} }
} }
@@ -328,15 +335,13 @@ function main() {
adapter.setState('info.connection', false, true); adapter.setState('info.connection', false, true);
// in this template all states changes inside the adapters namespace are subscribed
adapter.subscribeStates('*');
hass = new HASS(adapter.config, adapter.log); hass = new HASS(adapter.config, adapter.log);
hass.on('error', err => hass.on('error', err =>
adapter.log.error(err)); adapter.log.error(err));
hass.on('state_changed', entity => { hass.on('state_changed', entity => {
adapter.log.debug(`HASS-Message: State Changed: ${JSON.stringify(entity)}`);
const id = adapter.namespace + '.entities.' + entity.entity_id + '.'; const id = adapter.namespace + '.entities.' + entity.entity_id + '.';
const lc = entity.last_changed ? new Date(entity.last_changed).getTime() : undefined; const lc = entity.last_changed ? new Date(entity.last_changed).getTime() : undefined;
const ts = entity.last_updated ? new Date(entity.last_updated).getTime() : undefined; const ts = entity.last_updated ? new Date(entity.last_updated).getTime() : undefined;
@@ -348,7 +353,11 @@ function main() {
if (!entity.attributes.hasOwnProperty(attr) || attr === 'friendly_name' || attr === 'unit_of_measurement' || attr === 'icon') { if (!entity.attributes.hasOwnProperty(attr) || attr === 'friendly_name' || attr === 'unit_of_measurement' || attr === 'icon') {
continue; continue;
} }
adapter.setForeignState(id + attr, {val: entity.attributes[attr], ack: true, lc: lc, ts: ts}); let val = entity.attributes[attr];
if (typeof val === 'object' && val !== null) {
val = JSON.stringify(entity.attributes[attr]);
}
adapter.setForeignState(id + attr, {val, ack: true, lc, ts});
} }
} }
}); });
@@ -377,7 +386,8 @@ function main() {
} else { } else {
//adapter.log.debug(JSON.stringify(services)); //adapter.log.debug(JSON.stringify(services));
parseStates(states, services, () => { parseStates(states, services, () => {
adapter.log.debug('Initial parsing of states done, subscribe to ioBroker states');
adapter.subscribeStates('*');
}); });
} }
}), 100); }), 100);