Publish and respond to bridge/config/devices/get (#1329)

* - When sent a empty payload to bridge/config/devices/get , publish to
  'bridge/config/devices'
- For bridge/config/devices & bridge/config/devices/get only ever
  publish to the corresponding topic if a empty payload is sent.
- Make sure devicesPublish doesn't treat messages to
  bridge/config/devices/get as a zigbee device.

* fix eslinting errors

* Update bridgeConfig.js
This commit is contained in:
Justin Hornosty
2019-03-26 20:40:50 +01:00
committed by Koen Kanters
parent 2328ecb2ee
commit 25408769e6
3 changed files with 32 additions and 4 deletions
+8 -3
View File
@@ -3,7 +3,7 @@ const logger = require('../util/logger');
const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
const utils = require('../util/utils');
const configRegex = new RegExp(`${settings.get().mqtt.base_topic}/bridge/config/\\w+`, 'g');
const configRegex = new RegExp(`${settings.get().mqtt.base_topic}/bridge/config/((?:\\w+/get)|(?:\\w+))`);
const allowedLogLevels = ['error', 'warn', 'info', 'debug'];
class BridgeConfig {
@@ -37,6 +37,7 @@ class BridgeConfig {
'log_level': this.logLevel,
'devices': this.devices,
'groups': this.groups,
'devices/get': this.devices,
'rename': this.rename,
'remove': this.remove,
'ban': this.ban,
@@ -147,7 +148,11 @@ class BridgeConfig {
return payload;
});
this.mqtt.log('devices', devices);
if (topic.split('/').pop() == 'get') {
this.mqtt.publish(`bridge/config/devices`, JSON.stringify(devices), {});
} else {
this.mqtt.log('devices', devices);
}
}
groups(topic, message) {
@@ -244,7 +249,7 @@ class BridgeConfig {
return false;
}
const option = topic.split('/').slice(-1)[0];
const option = topic.match(configRegex)[1];
if (!this.supportedOptions.hasOwnProperty(option)) {
return false;
+3 -1
View File
@@ -69,7 +69,9 @@ class DevicePublish {
// Remove base from topic
topic = topic.replace(`${settings.get().mqtt.base_topic}/`, '');
if (topic.match(/bridge\/config/)) {
return null;
}
// Parse type from topic
const type = topic.substr(topic.lastIndexOf('/') + 1, topic.length);
+21
View File
@@ -657,6 +657,27 @@ describe('DevicePublish', () => {
expect(parsed.postfix).toBe('');
});
it('Should not respond to bridge/config/devices/get', () => {
const topic = 'zigbee2mqtt/bridge/config/devices/get';
const parsed = devicePublish.parseTopic(topic);
expect(parsed).toBeNull();
});
it('Should not respond to bridge/config/devices/set', () => {
const topic = 'zigbee2mqtt/bridge/config/devices/set';
const parsed = devicePublish.parseTopic(topic);
expect(parsed).toBeNull();
});
it('Should not respond to bridge/config/devices', () => {
const topic = 'zigbee2mqtt/bridge/config/devices';
const parsed = devicePublish.parseTopic(topic);
expect(parsed).toBeNull();
});
it('Should parse topic with when base topic has multiple slashes', () => {
jest.spyOn(settings, 'get').mockReturnValue({
mqtt: {