Skip to content

Commit

Permalink
Fixes & improvements. #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Apr 18, 2018
1 parent dec470f commit 3b31753
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
11 changes: 5 additions & 6 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ class Controller {
const mappedModel = deviceMapping[modelID];

if (!mappedModel) {
logger.error(`Device with modelID '${modelID}' is not supported.`);
logger.error('Please create an issue on https://github.com/Koenkk/zigbee2mqtt/issues to add support for your device');
logger.warn(`Device with modelID '${modelID}' is not supported.`);
logger.warn('Please create an issue on https://github.com/Koenkk/zigbee2mqtt/issues to add support for your device');
return;
}

Expand All @@ -72,22 +72,21 @@ class Controller {
const converters = zigbee2mqtt.filter((c) => c.devices.includes(mappedModel.model) && c.cid === cid && c.type === message.type);

if (!converters.length) {
logger.error(`No converter available for '${mappedModel.model}' with cid '${cid}' and type '${message.type}'`);
logger.error('Please create an issue on https://github.com/Koenkk/zigbee2mqtt/issues with this message.');
logger.warn(`No converter available for '${mappedModel.model}' with cid '${cid}' and type '${message.type}'`);
logger.warn('Please create an issue on https://github.com/Koenkk/zigbee2mqtt/issues with this message.');
return;
}

// Convert this Zigbee message to a MQTT message.
const friendlyName = settings.get().devices[device.ieeeAddr].friendly_name;
const retain = settings.get().devices[device.ieeeAddr].retain;
const topic = `${settings.get().mqtt.base_topic}/${friendlyName}`;

const publish = (payload) => {
if (this.stateCache[device.ieeeAddr]) {
payload = {...this.stateCache[device.ieeeAddr], ...payload};
}

this.mqtt.publish(topic, JSON.stringify(payload), retain);
this.mqtt.publish(friendlyName, JSON.stringify(payload), retain);
}

// Get payload for the message.
Expand Down
12 changes: 6 additions & 6 deletions lib/mqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ class MQTT {
clearTimeout(this.connectionTimer);
this.connectionTimer = null;

this.publish('bridge/state', 'offline', true);

logger.info('Disconnecting from MQTT server');
this.client.end();
this.publish('bridge/state', 'offline', true, () => {
logger.info('Disconnecting from MQTT server');
this.client.end();
});
}

handleConnect() {
Expand All @@ -59,7 +59,7 @@ class MQTT {
}
}

publish(topic, payload, retain) {
publish(topic, payload, retain, callback) {
topic = `${settings.get().mqtt.base_topic}/${topic}`;

if (!this.client || this.client.reconnecting) {
Expand All @@ -69,7 +69,7 @@ class MQTT {
}

logger.info(`MQTT publish, topic: '${topic}', payload: '${payload}'`);
this.client.publish(topic, payload, {retain: retain});
this.client.publish(topic, payload, {retain: retain}, callback);
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/util/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ function read() {

module.exports = {
get: () => settings,
write: () => write(),
}
11 changes: 3 additions & 8 deletions lib/zigbee.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ class Zigbee {

stop(callback) {
this.shepherd.stop((error) => {
if (error) {
logger.error('Error while stopping zigbee-shepherd');
} else {
logger.error('zigbee-shepherd stopped')
}

logger.info('zigbee-shepherd stopped')
callback(error);
});
}
Expand Down Expand Up @@ -97,7 +92,7 @@ class Zigbee {
}

if (settings.get().devices[device.ieeeAddr]) {
friendlyName = settings.devices[device.ieeeAddr].friendly_name
friendlyName = settings.get().devices[device.ieeeAddr].friendly_name
}

return `${friendlyName} (${device.ieeeAddr}): ${friendlyDevice.model} - ${friendlyDevice.description}`;
Expand All @@ -110,7 +105,7 @@ class Zigbee {
logger.error(`Zigbee cannot publish message to device because '${deviceID}' is not known by zigbee-shepherd`);
}

logger.info(`Zigbee publish to '${deviceID}', ${cId} - ${cmd} - ${zclData}`);
logger.info(`Zigbee publish to '${deviceID}', ${cId} - ${cmd} - ${JSON.stringify(zclData)}`);
device.functional(cId, cmd, zclData, callback);
}
}
Expand Down

0 comments on commit 3b31753

Please sign in to comment.