2018-10-23 11:39:48 -07:00
|
|
|
|
|
|
|
const settings = require('../util/settings');
|
|
|
|
const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
|
|
|
|
const logger = require('../util/logger');
|
2019-03-15 13:19:42 -07:00
|
|
|
const utils = require('../util/utils');
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2019-03-15 13:19:42 -07:00
|
|
|
const postfixes = utils.getPostfixes();
|
2019-04-07 08:51:27 -07:00
|
|
|
const topicRegex = new RegExp(`^(.+?)(?:/(${postfixes.join('|')}))?/(get|set)(?:/(.+))?`);
|
2019-03-15 13:19:42 -07:00
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
const maxDepth = 20;
|
2018-10-23 11:39:48 -07:00
|
|
|
|
2018-12-21 16:07:53 -07:00
|
|
|
const groupConverters = [
|
2019-03-30 12:50:40 -07:00
|
|
|
zigbeeShepherdConverters.toZigbeeConverters.light_onoff_brightness,
|
|
|
|
zigbeeShepherdConverters.toZigbeeConverters.light_colortemp,
|
|
|
|
zigbeeShepherdConverters.toZigbeeConverters.light_color,
|
2019-07-08 12:06:08 -07:00
|
|
|
zigbeeShepherdConverters.toZigbeeConverters.light_alert,
|
2019-03-30 12:50:40 -07:00
|
|
|
zigbeeShepherdConverters.toZigbeeConverters.ignore_transition,
|
2019-02-14 10:13:51 -07:00
|
|
|
];
|
|
|
|
|
2018-11-05 13:55:30 -07:00
|
|
|
class DevicePublish {
|
2019-02-04 10:36:49 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishEntityState) {
|
2018-10-23 11:39:48 -07:00
|
|
|
this.zigbee = zigbee;
|
|
|
|
this.mqtt = mqtt;
|
|
|
|
this.state = state;
|
2019-02-04 10:36:49 -07:00
|
|
|
this.publishEntityState = publishEntityState;
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
2018-10-23 11:39:48 -07:00
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
onMQTTConnected() {
|
2018-10-23 11:39:48 -07:00
|
|
|
// Subscribe to topics.
|
2019-04-07 08:51:27 -07:00
|
|
|
const baseTopic = settings.get().mqtt.base_topic;
|
2018-11-07 11:40:58 -07:00
|
|
|
for (let step = 1; step < maxDepth; step++) {
|
2019-04-07 08:51:27 -07:00
|
|
|
const topic = `${baseTopic}/${'+/'.repeat(step)}`;
|
2018-11-07 11:40:58 -07:00
|
|
|
this.mqtt.subscribe(`${topic}set`);
|
2019-04-07 08:51:27 -07:00
|
|
|
this.mqtt.subscribe(`${topic}set/+`);
|
2018-11-07 11:40:58 -07:00
|
|
|
this.mqtt.subscribe(`${topic}get`);
|
2019-04-07 08:51:27 -07:00
|
|
|
this.mqtt.subscribe(`${topic}get/+`);
|
2018-11-07 11:40:58 -07:00
|
|
|
}
|
2018-10-23 11:39:48 -07:00
|
|
|
}
|
|
|
|
|
2018-11-07 11:40:58 -07:00
|
|
|
parseTopic(topic) {
|
2019-04-07 08:51:27 -07:00
|
|
|
const match = topic.match(topicRegex);
|
|
|
|
if (!match) {
|
2018-11-07 11:40:58 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-07 08:51:27 -07:00
|
|
|
const ID = match[1].replace(`${settings.get().mqtt.base_topic}/`, '');
|
|
|
|
// If we didn't repalce base_topic we received something we don't care about
|
|
|
|
if (ID === match[1] || ID.match(/bridge\/config/)) {
|
2019-03-26 12:24:03 -07:00
|
|
|
return null;
|
|
|
|
}
|
2018-11-07 11:40:58 -07:00
|
|
|
|
2019-04-07 08:51:27 -07:00
|
|
|
return {ID: ID, postfix: match[2] || '', type: match[3], attribute: match[4]};
|
2018-11-07 11:40:58 -07:00
|
|
|
}
|
|
|
|
|
2019-02-20 09:57:28 -07:00
|
|
|
handlePublishError(entity, message, error) {
|
|
|
|
const meta = {
|
|
|
|
entity,
|
|
|
|
message: message.toString(),
|
|
|
|
};
|
|
|
|
|
|
|
|
this.mqtt.log('zigbee_publish_error', error.toString(), meta);
|
|
|
|
}
|
|
|
|
|
2019-03-19 11:42:30 -07:00
|
|
|
handlePublished(entity, topic, converter, converted, key, value) {
|
2019-03-30 12:50:40 -07:00
|
|
|
if (topic.type === 'set' && converted.hasOwnProperty('newState') && converted.newState) {
|
|
|
|
const msg = converted.newState;
|
2019-02-14 10:13:51 -07:00
|
|
|
|
2019-03-30 12:50:40 -07:00
|
|
|
if (topic.postfix) {
|
|
|
|
msg[`state_${topic.postfix}`] = msg.state;
|
|
|
|
delete msg.state;
|
2019-02-14 10:13:51 -07:00
|
|
|
}
|
2019-03-30 12:50:40 -07:00
|
|
|
|
2019-06-24 11:16:03 -07:00
|
|
|
if (settings.get().advanced.last_seen !== 'disable') {
|
|
|
|
msg.last_seen = utils.formatDate(Date.now(), settings.get().advanced.last_seen);
|
|
|
|
}
|
|
|
|
|
2019-03-30 12:50:40 -07:00
|
|
|
this.publishEntityState(entity.ID, msg);
|
2019-02-14 10:13:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
onMQTTMessage(topic, message) {
|
2018-11-07 11:40:58 -07:00
|
|
|
topic = this.parseTopic(topic);
|
|
|
|
|
|
|
|
if (!topic) {
|
2018-10-23 11:39:48 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-27 10:43:34 -07:00
|
|
|
// Resolve the entity
|
2019-03-08 08:10:28 -07:00
|
|
|
const entity = settings.resolveEntity(topic.ID);
|
2018-10-23 11:39:48 -07:00
|
|
|
|
2018-12-21 16:07:53 -07:00
|
|
|
// Get entity details
|
|
|
|
let endpoint = null;
|
|
|
|
let converters = null;
|
|
|
|
let device = null;
|
2019-06-19 12:44:44 -07:00
|
|
|
let options = {};
|
2018-12-21 16:07:53 -07:00
|
|
|
|
2018-12-27 10:43:34 -07:00
|
|
|
if (entity.type === 'device') {
|
|
|
|
device = this.zigbee.getDevice(entity.ID);
|
2018-12-21 16:07:53 -07:00
|
|
|
if (!device) {
|
2019-05-12 12:03:27 -07:00
|
|
|
logger.error(`Failed to find device with ieeeAddr: '${entity.ID}'`);
|
2019-02-22 04:49:08 -07:00
|
|
|
this.mqtt.log('entity_not_found', entity.ID);
|
2018-12-21 16:07:53 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map device to a model
|
|
|
|
const model = zigbeeShepherdConverters.findByZigbeeModel(device.modelId);
|
|
|
|
if (!model) {
|
|
|
|
logger.warn(`Device with modelID '${device.modelId}' is not supported.`);
|
2019-02-12 13:39:37 -07:00
|
|
|
logger.warn(`Please see: https://www.zigbee2mqtt.io/how_tos/how_to_support_new_devices.html`);
|
2018-12-21 16:07:53 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine endpoint to publish to.
|
|
|
|
if (model.hasOwnProperty('ep')) {
|
|
|
|
const eps = model.ep(device);
|
|
|
|
endpoint = eps.hasOwnProperty(topic.postfix) ? eps[topic.postfix] : null;
|
2019-02-18 09:57:57 -07:00
|
|
|
if (endpoint === null && eps.hasOwnProperty('default')) {
|
|
|
|
endpoint = eps['default'];
|
|
|
|
}
|
2018-12-21 16:07:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
converters = model.toZigbee;
|
2019-06-19 12:44:44 -07:00
|
|
|
options = model.options || {};
|
2018-12-27 10:43:34 -07:00
|
|
|
} else if (entity.type === 'group') {
|
2019-03-30 12:50:40 -07:00
|
|
|
converters = groupConverters;
|
2018-10-23 11:39:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the MQTT message to a Zigbee message.
|
2019-04-07 08:51:27 -07:00
|
|
|
let json = {};
|
|
|
|
if (topic.hasOwnProperty('attribute') && topic.attribute) {
|
|
|
|
json[topic.attribute] = message.toString();
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
json = JSON.parse(message);
|
|
|
|
} catch (e) {
|
|
|
|
// Cannot be parsed to JSON, assume state message.
|
|
|
|
json = {state: message.toString()};
|
|
|
|
}
|
2018-10-23 11:39:48 -07:00
|
|
|
}
|
|
|
|
|
2019-03-30 10:19:10 -07:00
|
|
|
/**
|
|
|
|
* Home Assistant always publishes 'state', even when e.g. only setting
|
|
|
|
* the color temperature. This would lead to 2 zigbee publishes, where the first one
|
|
|
|
* (state) is probably unecessary.
|
|
|
|
*/
|
2019-05-02 11:14:44 -07:00
|
|
|
if (settings.get().homeassistant) {
|
2019-03-30 10:19:10 -07:00
|
|
|
const deviceState = this.state.get(entity.ID);
|
|
|
|
const hasColorTemp = json.hasOwnProperty('color_temp');
|
|
|
|
const hasColor = json.hasOwnProperty('color');
|
|
|
|
const hasBrightness = json.hasOwnProperty('brightness');
|
|
|
|
const isOn = deviceState && deviceState.state === 'ON' ? true : false;
|
|
|
|
if (isOn && (hasColorTemp || hasColor) && !hasBrightness) {
|
|
|
|
delete json.state;
|
|
|
|
logger.debug('Skipping state because of Home Assistant');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 09:38:06 -07:00
|
|
|
// Ensure that state and brightness are executed before other commands.
|
|
|
|
const keys = Object.keys(json);
|
|
|
|
keys.sort((a, b) => (['state', 'brightness'].includes(a) ? -1 : 1));
|
|
|
|
|
2018-10-23 11:39:48 -07:00
|
|
|
// For each key in the JSON message find the matching converter.
|
2019-03-02 08:35:00 -07:00
|
|
|
const usedConverters = [];
|
2019-01-21 09:38:06 -07:00
|
|
|
keys.forEach((key) => {
|
2018-12-21 16:07:53 -07:00
|
|
|
const converter = converters.find((c) => c.key.includes(key));
|
2019-03-02 08:35:00 -07:00
|
|
|
|
|
|
|
if (usedConverters.includes(converter)) {
|
|
|
|
// Use a converter only once (e.g. light_onoff_brightness converters can convert state and brightness)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-23 11:39:48 -07:00
|
|
|
if (!converter) {
|
|
|
|
logger.error(`No converter available for '${key}' (${json[key]})`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converter didn't return a result, skip
|
2019-06-19 12:44:44 -07:00
|
|
|
const convertedResults = converter.convert(key, json[key], json, topic.type, topic.postfix, options);
|
2019-05-01 12:50:01 -07:00
|
|
|
if (!convertedResults || !convertedResults.length) {
|
2018-10-23 11:39:48 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-01 12:50:01 -07:00
|
|
|
convertedResults.forEach((converted) => {
|
|
|
|
this.zigbee.publish(
|
|
|
|
entity.ID,
|
|
|
|
entity.type,
|
|
|
|
converted.cid,
|
|
|
|
converted.cmd,
|
|
|
|
converted.cmdType,
|
|
|
|
converted.zclData,
|
|
|
|
converted.cfg,
|
|
|
|
endpoint,
|
|
|
|
(error, rsp) => {
|
|
|
|
if (!error) {
|
|
|
|
this.handlePublished(entity, topic, converter, converted, key, json[key]);
|
|
|
|
} else {
|
|
|
|
this.handlePublishError(entity, message, error);
|
|
|
|
}
|
2018-10-23 11:39:48 -07:00
|
|
|
}
|
2019-05-01 12:50:01 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
// It's possible for devices to get out of sync when writing an attribute that's not reportable.
|
|
|
|
// So here we re-read the value after a specified timeout, this timeout could for example be the
|
|
|
|
// transition time of a color change or for forcing a state read for devices that don't
|
|
|
|
// automatically report a new state when set.
|
|
|
|
// When reporting is requested for a device (report: true in device-specific settings) we won't
|
|
|
|
// ever issue a read here, as we assume the device will properly report changes.
|
|
|
|
// Only do this when the retrieve_state option is enabled for this device.
|
|
|
|
const deviceSettings = settings.getDevice(entity.ID);
|
|
|
|
if (topic.type === 'set' && entity.type === 'device' &&
|
|
|
|
converted.hasOwnProperty('readAfterWriteTime') && deviceSettings && deviceSettings.retrieve_state) {
|
2019-06-27 10:11:21 -07:00
|
|
|
const getConvertedResults = converter.convert(key, json[key], json, 'get', '', options);
|
2019-05-01 12:50:01 -07:00
|
|
|
getConvertedResults.forEach((getConverted) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.zigbee.publish(
|
|
|
|
entity.ID, entity.type, getConverted.cid, getConverted.cmd, getConverted.cmdType,
|
|
|
|
getConverted.zclData, getConverted.cfg, endpoint, () => {}
|
|
|
|
);
|
|
|
|
}, converted.readAfterWriteTime);
|
|
|
|
});
|
2019-02-01 17:41:05 -07:00
|
|
|
}
|
2019-03-02 08:35:00 -07:00
|
|
|
|
2019-05-01 12:50:01 -07:00
|
|
|
usedConverters.push(converter);
|
|
|
|
});
|
2018-10-23 11:39:48 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 13:55:30 -07:00
|
|
|
module.exports = DevicePublish;
|