2018-12-30 14:42:55 -07:00
|
|
|
const settings = require('../util/settings');
|
|
|
|
const logger = require('../util/logger');
|
2020-07-12 12:56:11 -07:00
|
|
|
const utils = require('../util/utils');
|
2020-04-15 11:36:40 -07:00
|
|
|
const legacyTopicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/(bind|unbind)/.+$`);
|
2020-07-12 12:56:11 -07:00
|
|
|
const topicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/request/device/(bind|unbind)`);
|
2020-04-11 09:10:56 -07:00
|
|
|
const Extension = require('./extension');
|
2020-08-13 11:00:35 -07:00
|
|
|
const stringify = require('json-stable-stringify');
|
2018-12-30 14:42:55 -07:00
|
|
|
|
2020-07-28 12:05:02 -07:00
|
|
|
const clusterCandidates = ['genScenes', 'genOnOff', 'genLevelCtrl', 'lightingColorCtrl', 'closuresWindowCovering'];
|
2018-12-30 14:42:55 -07:00
|
|
|
|
2020-01-27 12:56:11 -07:00
|
|
|
// See zigbee-herdsman-converters devices.js
|
|
|
|
const defaultBindGroup = {type: 'group_number', ID: 901};
|
|
|
|
|
2020-04-15 11:36:40 -07:00
|
|
|
class Bind extends Extension {
|
|
|
|
constructor(zigbee, mqtt, state, publishEntityState, eventBus) {
|
|
|
|
super(zigbee, mqtt, state, publishEntityState, eventBus);
|
|
|
|
this.legacyApi = settings.get().advanced.legacy_api;
|
|
|
|
}
|
|
|
|
|
2018-12-30 14:42:55 -07:00
|
|
|
onMQTTConnected() {
|
2020-04-15 11:36:40 -07:00
|
|
|
/* istanbul ignore else */
|
|
|
|
if (this.legacyApi) {
|
|
|
|
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/bind/#`);
|
|
|
|
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/unbind/#`);
|
|
|
|
}
|
2020-07-12 12:56:11 -07:00
|
|
|
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (settings.get().experimental.new_api) {
|
|
|
|
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/request/device/bind`);
|
|
|
|
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/request/device/unbind`);
|
|
|
|
}
|
2019-03-15 13:19:42 -07:00
|
|
|
}
|
|
|
|
|
2020-04-15 11:36:40 -07:00
|
|
|
parseMQTTMessage(topic, message) {
|
|
|
|
let type = null;
|
|
|
|
let sourceKey = null;
|
|
|
|
let targetKey = null;
|
2020-07-28 12:05:02 -07:00
|
|
|
let clusters = null;
|
2020-04-15 11:36:40 -07:00
|
|
|
|
|
|
|
if (this.legacyApi && topic.match(legacyTopicRegex)) {
|
|
|
|
topic = topic.replace(`${settings.get().mqtt.base_topic}/bridge/`, '');
|
|
|
|
type = topic.split('/')[0];
|
|
|
|
sourceKey = topic.replace(`${type}/`, '');
|
|
|
|
targetKey = message;
|
2020-07-12 12:56:11 -07:00
|
|
|
} else if (settings.get().experimental.new_api && topic.match(topicRegex)) {
|
|
|
|
type = topic.endsWith('unbind') ? 'unbind' : 'bind';
|
|
|
|
message = JSON.parse(message);
|
|
|
|
sourceKey = message.from;
|
|
|
|
targetKey = message.to;
|
2020-07-28 12:05:02 -07:00
|
|
|
clusters = message.clusters;
|
2018-12-30 14:42:55 -07:00
|
|
|
}
|
|
|
|
|
2020-07-28 12:05:02 -07:00
|
|
|
return {type, sourceKey, targetKey, clusters};
|
2020-04-15 11:36:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async onMQTTMessage(topic, message) {
|
2020-07-28 12:05:02 -07:00
|
|
|
const {type, sourceKey, targetKey, clusters} = this.parseMQTTMessage(topic, message);
|
2020-04-15 11:36:40 -07:00
|
|
|
if (!type) return null;
|
2018-12-30 14:42:55 -07:00
|
|
|
|
2020-07-12 12:56:11 -07:00
|
|
|
let error = null;
|
2019-09-23 13:21:27 -07:00
|
|
|
const source = this.zigbee.resolveEntity(sourceKey);
|
2020-01-27 12:56:11 -07:00
|
|
|
const target = targetKey === 'default_bind_group' ? defaultBindGroup : this.zigbee.resolveEntity(targetKey);
|
2020-07-12 12:56:11 -07:00
|
|
|
const responseData = {from: sourceKey, to: targetKey};
|
|
|
|
|
|
|
|
if (!source || source.type !== 'device') {
|
|
|
|
error = `Source device '${sourceKey}' does not exist`;
|
|
|
|
} else if (!target) {
|
|
|
|
error = `Target device or group '${targetKey}' does not exist`;
|
|
|
|
} else {
|
|
|
|
const sourceName = source.settings.friendlyName;
|
|
|
|
const targetName = targetKey === 'default_bind_group' ? targetKey : target.settings.friendlyName;
|
|
|
|
const successfulClusters = [];
|
|
|
|
const failedClusters = [];
|
|
|
|
const attemptedClusters = [];
|
|
|
|
|
|
|
|
// Find which clusters are supported by both the source and target.
|
|
|
|
// Groups are assumed to support all clusters.
|
2020-07-28 12:05:02 -07:00
|
|
|
for (const cluster of clusterCandidates) {
|
|
|
|
if (clusters && !clusters.includes(cluster)) continue;
|
|
|
|
|
2020-07-12 12:56:11 -07:00
|
|
|
const targetValid = target.type === 'group' || target.type === 'group_number' ||
|
|
|
|
target.device.type === 'Coordinator' || target.endpoint.supportsInputCluster(cluster);
|
|
|
|
|
|
|
|
if (source.endpoint.supportsOutputCluster(cluster) && targetValid) {
|
|
|
|
logger.debug(`${type}ing cluster '${cluster}' from '${sourceName}' to '${targetName}'`);
|
|
|
|
attemptedClusters.push(cluster);
|
|
|
|
|
|
|
|
try {
|
|
|
|
let bindTarget = null;
|
|
|
|
if (target.type === 'group') bindTarget = target.group;
|
|
|
|
else if (target.type === 'group_number') bindTarget = target.ID;
|
|
|
|
else bindTarget = target.endpoint;
|
|
|
|
|
|
|
|
if (type === 'bind') {
|
|
|
|
await source.endpoint.bind(cluster, bindTarget);
|
|
|
|
} else {
|
|
|
|
await source.endpoint.unbind(cluster, bindTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
successfulClusters.push(cluster);
|
|
|
|
logger.info(
|
|
|
|
`Successfully ${type === 'bind' ? 'bound' : 'unbound'} cluster '${cluster}' from ` +
|
|
|
|
`'${sourceName}' to '${targetName}'`,
|
2020-04-05 09:36:08 -07:00
|
|
|
);
|
|
|
|
|
2020-07-12 12:56:11 -07:00
|
|
|
/* istanbul ignore else */
|
|
|
|
if (settings.get().advanced.legacy_api) {
|
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify({type: `device_${type}`,
|
2020-07-12 12:56:11 -07:00
|
|
|
message: {from: sourceName, to: targetName, cluster}}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
failedClusters.push(cluster);
|
|
|
|
logger.error(
|
|
|
|
`Failed to ${type} cluster '${cluster}' from '${sourceName}' to ` +
|
|
|
|
`'${targetName}' (${error})`,
|
2020-04-05 09:36:08 -07:00
|
|
|
);
|
2020-07-12 12:56:11 -07:00
|
|
|
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (settings.get().advanced.legacy_api) {
|
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify({type: `device_${type}_failed`,
|
2020-07-12 12:56:11 -07:00
|
|
|
message: {from: sourceName, to: targetName, cluster}}),
|
|
|
|
);
|
|
|
|
}
|
2020-04-05 09:36:08 -07:00
|
|
|
}
|
2019-03-15 13:19:42 -07:00
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
2020-03-20 11:00:00 -07:00
|
|
|
|
2020-07-12 12:56:11 -07:00
|
|
|
if (attemptedClusters.length === 0) {
|
|
|
|
logger.error(`Nothing to ${type} from '${sourceName}' to '${targetName}'`);
|
|
|
|
error = `Nothing to ${type}`;
|
2020-04-05 09:36:08 -07:00
|
|
|
|
2020-07-12 12:56:11 -07:00
|
|
|
/* istanbul ignore else */
|
|
|
|
if (settings.get().advanced.legacy_api) {
|
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify({type: `device_${type}_failed`, message: {from: sourceName, to: targetName}}),
|
2020-07-12 12:56:11 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (failedClusters.length === attemptedClusters.length) {
|
|
|
|
error = `Failed to ${type}`;
|
2020-04-05 09:36:08 -07:00
|
|
|
}
|
2020-07-12 12:56:11 -07:00
|
|
|
|
|
|
|
responseData[`clusters`] = successfulClusters;
|
|
|
|
responseData[`failed`] = failedClusters;
|
|
|
|
}
|
|
|
|
|
|
|
|
const triggeredViaLegacyApi = topic.match(legacyTopicRegex);
|
|
|
|
if (!triggeredViaLegacyApi) {
|
|
|
|
const response = utils.getResponse(message, responseData, error);
|
2020-08-13 11:00:35 -07:00
|
|
|
await this.mqtt.publish(`bridge/response/device/${type}`, stringify(response));
|
2020-07-12 12:56:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
logger.error(error);
|
2020-03-20 11:00:00 -07:00
|
|
|
}
|
2018-12-30 14:42:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 11:36:40 -07:00
|
|
|
module.exports = Bind;
|