2018-12-30 14:42:55 -07:00
|
|
|
const settings = require('../util/settings');
|
|
|
|
const logger = require('../util/logger');
|
2019-09-09 10:48:09 -07:00
|
|
|
const assert = require('assert');
|
2018-12-30 14:42:55 -07:00
|
|
|
const topicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/(bind|unbind)/.+$`);
|
2019-09-17 09:32:16 -07:00
|
|
|
const BaseExtension = require('./baseExtension');
|
2018-12-30 14:42:55 -07:00
|
|
|
|
2019-12-11 12:00:09 -07:00
|
|
|
const clusters = ['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};
|
|
|
|
|
2019-09-17 09:32:16 -07:00
|
|
|
class DeviceBind extends BaseExtension {
|
2018-12-30 14:42:55 -07:00
|
|
|
onMQTTConnected() {
|
2020-03-11 13:30:01 -07:00
|
|
|
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/bind/#`);
|
|
|
|
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/unbind/#`);
|
2019-03-15 13:19:42 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async onMQTTMessage(topic, message) {
|
2018-12-30 14:42:55 -07:00
|
|
|
if (!topic.match(topicRegex)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
// Parse topic, retrieve type (bind or unbind) and source
|
2018-12-30 14:42:55 -07:00
|
|
|
topic = topic.replace(`${settings.get().mqtt.base_topic}/bridge/`, '');
|
|
|
|
const type = topic.split('/')[0];
|
2019-09-09 10:48:09 -07:00
|
|
|
const sourceKey = topic.replace(`${type}/`, '');
|
|
|
|
const targetKey = message;
|
2018-12-30 14:42:55 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
// Find source; can only be a device and target
|
2019-09-23 13:21:27 -07:00
|
|
|
const source = this.zigbee.resolveEntity(sourceKey);
|
2019-09-09 10:48:09 -07:00
|
|
|
assert(source != null && source.type === 'device', 'Source undefined or not a device');
|
2020-01-27 12:56:11 -07:00
|
|
|
const target = targetKey === 'default_bind_group' ? defaultBindGroup : this.zigbee.resolveEntity(targetKey);
|
2019-09-09 10:48:09 -07:00
|
|
|
assert(target != null, 'Target is unknown');
|
2018-12-30 14:42:55 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const sourceName = source.settings.friendlyName;
|
2020-01-27 12:56:11 -07:00
|
|
|
const targetName = targetKey === 'default_bind_group' ? targetKey : target.settings.friendlyName;
|
2020-03-20 11:00:00 -07:00
|
|
|
let attemptedToBindSomething = false;
|
2019-03-16 12:07:34 -07:00
|
|
|
|
2018-12-30 14:42:55 -07:00
|
|
|
// Find which clusters are supported by both the source and target.
|
2019-09-09 10:48:09 -07:00
|
|
|
// Groups are assumed to support all clusters.
|
|
|
|
for (const cluster of clusters) {
|
2020-01-27 12:56:11 -07:00
|
|
|
const targetValid = target.type === 'group' || target.type === 'group_number' ||
|
2019-09-09 10:48:09 -07:00
|
|
|
target.device.type === 'Coordinator' || target.endpoint.supportsInputCluster(cluster);
|
|
|
|
|
|
|
|
if (source.endpoint.supportsOutputCluster(cluster) && targetValid) {
|
|
|
|
logger.debug(`${type}ing cluster '${cluster}' from '${sourceName}' to '${targetName}'`);
|
2020-03-20 11:00:00 -07:00
|
|
|
attemptedToBindSomething = true;
|
2019-09-09 10:48:09 -07:00
|
|
|
try {
|
2020-01-27 12:56:11 -07:00
|
|
|
let bindTarget = null;
|
|
|
|
if (target.type === 'group') bindTarget = target.group;
|
|
|
|
else if (target.type === 'group_number') bindTarget = target.ID;
|
|
|
|
else bindTarget = target.endpoint;
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
if (type === 'bind') {
|
|
|
|
await source.endpoint.bind(cluster, bindTarget);
|
|
|
|
} else {
|
|
|
|
await source.endpoint.unbind(cluster, bindTarget);
|
|
|
|
}
|
2018-12-30 14:42:55 -07:00
|
|
|
|
2019-03-15 13:19:42 -07:00
|
|
|
logger.info(
|
2019-09-09 10:48:09 -07:00
|
|
|
`Successfully ${type === 'bind' ? 'bound' : 'unbound'} cluster '${cluster}' from ` +
|
2019-10-26 09:25:51 -07:00
|
|
|
`'${sourceName}' to '${targetName}'`,
|
2019-03-15 13:19:42 -07:00
|
|
|
);
|
2020-04-05 09:36:08 -07:00
|
|
|
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (settings.get().advanced.legacy_api) {
|
|
|
|
const message = {from: sourceName, to: targetName, cluster};
|
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: `device_${type}`, message}),
|
|
|
|
);
|
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
`Failed to ${type} cluster '${cluster}' from '${sourceName}' to ` +
|
2019-10-26 09:25:51 -07:00
|
|
|
`'${targetName}' (${error})`,
|
2019-03-15 13:19:42 -07:00
|
|
|
);
|
2020-04-05 09:36:08 -07:00
|
|
|
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (settings.get().advanced.legacy_api) {
|
|
|
|
const message = {from: sourceName, to: targetName, cluster};
|
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: `device_${type}_failed`, message}),
|
|
|
|
);
|
|
|
|
}
|
2019-03-15 13:19:42 -07:00
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 11:00:00 -07:00
|
|
|
|
|
|
|
if (!attemptedToBindSomething) {
|
2020-03-21 13:33:00 -07:00
|
|
|
logger.error(`Nothing to ${type} from '${sourceName}' to '${targetName}'`);
|
2020-04-05 09:36:08 -07:00
|
|
|
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (settings.get().advanced.legacy_api) {
|
|
|
|
const message = {from: sourceName, to: targetName};
|
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: `device_${type}_failed`, message}),
|
|
|
|
);
|
|
|
|
}
|
2020-03-20 11:00:00 -07:00
|
|
|
}
|
2018-12-30 14:42:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 12:55:14 -07:00
|
|
|
module.exports = DeviceBind;
|