Perform home assistant MQTT discovery on startup.

This commit is contained in:
Koen Kanters
2018-04-23 18:17:47 +02:00
parent 41b2fb3fc9
commit 500bce2418
4 changed files with 32 additions and 9 deletions
+20 -6
View File
@@ -23,7 +23,21 @@ class Controller {
if (error) {
logger.error('Failed to start');
} else {
this.mqtt.connect(this.handleMQTTMessage);
this.mqtt.connect(this.handleMQTTMessage, () => {
// Home assistant MQTT discovery on startup.
if (settings.get().homeassistant_discovery) {
const devices = this.zigbee.getAllClients();
devices.forEach((device) => {
const mappedModel = deviceMapping[device.modelId];
if (mappedModel && mappedModel.homeassistant && !this.hassDiscoveryCache[device.ieeeAddr]) {
this.homeassistantDiscover(mappedModel, device, settings.getDevice(device.ieeeAddr).friendly_name);
this.hassDiscoveryCache[device.ieeeAddr] = true;
}
});
}
});
}
});
}
@@ -49,7 +63,7 @@ class Controller {
const device = message.endpoints[0].device;
// Check if this is a new device.
if (!settings.get().devices[device.ieeeAddr]) {
if (!settings.getDevice(device.ieeeAddr)) {
logger.info(`New device with address ${device.ieeeAddr} connected!`);
settings.get().devices[device.ieeeAddr] = {
@@ -68,7 +82,7 @@ class Controller {
// Map Zigbee modelID to vendor modelID.
const modelID = message.endpoints[0].device.modelId;
const mappedModel = deviceMapping[modelID];
const friendlyName = settings.get().devices[device.ieeeAddr].friendly_name;
const friendlyName = settings.getDevice(device.ieeeAddr).friendly_name;
if (!mappedModel) {
logger.warn(`Device with modelID '${modelID}' is not supported.`);
@@ -99,7 +113,7 @@ class Controller {
}
// Convert this Zigbee message to a MQTT message.
const retain = settings.get().devices[device.ieeeAddr].retain;
const retain = settings.getDevice(device.ieeeAddr).retain;
const publish = (payload) => {
if (this.stateCache[device.ieeeAddr]) {
@@ -130,7 +144,7 @@ class Controller {
const friendlyName = topic.split('/')[1];
// Map friendlyName to deviceID.
const deviceID = Object.keys(settings.get().devices).find((id) => settings.get().devices[id].friendly_name === friendlyName);
const deviceID = Object.keys(settings.get().devices).find((id) => settings.getDevice(id).friendly_name === friendlyName);
if (!deviceID) {
logger.error(`Cannot handle '${topic}' because deviceID of '${friendlyName}' cannot be found`);
return;
@@ -153,7 +167,7 @@ class Controller {
this.mqtt.publish(
friendlyName,
JSON.stringify({state: json[key]}),
settings.get().devices[deviceID].retain,
settings.getDevice(deviceID).retain,
);
}
};
+6 -2
View File
@@ -10,7 +10,7 @@ class MQTT {
this.handleMessage = this.handleMessage.bind(this);
}
connect(onMessage) {
connect(onMessage, callback) {
const mqttSettings = settings.get().mqtt;
logger.info(`Connecting to MQTT server at ${mqttSettings.server}`);
@@ -23,7 +23,11 @@ class MQTT {
this.client = mqtt.connect(mqttSettings.server, options);
// Register callbacks.
this.client.on('connect', this.handleConnect);
this.client.on('connect', () => {
this.handleConnect();
callback();
});
this.client.on('message', this.handleMessage);
// Set timer at interval to check if connected to MQTT server.
+1
View File
@@ -21,4 +21,5 @@ function read() {
module.exports = {
get: () => settings,
write: () => write(),
getDevice: (id) => settings.devices[id],
}
+5 -1
View File
@@ -47,7 +47,7 @@ class Zigbee {
handleReady() {
logger.info('zigbee-shepherd ready');
const devices = this.shepherd.list().filter((device) => device.type !== 'Coordinator');
const devices = this.getAllClients();
logger.info(`Currently ${devices.length} devices are joined:`);
devices.forEach((device) => logger.info(this.getDeviceLogMessage(device)));
@@ -77,6 +77,10 @@ class Zigbee {
});
}
getAllClients() {
return this.shepherd.list().filter((device) => device.type !== 'Coordinator');
}
handleMessage(message) {
if (this.onMessage) {
this.onMessage(message);