Skip to content

Commit 03d913d

Browse files
stas-demydiukKoenkk
authored andcommitted
Add ability to send device information in MQTT message payload (#374)
1 parent e4bcac5 commit 03d913d

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

.eslintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
},
55
"extends": ["eslint:recommended", "google"],
66
"parserOptions": {
7-
"ecmaVersion": 6,
7+
"ecmaVersion": 2018,
88
"sourceType": "module"
99
},
1010
"rules": {

lib/controller.js

+32-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const settings = require('./util/settings');
66
const ExtensionNetworkMap = require('./extension/networkMap');
77
const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
88
const homeassistant = require('./homeassistant');
9-
const objectAssignDeep = require(`object-assign-deep`);
9+
const objectAssignDeep = require('object-assign-deep');
1010

1111
const mqttConfigRegex = new RegExp(`${settings.get().mqtt.base_topic}/bridge/config/\\w+`, 'g');
1212
const mqttDeviceRegex = new RegExp(`${settings.get().mqtt.base_topic}/[\\w\\s\\d.-]+/set`, 'g');
@@ -107,7 +107,7 @@ class Controller {
107107
sendAllCachedStates() {
108108
this.zigbee.getAllClients().forEach((device) => {
109109
if (this.state.exists(device.ieeeAddr)) {
110-
this.mqttPublishDeviceState(device.ieeeAddr, this.state.get(device.ieeeAddr), false);
110+
this.mqttPublishDeviceState(device, this.state.get(device.ieeeAddr), false);
111111
}
112112
});
113113
}
@@ -216,6 +216,23 @@ class Controller {
216216
`${friendlyDevice.vendor} ${friendlyDevice.description} (${type})`;
217217
}
218218

219+
getDeviceInfoForMqtt(device) {
220+
const {type, ieeeAddr, nwkAddr, manufId, manufName, powerSource, modelId, status} = device;
221+
const deviceSettings = settings.getDevice(device.ieeeAddr);
222+
223+
return {
224+
ieeeAddr,
225+
friendlyName: deviceSettings.friendly_name || '',
226+
type,
227+
nwkAddr,
228+
manufId,
229+
manufName,
230+
powerSource,
231+
modelId,
232+
status,
233+
};
234+
}
235+
219236
handleZigbeeMessage(message) {
220237
// Zigbee message receieved, reset soft reset timeout.
221238
this.softResetTimeout(true);
@@ -337,7 +354,7 @@ class Controller {
337354
payload.linkquality = message.linkquality;
338355
}
339356

340-
this.mqttPublishDeviceState(device.ieeeAddr, payload, cache);
357+
this.mqttPublishDeviceState(device, payload, cache);
341358
};
342359

343360
const payload = converter.convert(mappedModel, message, publish, settings.getDevice(device.ieeeAddr));
@@ -536,7 +553,7 @@ class Controller {
536553
const msg = {};
537554
const _key = topicPrefix ? `${key}_${topicPrefix}` : key;
538555
msg[_key] = json[key];
539-
this.mqttPublishDeviceState(deviceID, msg, true);
556+
this.mqttPublishDeviceState(device, msg, true);
540557
}
541558
};
542559

@@ -575,11 +592,15 @@ class Controller {
575592
});
576593
}
577594

578-
mqttPublishDeviceState(deviceID, payload, cache) {
595+
mqttPublishDeviceState(device, payload, cache) {
596+
const deviceID = device.ieeeAddr;
597+
const appSettings = settings.get();
598+
let messagePayload = {...payload};
599+
579600
if (cacheState) {
580601
// Add cached state to payload
581602
if (this.state.exists(deviceID)) {
582-
payload = objectAssignDeep.noMutate(this.state.get(deviceID), payload);
603+
messagePayload = objectAssignDeep.noMutate(this.state.get(deviceID), payload);
583604
}
584605

585606
// Update state cache with new state.
@@ -594,7 +615,11 @@ class Controller {
594615
qos: deviceSettings.qos ? deviceSettings.qos : 0,
595616
};
596617

597-
this.mqtt.publish(deviceSettings.friendly_name, JSON.stringify(payload), options);
618+
if (appSettings.mqtt.include_device_information) {
619+
messagePayload.device = this.getDeviceInfoForMqtt(device);
620+
}
621+
622+
this.mqtt.publish(deviceSettings.friendly_name, JSON.stringify(messagePayload), options);
598623
}
599624

600625
startupLogVersion(callback) {

0 commit comments

Comments
 (0)