Skip to content

Commit

Permalink
Switch: support single, double, triple, quadruple and long clicks. Ko…
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Apr 9, 2018
1 parent 990176c commit 67112d0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
21 changes: 15 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const debug = require('debug')('xiaomi-zb2mqtt')
const util = require("util");
const perfy = require('perfy');
const ZShepherd = require('zigbee-shepherd');
const mqtt = require('mqtt')
const fs = require('fs');
Expand Down Expand Up @@ -113,14 +112,24 @@ function handleMessage(msg) {
return;
}

// Parse the message.
// Parse generic information from message.
const friendlyName = settings.devices[device.ieeeAddr].friendly_name;
const payload = parser.parse(msg).toString();
const topic = `${settings.mqtt.base_topic}/${friendlyName}/${parser.topic}`;

// Send the message.
console.log(`MQTT publish, topic: '${topic}', payload: '${payload}'`);
client.publish(topic, payload);
// Define publih function.
const publish = (payload) => {
console.log(`MQTT publish, topic: '${topic}', payload: '${payload}'`);
client.publish(topic, payload.toString());
}

// Get payload for the message.
// - If a payload is returned publish it to the MQTT broker
// - If NO payload is returned do nothing. This is for non-standard behaviour
// for e.g. click switches where we need to count number of clicks and detect long presses.
const payload = parser.parse(msg, publish);
if (payload) {
publish(payload);
}
}

function handleQuit() {
Expand Down
36 changes: 34 additions & 2 deletions parsers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
const perfy = require('perfy');

const clickLookup = {
2: 'double',
3: 'triple',
4: 'quadruple',
}

module.exports = [
{
supportedDevices: [260],
description: 'WXKG01LM switch (260)',
topic: 'switch',
parse: (msg) => {
return msg.data.data['onOff'] === 0 ? 'on' : 'off';
parse: (msg, publish) => {
const deviceID = msg.endpoints[0].device.ieeeAddr;
const state = msg.data.data['onOff'];

// 0 = click down, 1 = click up, else = multiple clicks
if (state === 0) {
perfy.start(deviceID);
setTimeout(() => {
if (perfy.exists(deviceID)) {
publish('long');
perfy.end(deviceID);
}
}, 300); // After 300 seconds of not releasing we assume long click.
} else if (state === 1) {
if (perfy.exists(deviceID)) {
perfy.end(deviceID);
publish('single');
}
} else {
const clicks = msg.data.data['32768'];
if (clickLookup[clicks]) {
publish(clickLookup[clicks]);
} else {
publish('many');
}
}
}
},
]
Expand Down

0 comments on commit 67112d0

Please sign in to comment.