* (Apollon77) Prevent crashes when attributes contain "." at the end of their names

* (Apollon77) Added logging for state updates for unknown objects
This commit is contained in:
Ingo Fischer
2022-12-08 23:31:18 +01:00
parent 5a8e22b408
commit 56a9cbbb1c
2 changed files with 32 additions and 14 deletions
+6
View File
@@ -93,6 +93,12 @@ Please check it https://www.smarthomejetzt.de/mit-iobroker-auf-eine-home-assista
--> -->
## Changelog ## Changelog
### __WORK IN PROGRESS__
* (Apollon77) Added more guidance logging when setting services incorrectly
* (Apollon77) Prevent crashes when attributes contain "." at the end of their names
* (Apollon77) Added logging for state updates for unknown objects
### 1.3.0 (2022-07-01) ### 1.3.0 (2022-07-01)
* (Apollon77) Further optimize sending data to HASS and allow to set values like numbers as normal states if the service has one attribute and it can be mapped * (Apollon77) Further optimize sending data to HASS and allow to set values like numbers as normal states if the service has one attribute and it can be mapped
+26 -14
View File
@@ -50,7 +50,7 @@ function startAdapter(options) {
} }
// If a non-JSON value was set and we only have one relevant field, use this field as value // If a non-JSON value was set and we only have one relevant field, use this field as value
if (Object.keys(requestFields).length === 0) { if (fields && Object.keys(requestFields).length === 0) {
const fieldList = Object.keys(fields); const fieldList = Object.keys(fields);
if (fieldList.length === 1 && fieldList[0] !== 'entity_id') { if (fieldList.length === 1 && fieldList[0] !== 'entity_id') {
requestFields[fieldList[0]] = state.val; requestFields[fieldList[0]] = state.val;
@@ -60,15 +60,17 @@ function startAdapter(options) {
} }
adapter.log.debug('Prepare service call for ' + id + ' with (mapped) request parameters ' + JSON.stringify(requestFields) + ' from value: ' + JSON.stringify(state.val)); adapter.log.debug('Prepare service call for ' + id + ' with (mapped) request parameters ' + JSON.stringify(requestFields) + ' from value: ' + JSON.stringify(state.val));
for (const field in fields) { if (fields) {
if (!fields.hasOwnProperty(field)) { for (const field in fields) {
continue; if (!fields.hasOwnProperty(field)) {
} continue;
}
if (field === 'entity_id') { if (field === 'entity_id') {
target.entity_id = hassObjects[id].native.entity_id target.entity_id = hassObjects[id].native.entity_id
} else if (requestFields[field] !== undefined) { } else if (requestFields[field] !== undefined) {
serviceData[field] = requestFields[field]; serviceData[field] = requestFields[field];
}
} }
} }
const noFields = Object.keys(serviceData).length === 0; const noFields = Object.keys(serviceData).length === 0;
@@ -77,7 +79,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)}`) 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, target, err => { hass.callService(hassObjects[id].native.attr, hassObjects[id].native.domain || hassObjects[id].native.type, serviceData, target, err => {
err && adapter.log.error('Cannot control ' + id + ': ' + err); err && adapter.log.error('Cannot control ' + id + ': ' + err);
if (err && noFields) { if (err && fields && noFields) {
adapter.log.warn(`Please make sure to provide a stringified JSON as value to set relevant fields! Please refer to the Readme for details!`); adapter.log.warn(`Please make sure to provide a stringified JSON as value to set relevant fields! Please refer to the Readme for details!`);
adapter.log.warn(`Allowed field keys are: ${Object.keys(fields).join(', ')}`); adapter.log.warn(`Allowed field keys are: ${Object.keys(fields).join(', ')}`);
} }
@@ -300,8 +302,9 @@ function parseStates(entities, services, callback) {
common = {}; common = {};
} }
const attrId = attr.replace(adapter.FORBIDDEN_CHARS, '_').replace(/\.+$/, '_');
obj = { obj = {
_id: `${adapter.namespace}.entities.${entity.entity_id}.${attr.replace(adapter.FORBIDDEN_CHARS, '_')}`, _id: `${adapter.namespace}.entities.${entity.entity_id}.${attrId}`,
type: 'state', type: 'state',
common: common, common: common,
native: { native: {
@@ -392,11 +395,15 @@ function main() {
return; return;
} }
const id = adapter.namespace + '.entities.' + entity.entity_id + '.'; const id = '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;
if (entity.state !== undefined) { if (entity.state !== undefined) {
adapter.setForeignState(id + 'state', {val: entity.state, ack: true, lc: lc, ts: ts}); if (hassObjects[id + 'state']) {
adapter.setState(id + 'state', {val: entity.state, ack: true, lc: lc, ts: ts});
} else {
adapter.log.info(`State changed for unknown object ${id + 'state'}. Please restart the adapter to resync the objects.`);
}
} }
if (entity.attributes) { if (entity.attributes) {
for (const attr in entity.attributes) { for (const attr in entity.attributes) {
@@ -407,7 +414,12 @@ function main() {
if ((typeof val === 'object' && val !== null) || Array.isArray(val)) { if ((typeof val === 'object' && val !== null) || Array.isArray(val)) {
val = JSON.stringify(val); val = JSON.stringify(val);
} }
adapter.setForeignState(id + attr.replace(adapter.FORBIDDEN_CHARS, '_'), {val, ack: true, lc, ts}); const attrId = attr.replace(adapter.FORBIDDEN_CHARS, '_').replace(/\.+$/, '_');
if (hassObjects[id + attrId]) {
adapter.setState(id + attrId, {val, ack: true, lc, ts});
} else {
adapter.log.info(`State changed for unknown object ${id + attrId}. Please restart the adapter to resync the objects.`);
}
} }
} }
}); });