Skip to content

Commit f017166

Browse files
committed
Add option to deactivate probing and specify stick manually (#308)
1 parent 31e3a7e commit f017166

File tree

2 files changed

+116
-54
lines changed

2 files changed

+116
-54
lines changed

manifest.json

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"allowFTDISerial": false,
2222
"allowAMASerial": false,
2323
"showAging": false,
24-
"debug": ""
24+
"debug": "",
25+
"probing": true
2526
},
2627
"schema": {
2728
"type": "object",
@@ -48,6 +49,28 @@
4849
"title": "Show Aging",
4950
"description": "experimental - Creates an additional 'Last seen' property to show when each device was last active on the Zigbee network"
5051
},
52+
"probing": {
53+
"type": "boolean",
54+
"title": "Automatic probing of the serial ports"
55+
},
56+
"sticks": {
57+
"type": "array",
58+
"title": "List of ZigBee sticks to use",
59+
"items": {
60+
"type": "object",
61+
"title": "ZigBee Stick",
62+
"required": ["type", "port"],
63+
"properties": {
64+
"type": {
65+
"type": "string",
66+
"enum": ["xbee", "conbee", "zstack"]
67+
},
68+
"port": {
69+
"type": "string"
70+
}
71+
}
72+
}
73+
},
5174
"zigbee2mqtt": {
5275
"title": "Zigbee2Mqtt",
5376
"type": "object",

src/index.js

Lines changed: 92 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const { Database } = require('gateway-addon');
1313
const manifest = require('./manifest.json');
1414
const SerialProber = require('serial-prober');
1515
const { Zigbee2MqttDriver } = require('./zigbee2mqtt/zigbee2mqtt-driver');
16+
const SerialPort = require('serialport');
1617

1718
const XBEE_FTDI_FILTER = {
1819
// Devices like the UartSBee, use a generic FTDI chip and with
@@ -225,62 +226,100 @@ async function loadZigbeeAdapters(addonManager, _, errorCallback) {
225226
zigbee2mqttConfigured = true;
226227
}
227228

228-
const { DEBUG_serialProber } = require('./zb-debug').default;
229-
SerialProber.debug(DEBUG_serialProber);
230-
if (allowFTDISerial) {
231-
xbeeSerialProber.param.filter.push(XBEE_FTDI_FILTER);
232-
}
233-
if (allowAMASerial) {
234-
conbeeSerialProber.param.allowAMASerial = true;
235-
}
236-
SerialProber.probeAll(PROBERS)
237-
.then((matches) => {
238-
if (matches.length == 0) {
239-
SerialProber.listAll()
240-
.then(() => {
241-
if (!zigbee2mqttConfigured) {
242-
errorCallback(manifest.id, 'No Zigbee dongle found');
243-
} else {
244-
console.debug('No Zigbee dongle found');
245-
}
246-
})
247-
.catch((err) => {
248-
if (!zigbee2mqttConfigured) {
249-
errorCallback(manifest.id, err);
250-
} else {
251-
console.debug(`Could not probe serial ports: ${err}`);
252-
}
253-
});
254-
return;
229+
for (const stick of config.sticks || []) {
230+
console.log(`Creating ${stick.type} driver for ${stick.port}`);
231+
232+
switch (stick.type) {
233+
case 'xbee': {
234+
const XBeeDriver = require('./driver/xbee');
235+
const serialPort = new SerialPort(stick.port, {
236+
baudRate: 9600,
237+
lock: true,
238+
});
239+
new XBeeDriver(addonManager, config, stick.port, serialPort);
240+
break;
255241
}
256-
// We put the driver requires here rather than at the top of
257-
// the file so that the debug config gets initialized before we
258-
// import the driver class.
259-
const XBeeDriver = require('./driver/xbee');
260-
const ConBeeDriver = require('./driver/conbee');
261-
const ZStackDriver = require('./driver/zstack');
262-
const driver = {
263-
[xbeeSerialProber.param.name]: XBeeDriver,
264-
[conbeeSerialProber.param.name]: ConBeeDriver,
265-
[cc2531SerialProber.param.name]: ZStackDriver,
266-
[conbeeNewerFirmwareSerialProber.param.name]: ConBeeDriver,
267-
};
268-
for (const match of matches) {
269-
new driver[match.prober.param.name](
270-
addonManager,
271-
config,
272-
match.port.path,
273-
match.serialPort
274-
);
242+
case 'conbee': {
243+
const ConBeeDriver = require('./driver/conbee');
244+
const serialPort = new SerialPort(stick.port, {
245+
baudRate: 38400,
246+
lock: true,
247+
});
248+
new ConBeeDriver(addonManager, config, stick.port, serialPort);
249+
break;
275250
}
276-
})
277-
.catch((err) => {
278-
if (!zigbee2mqttConfigured) {
279-
errorCallback(manifest.id, err);
280-
} else {
281-
console.debug(`Could not load serial drivers: ${err}`);
251+
case 'zstack': {
252+
const ZStackDriver = require('./driver/zstack');
253+
const serialPort = new SerialPort(stick.port, {
254+
baudRate: 115200,
255+
lock: true,
256+
});
257+
new ZStackDriver(addonManager, config, stick.port, serialPort);
258+
break;
282259
}
283-
});
260+
}
261+
}
262+
263+
if (config.probing) {
264+
console.log('Probing serial ports');
265+
266+
const { DEBUG_serialProber } = require('./zb-debug').default;
267+
SerialProber.debug(DEBUG_serialProber);
268+
if (allowFTDISerial) {
269+
xbeeSerialProber.param.filter.push(XBEE_FTDI_FILTER);
270+
}
271+
if (allowAMASerial) {
272+
conbeeSerialProber.param.allowAMASerial = true;
273+
}
274+
SerialProber.probeAll(PROBERS)
275+
.then((matches) => {
276+
if (matches.length == 0) {
277+
SerialProber.listAll()
278+
.then(() => {
279+
if (!zigbee2mqttConfigured) {
280+
errorCallback(manifest.id, 'No Zigbee dongle found');
281+
} else {
282+
console.debug('No Zigbee dongle found');
283+
}
284+
})
285+
.catch((err) => {
286+
if (!zigbee2mqttConfigured) {
287+
errorCallback(manifest.id, err);
288+
} else {
289+
console.debug(`Could not probe serial ports: ${err}`);
290+
}
291+
});
292+
return;
293+
}
294+
// We put the driver requires here rather than at the top of
295+
// the file so that the debug config gets initialized before we
296+
// import the driver class.
297+
const XBeeDriver = require('./driver/xbee');
298+
const ConBeeDriver = require('./driver/conbee');
299+
const ZStackDriver = require('./driver/zstack');
300+
const driver = {
301+
[xbeeSerialProber.param.name]: XBeeDriver,
302+
[conbeeSerialProber.param.name]: ConBeeDriver,
303+
[cc2531SerialProber.param.name]: ZStackDriver,
304+
[conbeeNewerFirmwareSerialProber.param.name]: ConBeeDriver,
305+
};
306+
for (const match of matches) {
307+
new driver[match.prober.param.name](
308+
addonManager,
309+
config,
310+
match.port.path,
311+
match.serialPort
312+
);
313+
}
314+
})
315+
.catch((err) => {
316+
if (!zigbee2mqttConfigured) {
317+
errorCallback(manifest.id, err);
318+
} else {
319+
console.debug(`Could not load serial drivers: ${err}`);
320+
}
321+
});
322+
}
284323

285324
new Zigbee2MqttDriver(addonManager, config);
286325
}

0 commit comments

Comments
 (0)