Move resolveEntity into settings.js

Had to refer to `module.exports.`
because all of those functions get mocked in tests.
This commit is contained in:
qm3ster
2019-03-08 17:10:28 +02:00
committed by Koen Kanters
parent 184eef72b0
commit 9321a04ecc
6 changed files with 35 additions and 41 deletions
+1 -2
View File
@@ -5,7 +5,6 @@ const logger = require('./util/logger');
const settings = require('./util/settings');
const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
const objectAssignDeep = require('object-assign-deep');
const utils = require('./util/utils');
// Extensions
const ExtensionNetworkMap = require('./extension/networkMap');
@@ -233,7 +232,7 @@ class Controller {
}
publishEntityState(entityID, payload, cache) {
const entity = utils.resolveEntity(entityID);
const entity = settings.resolveEntity(entityID);
const appSettings = settings.get();
let messagePayload = {...payload};
+2 -3
View File
@@ -1,6 +1,5 @@
const settings = require('../util/settings');
const logger = require('../util/logger');
const utils = require('../util/utils');
const Queue = require('queue');
const topicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/(bind|unbind)/.+$`);
@@ -55,7 +54,7 @@ class DeviceBind {
}
// Find source; can only be a device.
const sourceEntity = utils.resolveEntity(topic.ID);
const sourceEntity = settings.resolveEntity(topic.ID);
const source = this.zigbee.getEndpoint(sourceEntity.ID);
if (!source) {
@@ -64,7 +63,7 @@ class DeviceBind {
}
// Find target; can be a device or group.
const targetEntity = utils.resolveEntity(message.toString());
const targetEntity = settings.resolveEntity(message.toString());
let target = null;
if (targetEntity.type === 'device') {
+1 -2
View File
@@ -2,7 +2,6 @@
const settings = require('../util/settings');
const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
const logger = require('../util/logger');
const utils = require('../util/utils');
const topicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/.+/(set|get)$`);
const postfixes = [
@@ -137,7 +136,7 @@ class DevicePublish {
}
// Resolve the entity
const entity = utils.resolveEntity(topic.ID);
const entity = settings.resolveEntity(topic.ID);
// Get entity details
let endpoint = null;
+1 -2
View File
@@ -2,7 +2,6 @@ const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
const settings = require('../util/settings');
const logger = require('../util/logger');
const zigbee2mqttVersion = require('../../package.json').version;
const utils = require('../util/utils');
const configurations = {
// Binary sensor
@@ -607,7 +606,7 @@ class HomeAssistant {
return;
}
const entity = utils.resolveEntity(entityID);
const entity = settings.resolveEntity(entityID);
if (entity.type === 'device' && (!mapping[mappedModel.model] || !settings.getDevice(entity.ID))) {
return;
} else if (entity.type === 'group' && (!settings.getGroup(entity.ID))) {
+30
View File
@@ -204,6 +204,35 @@ function changeFriendlyName(old, new_) {
return true;
}
// An entity can be either a group or a device.
function resolveEntity(ID) {
let type = null;
let friendlyName = null;
if (module.exports.getIeeeAddrByFriendlyName(ID)) {
// Check if the ID is a friendly_name of a device.
friendlyName = ID;
ID = module.exports.getIeeeAddrByFriendlyName(ID);
type = 'device';
} else if (module.exports.getGroupIDByFriendlyName(ID)) {
// Check if the ID is a friendly_name of a group.
friendlyName = ID;
ID = Number(module.exports.getGroupIDByFriendlyName(ID));
type = 'group';
} else if (module.exports.getGroup(ID)) {
friendlyName = module.exports.getGroup(ID).friendly_name;
ID = Number(ID);
type = 'group';
} else {
// By default it is a device with ID as ID.
type = 'device';
const device = module.exports.getDevice(ID);
friendlyName = device ? device.friendly_name : ID;
}
return {ID: ID, type: type, friendlyName: friendlyName};
}
module.exports = {
get: () => objectAssignDeep.noMutate(defaults, settings),
write: () => write(),
@@ -219,6 +248,7 @@ module.exports = {
getGroupIDByFriendlyName: (friendlyName) => getGroupIDByFriendlyName(friendlyName),
changeFriendlyName: (old, new_) => changeFriendlyName(old, new_),
changeDeviceOptions: (ieeeAddr, options) => changeDeviceOptions(ieeeAddr, options),
resolveEntity,
addOnChangeHandler: (handler) => onChangeHandlers.push(handler),
-32
View File
@@ -1,38 +1,7 @@
const settings = require('./settings');
// Xiaomi uses 4151 and 4447 (lumi.plug) as manufacturer ID.
const xiaomiManufacturerID = [4151, 4447];
const ikeaTradfriManufacturerID = [4476];
// An entity can be either a group or a device.
function resolveEntity(ID) {
let type = null;
let friendlyName = null;
if (settings.getIeeeAddrByFriendlyName(ID)) {
// Check if the ID is a friendly_name of a device.
friendlyName = ID;
ID = settings.getIeeeAddrByFriendlyName(ID);
type = 'device';
} else if (settings.getGroupIDByFriendlyName(ID)) {
// Check if the ID is a friendly_name of a group.
friendlyName = ID;
ID = Number(settings.getGroupIDByFriendlyName(ID));
type = 'group';
} else if (settings.getGroup(ID)) {
friendlyName = settings.getGroup(ID).friendly_name;
ID = Number(ID);
type = 'group';
} else {
// By default it is a device with ID as ID.
type = 'device';
const device = settings.getDevice(ID);
friendlyName = device ? device.friendly_name : ID;
}
return {ID: ID, type: type, friendlyName: friendlyName};
}
// construct a local ISO8601 string (instead of UTC-based)
// Example:
// - ISO8601 (UTC) = 2019-03-01T15:32:45.941+0000
@@ -61,6 +30,5 @@ module.exports = {
isXiaomiDevice: (device) => xiaomiManufacturerID.includes(device.manufId),
isIkeaTradfriDevice: (device) => ikeaTradfriManufacturerID.includes(device.manufId),
isNumeric: (string) => /^\d+$/.test(string),
resolveEntity: (ID) => resolveEntity(ID),
toLocalISOString: (dDate) => toLocalISOString(dDate),
};