* (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

This commit is contained in:
Ingo Fischer
2022-06-30 11:51:23 +02:00
parent b54916d743
commit 0ac6c66aaf
2 changed files with 15 additions and 1 deletions
+4
View File
@@ -93,6 +93,10 @@ Please check it https://www.smarthomejetzt.de/mit-iobroker-auf-eine-home-assista
--> -->
## Changelog ## Changelog
### __WORK IN PROGRESS__
* (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
### 1.2.0 (2022-06-17) ### 1.2.0 (2022-06-17)
* (Apollon77) IMPORTANT: Replace special characters in entity attribute names with an underscore! Object IDs might change! * (Apollon77) IMPORTANT: Replace special characters in entity attribute names with an underscore! Object IDs might change!
* (Apollon77) make sure a "null" value in state changes is not crashing * (Apollon77) make sure a "null" value in state changes is not crashing
+11 -1
View File
@@ -40,7 +40,7 @@ function startAdapter(options) {
const target = {}; const target = {};
let requestFields = {}; let requestFields = {};
if (typeof state.val === 'string') { if (typeof state.val === 'string' && state.val.startsWith('{') && state.val.endsWith('}')) {
try { try {
requestFields = JSON.parse(state.val) || {}; requestFields = JSON.parse(state.val) || {};
} catch (err) { } catch (err) {
@@ -49,6 +49,16 @@ function startAdapter(options) {
} }
} }
// 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) {
const fieldList = Object.keys(fields);
if (fieldList.length === 1 && fieldList[0] !== 'entity_id') {
requestFields[fieldList[0]] = state.val;
} else if (fieldList.length === 2 && fields.entity_id) {
requestFields[fieldList[1 - fields.indexOf('entity_id')]] = state.val;
}
}
for (const field in fields) { for (const field in fields) {
if (!fields.hasOwnProperty(field)) { if (!fields.hasOwnProperty(field)) {
continue; continue;