* (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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user