* (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
+11 -1
View File
@@ -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;