2020-04-11 06:18:17 -07:00
|
|
|
const settings = require('../../util/settings');
|
|
|
|
const logger = require('../../util/logger');
|
2019-09-09 10:48:09 -07:00
|
|
|
const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters');
|
2020-04-11 06:18:17 -07:00
|
|
|
const utils = require('../../util/utils');
|
2019-09-09 10:48:09 -07:00
|
|
|
const assert = require('assert');
|
2020-04-11 11:47:25 -07:00
|
|
|
const Extension = require('../extension');
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2019-11-23 12:44:23 -07:00
|
|
|
const configRegex =
|
|
|
|
new RegExp(`${settings.get().mqtt.base_topic}/bridge/config/((?:\\w+/get)|(?:\\w+/factory_reset)|(?:\\w+))`);
|
2018-11-16 12:23:11 -07:00
|
|
|
const allowedLogLevels = ['error', 'warn', 'info', 'debug'];
|
|
|
|
|
2020-04-11 09:10:56 -07:00
|
|
|
class BridgeLegacy extends Extension {
|
2020-01-09 13:47:19 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishEntityState, eventBus) {
|
|
|
|
super(zigbee, mqtt, state, publishEntityState, eventBus);
|
2018-11-16 12:23:11 -07:00
|
|
|
|
|
|
|
// Bind functions
|
|
|
|
this.permitJoin = this.permitJoin.bind(this);
|
2019-02-18 11:46:57 -07:00
|
|
|
this.lastSeen = this.lastSeen.bind(this);
|
2019-03-10 06:04:09 -07:00
|
|
|
this.elapsed = this.elapsed.bind(this);
|
2019-02-16 11:40:34 -07:00
|
|
|
this.reset = this.reset.bind(this);
|
2018-11-16 12:23:11 -07:00
|
|
|
this.logLevel = this.logLevel.bind(this);
|
|
|
|
this.devices = this.devices.bind(this);
|
2019-03-19 11:18:22 -07:00
|
|
|
this.groups = this.groups.bind(this);
|
2018-11-16 12:23:11 -07:00
|
|
|
this.rename = this.rename.bind(this);
|
2019-12-08 08:50:18 -07:00
|
|
|
this.renameLast = this.renameLast.bind(this);
|
2018-11-16 12:23:11 -07:00
|
|
|
this.remove = this.remove.bind(this);
|
2019-11-03 06:54:03 -07:00
|
|
|
this.forceRemove = this.forceRemove.bind(this);
|
2019-01-08 11:00:02 -07:00
|
|
|
this.ban = this.ban.bind(this);
|
2019-02-22 12:10:00 -07:00
|
|
|
this.deviceOptions = this.deviceOptions.bind(this);
|
2019-03-12 13:19:04 -07:00
|
|
|
this.addGroup = this.addGroup.bind(this);
|
|
|
|
this.removeGroup = this.removeGroup.bind(this);
|
2019-11-23 12:44:23 -07:00
|
|
|
this.whitelist = this.whitelist.bind(this);
|
|
|
|
this.touchlinkFactoryReset = this.touchlinkFactoryReset.bind(this);
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2019-12-08 08:50:18 -07:00
|
|
|
this.lastJoinedDeviceName = null;
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
// Set supported options
|
|
|
|
this.supportedOptions = {
|
|
|
|
'permit_join': this.permitJoin,
|
2019-02-18 11:46:57 -07:00
|
|
|
'last_seen': this.lastSeen,
|
2019-03-10 06:04:09 -07:00
|
|
|
'elapsed': this.elapsed,
|
2019-02-16 11:40:34 -07:00
|
|
|
'reset': this.reset,
|
2018-11-16 12:23:11 -07:00
|
|
|
'log_level': this.logLevel,
|
|
|
|
'devices': this.devices,
|
2019-03-19 11:18:22 -07:00
|
|
|
'groups': this.groups,
|
2019-03-26 12:24:03 -07:00
|
|
|
'devices/get': this.devices,
|
2018-11-16 12:23:11 -07:00
|
|
|
'rename': this.rename,
|
2019-12-08 08:50:18 -07:00
|
|
|
'rename_last': this.renameLast,
|
2018-11-16 12:23:11 -07:00
|
|
|
'remove': this.remove,
|
2019-11-03 06:54:03 -07:00
|
|
|
'force_remove': this.forceRemove,
|
2019-01-08 11:00:02 -07:00
|
|
|
'ban': this.ban,
|
2019-02-22 12:10:00 -07:00
|
|
|
'device_options': this.deviceOptions,
|
2019-03-12 13:19:04 -07:00
|
|
|
'add_group': this.addGroup,
|
|
|
|
'remove_group': this.removeGroup,
|
2019-06-25 10:38:36 -07:00
|
|
|
'whitelist': this.whitelist,
|
2019-11-23 12:44:23 -07:00
|
|
|
'touchlink/factory_reset': this.touchlinkFactoryReset,
|
2018-11-16 12:23:11 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:38:36 -07:00
|
|
|
whitelist(topic, message) {
|
2019-09-09 10:48:09 -07:00
|
|
|
try {
|
|
|
|
const entity = settings.getEntity(message);
|
|
|
|
assert(entity, `Entity '${message}' does not exist`);
|
|
|
|
settings.whitelistDevice(entity.ID);
|
|
|
|
logger.info(`Whitelisted '${entity.friendlyName}'`);
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: 'device_whitelisted', message: {friendly_name: entity.friendlyName}}),
|
|
|
|
);
|
2019-09-09 10:48:09 -07:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(`Failed to whitelist '${message}' '${error}'`);
|
|
|
|
}
|
2019-06-25 10:38:36 -07:00
|
|
|
}
|
|
|
|
|
2019-02-22 12:10:00 -07:00
|
|
|
deviceOptions(topic, message) {
|
|
|
|
let json = null;
|
|
|
|
try {
|
2019-09-09 10:48:09 -07:00
|
|
|
json = JSON.parse(message);
|
2019-02-22 12:10:00 -07:00
|
|
|
} catch (e) {
|
|
|
|
logger.error('Failed to parse message as JSON');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json.hasOwnProperty('friendly_name') || !json.hasOwnProperty('options')) {
|
|
|
|
logger.error('Invalid JSON message, should contain "friendly_name" and "options"');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const entity = settings.getEntity(json.friendly_name);
|
|
|
|
assert(entity, `Entity '${json.friendly_name}' does not exist`);
|
|
|
|
settings.changeDeviceOptions(entity.ID, json.options);
|
2019-02-22 12:10:00 -07:00
|
|
|
logger.info(`Changed device specific options of '${json.friendly_name}' (${JSON.stringify(json.options)})`);
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async permitJoin(topic, message) {
|
|
|
|
await this.zigbee.permitJoin(message.toLowerCase() === 'true');
|
|
|
|
this.publish();
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async reset(topic, message) {
|
|
|
|
try {
|
2019-10-07 10:34:10 -07:00
|
|
|
await this.zigbee.reset('soft');
|
2019-09-09 10:48:09 -07:00
|
|
|
logger.info('Soft resetted ZNP');
|
|
|
|
} catch (error) {
|
|
|
|
logger.error('Soft reset failed');
|
|
|
|
}
|
2019-02-16 11:40:34 -07:00
|
|
|
}
|
|
|
|
|
2019-02-18 11:46:57 -07:00
|
|
|
lastSeen(topic, message) {
|
2019-03-02 08:47:36 -07:00
|
|
|
const allowed = ['disable', 'ISO_8601', 'epoch', 'ISO_8601_local'];
|
2019-02-18 11:46:57 -07:00
|
|
|
if (!allowed.includes(message)) {
|
|
|
|
logger.error(`${message} is not an allowed value, possible: ${allowed}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.set(['advanced', 'last_seen'], message);
|
|
|
|
logger.info(`Set last_seen to ${message}`);
|
|
|
|
}
|
|
|
|
|
2019-03-10 06:04:09 -07:00
|
|
|
elapsed(topic, message) {
|
|
|
|
const allowed = ['true', 'false'];
|
|
|
|
if (!allowed.includes(message)) {
|
|
|
|
logger.error(`${message} is not an allowed value, possible: ${allowed}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-10 13:34:20 -07:00
|
|
|
settings.set(['advanced', 'elapsed'], message === 'true');
|
2019-03-10 06:04:09 -07:00
|
|
|
logger.info(`Set elapsed to ${message}`);
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
logLevel(topic, message) {
|
2019-09-09 10:48:09 -07:00
|
|
|
const level = message.toLowerCase();
|
2018-11-16 12:23:11 -07:00
|
|
|
if (allowedLogLevels.includes(level)) {
|
|
|
|
logger.info(`Switching log level to '${level}'`);
|
2019-09-25 01:55:50 -07:00
|
|
|
logger.setLevel(level);
|
2018-11-16 12:23:11 -07:00
|
|
|
} else {
|
|
|
|
logger.error(`Could not set log level to '${level}'. Allowed level: '${allowedLogLevels.join(',')}'`);
|
|
|
|
}
|
2018-12-01 07:30:59 -07:00
|
|
|
|
|
|
|
this.publish();
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async devices(topic, message) {
|
2019-09-23 14:10:29 -07:00
|
|
|
const coordinator = await this.zigbee.getCoordinatorVersion();
|
2019-09-23 13:21:27 -07:00
|
|
|
const devices = this.zigbee.getDevices().map((device) => {
|
2019-03-14 12:30:06 -07:00
|
|
|
const payload = {
|
2018-11-16 12:23:11 -07:00
|
|
|
ieeeAddr: device.ieeeAddr,
|
|
|
|
type: device.type,
|
2019-09-23 14:10:29 -07:00
|
|
|
networkAddress: device.networkAddress,
|
2018-11-16 12:23:11 -07:00
|
|
|
};
|
2019-03-14 12:30:06 -07:00
|
|
|
|
|
|
|
if (device.type !== 'Coordinator') {
|
2020-04-13 12:15:08 -07:00
|
|
|
const definition = zigbeeHerdsmanConverters.findByDevice(device);
|
2019-03-14 12:30:06 -07:00
|
|
|
const friendlyDevice = settings.getDevice(device.ieeeAddr);
|
2020-04-05 06:41:24 -07:00
|
|
|
payload.model = definition ? definition.model : device.modelID;
|
|
|
|
payload.vendor = definition ? definition.vendor : '-';
|
|
|
|
payload.description = definition ? definition.description : '-';
|
2019-03-14 12:30:06 -07:00
|
|
|
payload.friendly_name = friendlyDevice ? friendlyDevice.friendly_name : device.ieeeAddr;
|
2019-09-09 10:48:09 -07:00
|
|
|
payload.manufacturerID = device.manufacturerID;
|
|
|
|
payload.manufacturerName = device.manufacturerName;
|
2019-03-21 12:00:13 -07:00
|
|
|
payload.powerSource = device.powerSource;
|
2019-09-09 10:48:09 -07:00
|
|
|
payload.modelID = device.modelID;
|
2019-09-12 11:58:57 -07:00
|
|
|
payload.hardwareVersion = device.hardwareVersion;
|
|
|
|
payload.softwareBuildID = device.softwareBuildID;
|
2019-03-27 10:18:49 -07:00
|
|
|
payload.dateCode = device.dateCode;
|
2019-09-12 13:50:51 -07:00
|
|
|
payload.lastSeen = device.lastSeen;
|
2019-09-23 14:10:29 -07:00
|
|
|
} else {
|
|
|
|
payload.friendly_name = 'Coordinator';
|
|
|
|
payload.softwareBuildID = coordinator.type;
|
|
|
|
payload.dateCode = coordinator.meta.revision.toString();
|
|
|
|
payload.lastSeen = Date.now();
|
2019-03-14 12:30:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return payload;
|
2018-11-16 12:23:11 -07:00
|
|
|
});
|
|
|
|
|
2019-03-26 12:24:03 -07:00
|
|
|
if (topic.split('/').pop() == 'get') {
|
|
|
|
this.mqtt.publish(`bridge/config/devices`, JSON.stringify(devices), {});
|
|
|
|
} else {
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish('bridge/log', JSON.stringify({type: 'devices', message: devices}));
|
2019-03-26 12:24:03 -07:00
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
|
2019-03-19 11:18:22 -07:00
|
|
|
groups(topic, message) {
|
2020-04-05 09:36:08 -07:00
|
|
|
const payload = settings.getGroups().map((g) => {
|
2019-09-09 10:48:09 -07:00
|
|
|
const group = {...g};
|
|
|
|
delete group.friendlyName;
|
|
|
|
return group;
|
2020-04-05 09:36:08 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
this.mqtt.publish('bridge/log', JSON.stringify({type: 'groups', message: payload}));
|
2019-03-19 11:18:22 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
rename(topic, message) {
|
2019-09-09 10:48:09 -07:00
|
|
|
const invalid = `Invalid rename message format expected {old: 'friendly_name', new: 'new_name} got ${message}`;
|
2018-11-16 12:23:11 -07:00
|
|
|
|
|
|
|
let json = null;
|
|
|
|
try {
|
2019-09-09 10:48:09 -07:00
|
|
|
json = JSON.parse(message);
|
2018-11-16 12:23:11 -07:00
|
|
|
} catch (e) {
|
|
|
|
logger.error(invalid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate message
|
|
|
|
if (!json.new || !json.old) {
|
|
|
|
logger.error(invalid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-08 08:50:18 -07:00
|
|
|
this._renameInternal(json.old, json.new);
|
|
|
|
}
|
|
|
|
|
|
|
|
renameLast(topic, message) {
|
|
|
|
if (!this.lastJoinedDeviceName) {
|
|
|
|
logger.error(`Cannot rename last joined device, no device has joined during this session`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._renameInternal(this.lastJoinedDeviceName, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
_renameInternal(from, to) {
|
2019-09-09 10:48:09 -07:00
|
|
|
try {
|
2020-02-23 13:51:30 -07:00
|
|
|
const isGroup = settings.getGroup(from) !== null;
|
2019-12-08 08:50:18 -07:00
|
|
|
settings.changeFriendlyName(from, to);
|
|
|
|
logger.info(`Successfully renamed - ${from} to ${to} `);
|
2020-03-04 04:55:08 -07:00
|
|
|
const entity = this.zigbee.resolveEntity(to);
|
|
|
|
const eventData = isGroup ? {group: entity.group} : {device: entity.device};
|
|
|
|
this.eventBus.emit(`${isGroup ? 'group' : 'device'}Renamed`, eventData);
|
2020-04-05 09:36:08 -07:00
|
|
|
|
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: `${isGroup ? 'group' : 'device'}_renamed`, message: {from, to}}),
|
|
|
|
);
|
2019-09-09 10:48:09 -07:00
|
|
|
} catch (error) {
|
2019-12-08 08:50:18 -07:00
|
|
|
logger.error(`Failed to rename - ${from} to ${to}`);
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 13:21:27 -07:00
|
|
|
addGroup(topic, message) {
|
2019-11-04 09:59:00 -07:00
|
|
|
let id = null;
|
|
|
|
let name = null;
|
|
|
|
try {
|
|
|
|
// json payload with id and friendly_name
|
|
|
|
const json = JSON.parse(message);
|
|
|
|
if (json.hasOwnProperty('id')) {
|
|
|
|
id = json.id;
|
|
|
|
name = `group_${id}`;
|
|
|
|
}
|
|
|
|
if (json.hasOwnProperty('friendly_name')) {
|
|
|
|
name = json.friendly_name;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// just friendly_name
|
|
|
|
name = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name == null) {
|
|
|
|
logger.error('Failed to add group, missing friendly_name!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const group = settings.addGroup(name, id);
|
2019-09-23 13:21:27 -07:00
|
|
|
this.zigbee.createGroup(group.ID);
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish('bridge/log', JSON.stringify({type: `group_added`, message: name}));
|
2019-09-09 10:48:09 -07:00
|
|
|
logger.info(`Added group '${name}'`);
|
2019-03-12 13:19:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
removeGroup(topic, message) {
|
2019-09-09 10:48:09 -07:00
|
|
|
const name = message;
|
2020-02-22 07:45:56 -07:00
|
|
|
const entity = this.zigbee.resolveEntity(message);
|
|
|
|
assert(entity && entity.type === 'group', `Group '${message}' does not exist`);
|
|
|
|
settings.removeGroup(message);
|
|
|
|
entity.group.removeFromDatabase();
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish('bridge/log', JSON.stringify({type: `group_removed`, message}));
|
2019-09-09 10:48:09 -07:00
|
|
|
logger.info(`Removed group '${name}'`);
|
2019-03-12 13:19:04 -07:00
|
|
|
}
|
|
|
|
|
2019-11-03 06:54:03 -07:00
|
|
|
async forceRemove(topic, message) {
|
|
|
|
await this.removeForceRemoveOrBan('force_remove', message);
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async remove(topic, message) {
|
2019-11-03 06:54:03 -07:00
|
|
|
await this.removeForceRemoveOrBan('remove', message);
|
2019-01-08 11:00:02 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async ban(topic, message) {
|
2019-11-03 06:54:03 -07:00
|
|
|
await this.removeForceRemoveOrBan('ban', message);
|
2019-01-08 11:00:02 -07:00
|
|
|
}
|
|
|
|
|
2019-11-03 06:54:03 -07:00
|
|
|
async removeForceRemoveOrBan(action, message) {
|
2019-09-25 16:14:58 -07:00
|
|
|
const entity = this.zigbee.resolveEntity(message.trim());
|
2019-11-03 06:54:03 -07:00
|
|
|
const lookup = {
|
|
|
|
ban: ['banned', 'Banning', 'ban'],
|
|
|
|
force_remove: ['force_removed', 'Force removing', 'force remove'],
|
|
|
|
remove: ['removed', 'Removing', 'remove'],
|
|
|
|
};
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2019-11-03 07:02:00 -07:00
|
|
|
if (!entity) {
|
|
|
|
logger.error(`Cannot ${lookup[action][2]}, device '${message}' does not exist`);
|
2020-01-07 13:06:48 -07:00
|
|
|
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish('bridge/log', JSON.stringify({type: `device_${lookup[action][0]}_failed`, message}));
|
2019-11-03 07:02:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
const cleanup = () => {
|
2020-01-09 13:47:19 -07:00
|
|
|
// Fire event
|
|
|
|
this.eventBus.emit('deviceRemoved', {device: entity.device});
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
// Remove from configuration.yaml
|
2019-09-09 10:48:09 -07:00
|
|
|
settings.removeDevice(entity.settings.ID);
|
2018-11-16 12:23:11 -07:00
|
|
|
|
|
|
|
// Remove from state
|
2019-09-09 10:48:09 -07:00
|
|
|
this.state.remove(entity.settings.ID);
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2019-11-03 06:54:03 -07:00
|
|
|
logger.info(`Successfully ${lookup[action][0]} ${entity.settings.friendlyName}`);
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish('bridge/log', JSON.stringify({type: `device_${lookup[action][0]}`, message}));
|
2018-11-16 12:23:11 -07:00
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
try {
|
2019-11-03 06:54:03 -07:00
|
|
|
logger.info(`${lookup[action][1]} '${entity.settings.friendlyName}'`);
|
|
|
|
if (action === 'force_remove') {
|
|
|
|
await entity.device.removeFromDatabase();
|
|
|
|
} else {
|
|
|
|
await entity.device.removeFromNetwork();
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
cleanup();
|
2019-09-09 10:48:09 -07:00
|
|
|
} catch (error) {
|
2019-11-03 06:54:03 -07:00
|
|
|
logger.error(`Failed to ${lookup[action][2]} ${entity.settings.friendlyName} (${error})`);
|
2019-12-07 09:57:39 -07:00
|
|
|
// eslint-disable-next-line
|
|
|
|
logger.error(`See https://www.zigbee2mqtt.io/information/mqtt_topics_and_message_structure.html#zigbee2mqttbridgeconfigremove for more info`);
|
2020-01-07 13:06:48 -07:00
|
|
|
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish('bridge/log', JSON.stringify({type: `device_${lookup[action][0]}_failed`, message}));
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
2019-10-17 13:08:34 -07:00
|
|
|
|
2019-11-03 06:54:03 -07:00
|
|
|
if (action === 'ban') {
|
2019-10-17 13:08:34 -07:00
|
|
|
settings.banDevice(entity.settings.ID);
|
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async onMQTTConnected() {
|
2018-11-16 12:23:11 -07:00
|
|
|
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/config/+`);
|
2019-11-23 12:44:23 -07:00
|
|
|
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/config/+/+`);
|
2019-09-09 10:48:09 -07:00
|
|
|
await this.publish();
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async onMQTTMessage(topic, message) {
|
2018-11-16 12:23:11 -07:00
|
|
|
if (!topic.match(configRegex)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-26 12:24:03 -07:00
|
|
|
const option = topic.match(configRegex)[1];
|
2018-11-16 12:23:11 -07:00
|
|
|
|
|
|
|
if (!this.supportedOptions.hasOwnProperty(option)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
await this.supportedOptions[option](topic, message);
|
2018-11-16 12:23:11 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-01 07:30:59 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async publish() {
|
|
|
|
const info = await utils.getZigbee2mqttVersion();
|
|
|
|
const coordinator = await this.zigbee.getCoordinatorVersion();
|
|
|
|
const topic = `bridge/config`;
|
|
|
|
const payload = {
|
|
|
|
version: info.version,
|
|
|
|
commit: info.commitHash,
|
|
|
|
coordinator,
|
2019-09-25 01:55:50 -07:00
|
|
|
log_level: logger.getLevel(),
|
2019-09-09 10:48:09 -07:00
|
|
|
permit_join: await this.zigbee.getPermitJoin(),
|
|
|
|
};
|
2018-12-01 07:30:59 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
await this.mqtt.publish(topic, JSON.stringify(payload), {retain: true, qos: 0});
|
2018-12-01 07:30:59 -07:00
|
|
|
}
|
2019-11-23 12:44:23 -07:00
|
|
|
|
2020-04-05 06:41:24 -07:00
|
|
|
onZigbeeEvent(type, data, resolvedEntity) {
|
|
|
|
if (type === 'deviceJoined' && resolvedEntity) {
|
|
|
|
this.lastJoinedDeviceName = resolvedEntity.name;
|
2019-12-08 08:50:18 -07:00
|
|
|
}
|
2020-04-04 13:47:23 -07:00
|
|
|
|
|
|
|
if (type === 'deviceJoined') {
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: `device_connected`, message: {friendly_name: resolvedEntity.name}}),
|
|
|
|
);
|
2020-04-04 13:47:23 -07:00
|
|
|
} else if (type === 'deviceInterview') {
|
|
|
|
if (data.status === 'successful') {
|
2020-04-05 06:41:24 -07:00
|
|
|
if (resolvedEntity.definition) {
|
|
|
|
const {vendor, description, model} = resolvedEntity.definition;
|
|
|
|
const log = {friendly_name: resolvedEntity.name, model, vendor, description, supported: true};
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: `pairing`, message: 'interview_successful', meta: log}),
|
|
|
|
);
|
2020-04-04 13:47:23 -07:00
|
|
|
} else {
|
2020-04-05 09:36:08 -07:00
|
|
|
const meta = {friendly_name: resolvedEntity.name, supported: false};
|
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: `pairing`, message: 'interview_successful', meta}),
|
|
|
|
);
|
2020-04-04 13:47:23 -07:00
|
|
|
}
|
|
|
|
} else if (data.status === 'failed') {
|
2020-04-05 09:36:08 -07:00
|
|
|
const meta = {friendly_name: resolvedEntity.name};
|
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: `pairing`, message: 'interview_failed', meta}),
|
|
|
|
);
|
2020-04-04 13:47:23 -07:00
|
|
|
} else {
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (data.status === 'started') {
|
2020-04-05 09:36:08 -07:00
|
|
|
const meta = {friendly_name: resolvedEntity.name};
|
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: `pairing`, message: 'interview_started', meta}),
|
|
|
|
);
|
2020-04-04 13:47:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (type === 'deviceAnnounce') {
|
2020-04-05 09:36:08 -07:00
|
|
|
const meta = {friendly_name: resolvedEntity.name};
|
|
|
|
this.mqtt.publish('bridge/log', JSON.stringify({type: `device_announced`, message: 'announce', meta}));
|
2020-04-04 13:47:23 -07:00
|
|
|
} else {
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (type === 'deviceLeave') {
|
2020-04-05 06:41:24 -07:00
|
|
|
const name = resolvedEntity ? resolvedEntity.name : data.ieeeAddr;
|
2020-04-05 09:36:08 -07:00
|
|
|
const meta = {friendly_name: name};
|
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: `device_removed`, message: 'left_network', meta}),
|
|
|
|
);
|
2020-04-04 13:47:23 -07:00
|
|
|
}
|
|
|
|
}
|
2019-12-08 08:50:18 -07:00
|
|
|
}
|
|
|
|
|
2019-11-23 12:44:23 -07:00
|
|
|
async touchlinkFactoryReset() {
|
|
|
|
logger.info('Starting touchlink factory reset...');
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: `touchlink`, message: 'reset_started', meta: {status: 'started'}}),
|
|
|
|
);
|
2019-11-23 12:44:23 -07:00
|
|
|
const result = await this.zigbee.touchlinkFactoryReset();
|
|
|
|
|
|
|
|
if (result) {
|
2020-01-08 11:29:22 -07:00
|
|
|
logger.info('Successfully factory reset device through Touchlink');
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: `touchlink`, message: 'reset_success', meta: {status: 'success'}}),
|
|
|
|
);
|
2019-11-23 12:44:23 -07:00
|
|
|
} else {
|
|
|
|
logger.warn('Failed to factory reset device through Touchlink');
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
|
|
|
JSON.stringify({type: `touchlink`, message: 'reset_failed', meta: {status: 'failed'}}),
|
|
|
|
);
|
2019-11-23 12:44:23 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
|
|
|
|
2020-04-04 13:47:23 -07:00
|
|
|
module.exports = BridgeLegacy;
|