From 0ac6c66aaf166c9a532d2a9c6234ee6931bb938e Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Thu, 30 Jun 2022 11:51:23 +0200 Subject: [PATCH] * (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 --- README.md | 4 ++++ main.js | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b165f6e..1226b3f 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,10 @@ Please check it https://www.smarthomejetzt.de/mit-iobroker-auf-eine-home-assista --> ## 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) * (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 diff --git a/main.js b/main.js index 100d24d..2d90f18 100644 --- a/main.js +++ b/main.js @@ -40,7 +40,7 @@ function startAdapter(options) { const target = {}; let requestFields = {}; - if (typeof state.val === 'string') { + if (typeof state.val === 'string' && state.val.startsWith('{') && state.val.endsWith('}')) { try { requestFields = JSON.parse(state.val) || {}; } 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) { if (!fields.hasOwnProperty(field)) { continue;