Skip to content

Commit

Permalink
Merge pull request #59 from o0shojo0o/dev_ActionIsNotBoolean
Browse files Browse the repository at this point in the history
 action is not boolean
  • Loading branch information
o0shojo0o authored Nov 6, 2022
2 parents d33997f + 796c642 commit ab19321
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
2 changes: 2 additions & 0 deletions lib/exposes.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ function createFromExposes(deviceID, ieee_address, definitions, power_source, sc
min: config.useKelvin == true ? utils.miredKelvinConversion(500) : 150,
max: config.useKelvin == true ? utils.miredKelvinConversion(150) : 500,
unit: config.useKelvin == true ? 'K' : 'mired',
isEvent: true,
getter: (payload) => {
if (payload.action != 'color_temperature_move') {
return undefined;
Expand Down Expand Up @@ -671,6 +672,7 @@ function createFromExposes(deviceID, ieee_address, definitions, power_source, sc
read: true,
type: 'string',
def: '#ffffff',
isEvent: true,
getter: (payload) => {
if (payload.action != 'color_move') {
return undefined;
Expand Down
62 changes: 43 additions & 19 deletions lib/statesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,47 @@ class StatesController {
this.adapter.log.warn(`--->>> fromZ2M -> ${device.ieee_address} states: ${JSON.stringify(messageObj)}`);
}

for (const [key, value] of Object.entries(messageObj.payload)) {
let states;
if (key == 'action') {
states = device.states.filter(x => (x.prop && x.prop == key));
} else {
states = device.states.filter(x => (x.prop && x.prop == key) || x.id == key);
}
// Is an action
if (Object.keys(messageObj.payload).includes('action')) {
const states = device.states.filter(x => (x.prop && x.prop == 'action'));

for (const state of states) {
const stateName = `${device.ieee_address}.${state.id}`;

// It may be that the state has not yet been created!
if (!this.createCache[device.ieee_address] || !this.createCache[device.ieee_address][state.id] || !this.createCache[device.ieee_address][state.id].created) {
incStatsQueue[incStatsQueue.length] = messageObj;
continue;
}

if (!state.getter) {
this.adapter.log.error(`Action ${stateName} has no getter, this must not be!`);
continue;
}

try {
if (state.isEvent && state.isEvent == true) {
if (state.type == 'boolean') {
await this.setStateWithTimeoutAsync(stateName, state.getter(messageObj.payload), 300);
}
else {
await this.setStateSafelyAsync(stateName, state.getter(messageObj.payload));
}
}
else {
await this.setStateChangedSafelyAsync(stateName, state.getter(messageObj.payload));
}
} catch (err) {
incStatsQueue[incStatsQueue.length] = messageObj;
this.adapter.log.debug(`Can not set ${stateName}, queue state in incStatsQueue!`);
}
}
}
// Is not an action
else {
for (const [key, value] of Object.entries(messageObj.payload)) {
const state = device.states.find(x => (x.prop && x.prop == key) || x.id == key);

if (!state) {
continue;
}
Expand All @@ -53,28 +85,20 @@ class StatesController {
// It may be that the state has not yet been created!
if (!this.createCache[device.ieee_address] || !this.createCache[device.ieee_address][state.id] || !this.createCache[device.ieee_address][state.id].created) {
incStatsQueue[incStatsQueue.length] = messageObj;
//this.adapter.log.debug(`State ${stateName} is not yet created, queue state in incStatsQueue!`);
continue;
}

try {
if (state.isEvent) {
if (state.getter) {
await this.setStateWithTimeoutAsync(stateName, state.getter(messageObj.payload), 300);
} else {
await this.setStateWithTimeoutAsync(stateName, value, 300);
}
if (state.getter) {
await this.setStateChangedSafelyAsync(stateName, state.getter(messageObj.payload));
} else {
if (state.getter) {
await this.setStateChangedSafelyAsync(stateName, state.getter(messageObj.payload));
} else {
await this.setStateChangedSafelyAsync(stateName, value);
}
await this.setStateChangedSafelyAsync(stateName, value);
}
} catch (err) {
incStatsQueue[incStatsQueue.length] = messageObj;
this.adapter.log.debug(`Can not set ${stateName}, queue state in incStatsQueue!`);
}

}
}
}
Expand Down

0 comments on commit ab19321

Please sign in to comment.