Skip to content

Commit

Permalink
Load extensions from data directory. Koenkk#3297
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Apr 19, 2020
1 parent 11fd5ff commit a8793ab
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ data-backup/
data/coordinator_backup.json
data/.storage
data/database.db.backup
data/extension

# MacOS indexing file
.DS_Store
12 changes: 12 additions & 0 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const logger = require('./util/logger');
const settings = require('./util/settings');
const objectAssignDeep = require('object-assign-deep');
const utils = require('./util/utils');
const fs = require('fs');
const data = require('./util/data');
const path = require('path');

// Extensions
const ExtensionPublish = require('./extension/publish');
Expand Down Expand Up @@ -68,6 +71,15 @@ class Controller {
if (settings.get().advanced.availability_timeout) {
this.extensions.push(new ExtensionAvailability(...args));
}

const extensionPath = data.joinPath('extension');
if (fs.existsSync(extensionPath)) {
const extensions = fs.readdirSync(extensionPath);
for (const extension of extensions) {
const Extension = require(path.join(extensionPath, extension.split('.')[0]));
this.extensions.push(new Extension(...args));
}
}
}

async start() {
Expand Down
11 changes: 11 additions & 0 deletions test/assets/exampleExtension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Example {
constructor(zigbee, mqtt, state, publishEntityState, eventBus) {
this.mqtt = mqtt;
}

onMQTTConnected() {
this.mqtt.publish('example/extension', 'test')
}
}

module.exports = Example;
11 changes: 11 additions & 0 deletions test/controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,15 @@ describe('Controller', () => {
await flushPromises();
expect(controller.state.state).toStrictEqual({});
});

it('Load user extension', async () => {
const extensionPath = path.join(data.mockDir, 'extension');
fs.mkdirSync(extensionPath);
fs.copyFileSync(path.join(__dirname, 'assets', 'exampleExtension.js'), path.join(extensionPath, 'exampleExtension.js'))
controller = new Controller();
await controller.start();
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'test', { retain: false, qos: 0 }, expect.any(Function));
});

});

0 comments on commit a8793ab

Please sign in to comment.