Skip to content

Commit

Permalink
Cannot read properties of null (reading 'hasOwnProperty')
Browse files Browse the repository at this point in the history
arteck committed Dec 30, 2023
1 parent 8d47a16 commit e3a6625
Showing 1 changed file with 55 additions and 48 deletions.
103 changes: 55 additions & 48 deletions lib/statescontroller.js
Original file line number Diff line number Diff line change
@@ -624,61 +624,68 @@ class StatesController extends EventEmitter {
}
// find states for payload
if (devStates.states !== undefined) {
const states = statesMapping.commonStates.concat(
devStates.states.filter(statedesc => payload.hasOwnProperty(statedesc.prop || statedesc.id)));

for (const stateInd in states) {
const statedesc = states[stateInd];
let value;
if (statedesc.getter) {
value = statedesc.getter(payload);
} else {
value = payload[statedesc.prop || statedesc.id];
}
// checking value
if (value === undefined || value === null) {
continue;
}
let stateID = statedesc.id;
try {
const states = statesMapping.commonStates.concat(
devStates.states.filter(statedesc => payload.hasOwnProperty(statedesc.prop || statedesc.id))
);

for (const stateInd in states) {
const statedesc = states[stateInd];
let value;
if (statedesc.getter) {
value = statedesc.getter(payload);
} else {
value = payload[statedesc.prop || statedesc.id];
}
// checking value
if (value === undefined || value === null) {
continue;
}

if (has_debug && statedesc.id !== 'msg_from_zigbee') {
this.warn(`ELEVATED publishToState: value generated '${JSON.stringify(value)}' from device ${devId} for '${statedesc.name}'`);
}
let stateID = statedesc.id;

const common = {
name: statedesc.name,
type: statedesc.type,
unit: statedesc.unit,
read: statedesc.read,
write: statedesc.write,
icon: statedesc.icon,
role: statedesc.role,
min: statedesc.min,
max: statedesc.max,
};

if (typeof value === 'object' && value.hasOwnProperty('stateid')) {
stateID = `${stateID}.${value.stateid}`;
if (value.hasOwnProperty('unit')) {
common.unit = value.unit;
if (has_debug && statedesc.id !== 'msg_from_zigbee') {
this.warn(`ELEVATED publishToState: value generated '${JSON.stringify(value)}' from device ${devId} for '${statedesc.name}'`);
}
common.name = value.name ? value.name : value.stateid;
common.role = value.role ? `value.${value.role}` : 'number';
value = value.value;
}

// if needs to return value to back after timeout
if (statedesc.isEvent) {
this.updateStateWithTimeout(devId, statedesc.id, value, common, 300, !value);
} else {
if (statedesc.prepublish) {
this.collectOptions(devId, model, options =>
statedesc.prepublish(devId, value, newvalue =>
this.updateState(devId, stateID, newvalue, common), options));
const common = {
name: statedesc.name,
type: statedesc.type,
unit: statedesc.unit,
read: statedesc.read,
write: statedesc.write,
icon: statedesc.icon,
role: statedesc.role,
min: statedesc.min,
max: statedesc.max,
};

if (typeof value === 'object' && value.hasOwnProperty('stateid')) {
stateID = `${stateID}.${value.stateid}`;
if (value.hasOwnProperty('unit')) {
common.unit = value.unit;
}
common.name = value.name ? value.name : value.stateid;
common.role = value.role ? `value.${value.role}` : 'number';
value = value.value;
}

// if needs to return value to back after timeout
if (statedesc.isEvent) {
this.updateStateWithTimeout(devId, statedesc.id, value, common, 300, !value);
} else {
this.updateState(devId, stateID, value, common);
if (statedesc.prepublish) {
this.collectOptions(devId, model, options =>
statedesc.prepublish(devId, value, newvalue =>
this.updateState(devId, stateID, newvalue, common), options)
);
} else {
this.updateState(devId, stateID, value, common);
}
}
}
} catch (e) {
this.debug(`No states in device ${devId} : payload ${JSON.stringify(payload)}`);
}
}
}

0 comments on commit e3a6625

Please sign in to comment.