Skip to content

Commit

Permalink
Update last_seen for all received Zigbee messages (not only handled o…
Browse files Browse the repository at this point in the history
…nes). #7423
  • Loading branch information
Koenkk committed Jul 3, 2021
1 parent 82767a2 commit 96657e1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,14 @@ class Controller {
}

// Call extensions
this.callExtensionMethod('onZigbeeEvent', [type, data, resolvedEntity]);
const result = await this.callExtensionMethod('onZigbeeEvent', [type, data, resolvedEntity]);

// In case this message is not handled by the receive extension and last_seen is enabled, publish
// a message to update the last_seen.
if (name && !result[ExtensionReceive.prototype.constructor.name] &&
settings.get().advanced.last_seen !== 'disable' && type !== 'deviceLeave') {
this.publishEntityState(name, {});
}
}

onMQTTMessage(payload) {
Expand Down Expand Up @@ -391,10 +398,11 @@ class Controller {
}

async callExtensionMethod(method, parameters, extensions=null) {
const result = {};
for (const extension of extensions || this.extensions) {
if (extension[method]) {
try {
await extension[method](...parameters);
result[extension.constructor.name] = await extension[method](...parameters);
} catch (error) {
/* istanbul ignore next */
logger.error(`Failed to call '${extension.constructor.name}' '${method}' (${error.stack})`);
Expand All @@ -405,6 +413,7 @@ class Controller {
}
}
}
return result;
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/extension/receive.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class Receive extends Extension {

if (Object.keys(payload).length) {
publish(payload);
return true;
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/receive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,20 @@ describe('Receive', () => {
expect(MQTT.publish).toHaveBeenCalledTimes(0);
});

it('Should publish last_seen for unhandled messages', async () => {
const device = zigbeeHerdsman.devices.WXKG02LM_rev1;
settings.set(['advanced', 'last_seen'], 'epoch');
const data = {onOff: 1};
const payload = {data, cluster: 'genRssiLocation', device, endpoint: device.getEndpoint(1), type: 'attributeReport', linkquality: 10};
await zigbeeHerdsman.events.message(payload);
await flushPromises();
console.log(MQTT.publish.mock.calls);
expect(MQTT.publish).toHaveBeenCalledTimes(1);
expect(MQTT.publish.mock.calls[0][0]).toStrictEqual('zigbee2mqtt/button_double_key');
expect(typeof JSON.parse(MQTT.publish.mock.calls[0][1]).last_seen).toBe('number')
expect(MQTT.publish.mock.calls[0][2]).toStrictEqual({"qos": 0, "retain": false});
});

it('Should publish last_seen epoch', async () => {
const device = zigbeeHerdsman.devices.WXKG02LM_rev1;
settings.set(['advanced', 'last_seen'], 'epoch');
Expand Down

0 comments on commit 96657e1

Please sign in to comment.