zigbee2mqtt/lib/extension/bridge.js

421 lines
16 KiB
JavaScript
Raw Normal View History

2020-05-04 11:06:50 -07:00
const logger = require('../util/logger');
const utils = require('../util/utils');
const Extension = require('./extension');
const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters');
const settings = require('../util/settings');
const Transport = require('winston-transport');
2020-05-04 11:06:50 -07:00
const requestRegex = new RegExp(`${settings.get().mqtt.base_topic}/bridge/request/(.*)`);
2020-05-24 09:16:39 -07:00
class Bridge extends Extension {
constructor(zigbee, mqtt, state, publishEntityState, eventBus, enableDisableExtension) {
2020-05-04 11:06:50 -07:00
super(zigbee, mqtt, state, publishEntityState, eventBus);
this.enableDisableExtension = enableDisableExtension;
this.lastJoinedDeviceIeeeAddr = null;
this.setupMQTTLogging();
2020-05-04 11:06:50 -07:00
this.requestLookup = {
'device/options': this.deviceOptions.bind(this),
'device/remove': this.deviceRemove.bind(this),
'device/rename': this.deviceRename.bind(this),
2020-06-13 14:42:58 -07:00
'group/add': this.groupAdd.bind(this),
'group/options': this.groupOptions.bind(this),
2020-06-13 14:28:06 -07:00
'group/remove': this.groupRemove.bind(this),
'group/rename': this.groupRename.bind(this),
'permit_join': this.permitJoin.bind(this),
'config/last_seen': this.configLastSeen.bind(this),
'config/homeassistant': this.configHomeAssistant.bind(this),
2020-06-15 09:58:32 -07:00
'config/elapsed': this.configElapsed.bind(this),
'config/log_level': this.configLogLevel.bind(this),
'touchlink/factory_reset': this.touchlinkFactoryReset.bind(this),
2020-07-21 12:14:39 -07:00
'health_check': this.healthCheck.bind(this),
2020-05-04 11:06:50 -07:00
};
}
async onMQTTConnected() {
2020-05-24 09:16:39 -07:00
this.zigbee2mqttVersion = await utils.getZigbee2mqttVersion();
this.coordinatorVersion = await this.zigbee.getCoordinatorVersion();
this.eventBus.on(`groupMembersChanged`, () => this.publishGroups());
2020-05-24 09:16:39 -07:00
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/request/#`);
2020-05-04 11:06:50 -07:00
await this.publishInfo();
await this.publishDevices();
await this.publishGroups();
}
setupMQTTLogging() {
const mqtt = this.mqtt;
class EventTransport extends Transport {
log(info, callback) {
const payload = JSON.stringify({message: info.message, level: info.level});
mqtt.publish(`bridge/logging`, payload, {}, settings.get().mqtt.base_topic, true);
callback();
}
}
logger.add(new EventTransport());
}
2020-05-04 11:06:50 -07:00
async onMQTTMessage(topic, message) {
const match = topic.match(requestRegex);
if (match && this.requestLookup[match[1].toLowerCase()]) {
2020-06-15 11:10:30 -07:00
message = utils.parseJSON(message, message);
2020-06-13 14:46:04 -07:00
try {
const response = await this.requestLookup[match[1].toLowerCase()](message);
await this.mqtt.publish(`bridge/response/${match[1]}`, JSON.stringify(response));
} catch (error) {
logger.error(`Request '${topic}' failed with error: '${error.message}'`);
const response = utils.getResponse(message, {}, error.message);
await this.mqtt.publish(`bridge/response/${match[1]}`, JSON.stringify(response));
}
2020-05-04 11:06:50 -07:00
}
}
async onZigbeeEvent(type, data, resolvedEntity) {
if (type === 'deviceJoined' && resolvedEntity) {
this.lastJoinedDeviceIeeeAddr = resolvedEntity.device.ieeeAddr;
}
2020-05-04 11:06:50 -07:00
if (['deviceJoined', 'deviceLeave', 'deviceInterview'].includes(type)) {
let payload;
const ieeeAddress = data.device ? data.device.ieeeAddr : data.ieeeAddr;
if (type === 'deviceJoined') {
payload = {friendly_name: resolvedEntity.settings.friendlyName, ieee_address: ieeeAddress};
} else if (type === 'deviceInterview') {
payload = {
friendly_name: resolvedEntity.settings.friendlyName, status: data.status, ieee_address: ieeeAddress,
};
2020-05-04 11:06:50 -07:00
if (data.status === 'successful') {
payload.supported = !!resolvedEntity.definition;
payload.definition = resolvedEntity.definition ? {
model: resolvedEntity.definition.model,
vendor: resolvedEntity.definition.vendor,
description: resolvedEntity.definition.description,
supports: resolvedEntity.definition.supports,
} : null;
}
} else payload = {ieee_address: ieeeAddress}; // deviceLeave
2020-05-04 11:06:50 -07:00
await this.mqtt.publish(
'bridge/event',
JSON.stringify({type: utils.toSnakeCase(type), data: payload}),
{retain: false, qos: 0},
);
2020-05-04 11:06:50 -07:00
}
if ('deviceLeave' === type || ('deviceInterview' === type && data.status !== 'started')) {
await this.publishDevices();
}
}
/**
* Requests
*/
async deviceOptions(message) {
2020-06-13 14:28:06 -07:00
return this.changeEntityOptions('device', message);
}
async groupOptions(message) {
2020-06-13 14:28:06 -07:00
return this.changeEntityOptions('group', message);
}
async deviceRemove(message) {
return this.removeEntity('device', message);
}
async groupRemove(message) {
return this.removeEntity('group', message);
}
2020-07-21 12:14:39 -07:00
async healthCheck(message) {
return utils.getResponse(message, {healthy: true}, null);
}
2020-06-13 14:42:58 -07:00
async groupAdd(message) {
if (typeof message === 'object' && !message.hasOwnProperty('friendly_name')) {
2020-06-13 14:42:58 -07:00
throw new Error(`Invalid payload`);
}
const friendlyName = typeof message === 'object' ? message.friendly_name : message;
const ID = typeof message === 'object' && message.hasOwnProperty('id') ? message.id : null;
2020-06-13 14:42:58 -07:00
const group = settings.addGroup(friendlyName, ID);
this.zigbee.createGroup(group.ID);
this.publishGroups();
return utils.getResponse(message, {friendly_name: group.friendlyName, id: group.ID}, null);
2020-06-13 14:42:58 -07:00
}
async deviceRename(message) {
return this.renameEntity('device', message);
}
async groupRename(message) {
return this.renameEntity('group', message);
}
2020-05-24 09:16:39 -07:00
async permitJoin(message) {
2020-06-15 09:58:32 -07:00
const value = this.getValue(message);
await this.zigbee.permitJoin(value);
await this.publishInfo();
return utils.getResponse(message, {value: value}, null);
}
2020-06-15 09:58:32 -07:00
configLastSeen(message) {
const allowed = ['disable', 'ISO_8601', 'epoch', 'ISO_8601_local'];
const value = this.getValue(message);
if (!allowed.includes(value)) {
throw new Error(`'${value}' is not an allowed value, allowed: ${allowed}`);
}
settings.set(['advanced', 'last_seen'], message);
return utils.getResponse(message, {value}, null);
}
configHomeAssistant(message) {
const allowed = [true, false];
const value = this.getValue(message);
if (!allowed.includes(value)) {
throw new Error(`'${value}' is not an allowed value, allowed: ${allowed}`);
}
this.enableDisableExtension(value, 'HomeAssistant');
settings.set(['homeassistant'], value);
return utils.getResponse(message, {value}, null);
}
2020-06-15 09:58:32 -07:00
configElapsed(message) {
const allowed = [true, false];
const value = this.getValue(message);
if (!allowed.includes(value)) {
throw new Error(`'${value}' is not an allowed value, allowed: ${allowed}`);
}
settings.set(['advanced', 'elapsed'], value);
return utils.getResponse(message, {value}, null);
}
configLogLevel(message) {
const allowed = ['error', 'warn', 'info', 'debug'];
const value = this.getValue(message);
if (!allowed.includes(value)) {
throw new Error(`'${value}' is not an allowed value, allowed: ${allowed}`);
}
logger.setLevel(value);
this.publishInfo();
2020-06-15 09:58:32 -07:00
return utils.getResponse(message, {value}, null);
}
2020-06-15 10:19:57 -07:00
async touchlinkFactoryReset(message) {
logger.info('Start touchlink factory reset');
const result = await this.zigbee.touchlinkFactoryReset();
if (result) {
logger.info('Successfully factory reset device through Touchlink');
return utils.getResponse(message, {}, null);
} else {
logger.error('Failed to factory reset device through Touchlink');
throw new Error('Failed to factory reset device through Touchlink');
}
}
/**
* Utils
*/
2020-06-15 09:58:32 -07:00
getValue(message) {
if (typeof message === 'object') {
if (!message.hasOwnProperty('value')) {
throw new Error('No value given');
}
return message.value;
} else {
return message;
}
}
2020-06-13 14:28:06 -07:00
changeEntityOptions(entityType, message) {
if (typeof message !== 'object' || !message.hasOwnProperty('id') || !message.hasOwnProperty('options')) {
2020-06-13 14:28:06 -07:00
throw new Error(`Invalid payload`);
}
const ID = message.id;
2020-06-13 14:28:06 -07:00
const entity = this.getEntity(entityType, ID);
settings.changeEntityOptions(ID, message.options);
const cleanup = (o) => {
delete o.friendlyName; delete o.friendly_name; delete o.ID; delete o.type; delete o.devices;
return o;
2020-06-13 14:46:04 -07:00
};
2020-06-13 14:28:06 -07:00
const oldOptions = cleanup(entity.settings);
const newOptions = cleanup(settings.getEntity(ID));
return utils.getResponse(message, {from: oldOptions, to: newOptions, id: ID}, null);
2020-06-13 14:28:06 -07:00
}
renameEntity(entityType, message) {
const deviceAndHasLast = entityType === 'device' && typeof message === 'object' && message.last === true;
2020-06-13 14:46:04 -07:00
if (typeof message !== 'object' || (!message.hasOwnProperty('from') && !deviceAndHasLast) ||
!message.hasOwnProperty('to')) {
throw new Error(`Invalid payload`);
}
if (deviceAndHasLast && !this.lastJoinedDeviceIeeeAddr) {
2020-06-13 14:46:04 -07:00
throw new Error('No device has joined since start');
}
const from = deviceAndHasLast ? this.lastJoinedDeviceIeeeAddr : message.from;
const to = message.to;
2020-06-13 14:28:06 -07:00
const entity = this.getEntity(entityType, from);
settings.changeFriendlyName(from, to);
if (entity.type === 'device') {
this.publishDevices();
this.eventBus.emit(`deviceRenamed`, {device: entity.device});
} else {
this.publishGroups();
this.eventBus.emit(`groupRenamed`, {group: entity.group});
}
return utils.getResponse(message, {from: entity.settings.friendlyName, to}, null);
}
async removeEntity(entityType, message) {
const ID = typeof message === 'object' ? message.id : message.trim();
2020-06-13 14:28:06 -07:00
const entity = this.getEntity(entityType, ID);
let block = false;
let force = false;
let blockForceLog = '';
if (entityType === 'device' && typeof message === 'object') {
block = !!message.block;
force = !!message.force;
blockForceLog = ` (block: ${block}, force: ${force})`;
} else if (entityType === 'group' && typeof message === 'object') {
force = !!message.force;
blockForceLog = ` (force: ${force})`;
}
try {
logger.info(`Removing ${entity.type} '${entity.settings.friendlyName}'${blockForceLog}`);
if (entity.type === 'device') {
if (block) {
settings.blockDevice(entity.settings.ID);
}
if (force) {
2020-06-13 14:46:04 -07:00
await entity.device.removeFromDatabase();
} else {
await entity.device.removeFromNetwork();
}
} else {
if (force) {
await entity.group.removeFromDatabase();
} else {
await entity.group.removeFromNetwork();
}
}
// Fire event
if (entity.type === 'device') {
this.eventBus.emit('deviceRemoved', {device: entity.device});
}
// Remove from configuration.yaml
if (entity.type === 'device') {
settings.removeDevice(entity.settings.ID);
} else {
settings.removeGroup(entity.settings.ID);
}
// Remove from state
this.state.remove(entity.settings.ID);
logger.info(`Successfully removed ${entity.type} '${entity.settings.friendlyName}'${blockForceLog}`);
if (entity.type === 'device') {
this.publishDevices();
return utils.getResponse(message, {id: ID, block, force}, null);
} else {
this.publishGroups();
return utils.getResponse(message, {id: ID, force: force}, null);
}
} catch (error) {
2020-06-13 14:46:04 -07:00
throw new Error(
`Failed to remove ${entity.type} '${entity.settings.friendlyName}'${blockForceLog} (${error})`,
2020-06-13 14:46:04 -07:00
);
}
}
2020-06-13 14:28:06 -07:00
getEntity(type, ID) {
const entity = this.zigbee.resolveEntity(ID);
if (!entity || entity.type !== type) {
throw new Error(`${utils.capitalize(type)} '${ID}' does not exist`);
}
return entity;
}
2020-05-04 11:06:50 -07:00
async publishInfo() {
const payload = {
2020-05-24 09:16:39 -07:00
version: this.zigbee2mqttVersion.version,
commit: this.zigbee2mqttVersion.commitHash,
coordinator: this.coordinatorVersion,
network: utils.toSnakeCase(await this.zigbee.getNetworkParameters()),
log_level: logger.getLevel(),
permit_join: await this.zigbee.getPermitJoin(),
2020-05-04 11:06:50 -07:00
};
await this.mqtt.publish('bridge/info', JSON.stringify(payload), {retain: true, qos: 0});
}
async publishDevices() {
2020-05-04 11:06:50 -07:00
const devices = this.zigbee.getClients().map((device) => {
const definition = zigbeeHerdsmanConverters.findByDevice(device);
const resolved = this.zigbee.resolveEntity(device);
const definitionPayload = definition ? {
model: definition.model,
vendor: definition.vendor,
description: definition.description,
supports: definition.supports,
} : null;
return {
ieee_address: device.ieeeAddr,
2020-05-04 11:06:50 -07:00
type: device.type,
network_address: device.networkAddress,
2020-05-04 11:06:50 -07:00
supported: !!definition,
friendly_name: resolved.name,
2020-05-04 11:06:50 -07:00
definition: definitionPayload,
power_source: device.powerSource,
software_build_id: device.softwareBuildID,
date_code: device.dateCode,
2020-05-04 11:06:50 -07:00
interviewing: device.interviewing,
interview_completed: device.interviewCompleted,
2020-05-04 11:06:50 -07:00
};
});
await this.mqtt.publish('bridge/devices', JSON.stringify(devices), {retain: true, qos: 0});
}
async publishGroups() {
2020-05-04 11:06:50 -07:00
const groups = this.zigbee.getGroups().map((group) => {
const resolved = this.zigbee.resolveEntity(group);
return {
id: group.groupID,
friendly_name: resolved.name,
2020-05-04 11:06:50 -07:00
members: group.members.map((m) => {
return {
ieee_address: m.deviceIeeeAddress,
2020-05-04 11:06:50 -07:00
endpoint: m.ID,
};
}),
};
});
await this.mqtt.publish('bridge/groups', JSON.stringify(groups), {retain: true, qos: 0});
}
}
2020-05-24 09:16:39 -07:00
module.exports = Bridge;