mirror of
https://github.com/mtan93/homebridge.git
synced 2026-03-08 05:31:55 +00:00
Require plugin name during registration
This commit is contained in:
86
lib/api.js
86
lib/api.js
@@ -9,8 +9,8 @@ module.exports = {
|
||||
}
|
||||
|
||||
function API() {
|
||||
this._accessories = {}; // this._accessories[name] = accessory constructor
|
||||
this._platforms = {}; // this._platforms[name] = platform constructor
|
||||
this._accessories = {}; // this._accessories[pluginName.accessoryName] = accessory constructor
|
||||
this._platforms = {}; // this._platforms[pluginName.platformName] = platform constructor
|
||||
|
||||
// expose HAP-NodeJS in its entirely for plugins to use instead of making Plugins
|
||||
// require() it as a dependency - it's a heavy dependency so we don't want it in
|
||||
@@ -23,33 +23,83 @@ function API() {
|
||||
}
|
||||
|
||||
API.prototype.accessory = function(name) {
|
||||
if (!this._accessories[name])
|
||||
throw new Error("The requested accessory '" + name + "' was not registered by any plugin.");
|
||||
|
||||
return this._accessories[name];
|
||||
// if you passed the "short form" name like "Lockitron" instead of "homebridge-lockitron.Lockitron",
|
||||
// see if it matches exactly one accessory.
|
||||
if (name.indexOf('.') == -1) {
|
||||
var found = [];
|
||||
for (var fullName in this._accessories) {
|
||||
if (fullName.split(".")[1] == name)
|
||||
found.push(fullName);
|
||||
}
|
||||
|
||||
if (found.length == 1) {
|
||||
return this._accessories[found[0]];
|
||||
}
|
||||
else if (found.length > 1) {
|
||||
throw new Error("The requested accessory '" + name + "' has been registered multiple times. Please be more specific by writing one of: " + found.join(", "));
|
||||
}
|
||||
else {
|
||||
throw new Error("The requested accessory '" + name + "' was not registered by any plugin.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
if (!this._accessories[name])
|
||||
throw new Error("The requested accessory '" + name + "' was not registered by any plugin.");
|
||||
|
||||
return this._accessories[name];
|
||||
}
|
||||
}
|
||||
|
||||
API.prototype.registerAccessory = function(name, constructor) {
|
||||
if (this._accessories[name])
|
||||
throw new Error("Attempting to register an accessory '" + name + "' which has already been registered!");
|
||||
API.prototype.registerAccessory = function(pluginName, accessoryName, constructor) {
|
||||
var fullName = pluginName + "." + accessoryName;
|
||||
|
||||
if (this._accessories[fullName])
|
||||
throw new Error("Attempting to register an accessory '" + fullName + "' which has already been registered!");
|
||||
|
||||
log.info("Registering accessory '%s'", name);
|
||||
log.info("Registering accessory '%s'", fullName);
|
||||
|
||||
this._accessories[name] = constructor;
|
||||
this._accessories[fullName] = constructor;
|
||||
}
|
||||
|
||||
API.prototype.platform = function(name) {
|
||||
if (!this._platforms[name])
|
||||
throw new Error("The requested platform '" + name + "' was not registered by any plugin.");
|
||||
|
||||
return this._platforms[name];
|
||||
// if you passed the "short form" name like "Lockitron" instead of "homebridge-lockitron.Lockitron",
|
||||
// see if it matches exactly one platform.
|
||||
if (name.indexOf('.') == -1) {
|
||||
var found = [];
|
||||
for (var fullName in this._platforms) {
|
||||
if (fullName.split(".")[1] == name)
|
||||
found.push(fullName);
|
||||
}
|
||||
|
||||
if (found.length == 1) {
|
||||
return this._platforms[found[0]];
|
||||
}
|
||||
else if (found.length > 1) {
|
||||
throw new Error("The requested platform '" + name + "' has been registered multiple times. Please be more specific by writing one of: " + found.join(", "));
|
||||
}
|
||||
else {
|
||||
throw new Error("The requested platform '" + name + "' was not registered by any plugin.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
if (!this._platforms[name])
|
||||
throw new Error("The requested platform '" + name + "' was not registered by any plugin.");
|
||||
|
||||
return this._platforms[name];
|
||||
}
|
||||
}
|
||||
|
||||
API.prototype.registerPlatform = function(name, constructor) {
|
||||
if (this._platforms[name])
|
||||
throw new Error("Attempting to register a platform '" + name + "' which has already been registered!");
|
||||
API.prototype.registerPlatform = function(pluginName, platformName, constructor) {
|
||||
var fullName = pluginName + "." + platformName;
|
||||
|
||||
if (this._platforms[fullName])
|
||||
throw new Error("Attempting to register a platform '" + fullName + "' which has already been registered!");
|
||||
|
||||
log.info("Registering platform '%s'", name);
|
||||
log.info("Registering platform '%s'", fullName);
|
||||
|
||||
this._platforms[name] = constructor;
|
||||
this._platforms[fullName] = constructor;
|
||||
}
|
||||
Reference in New Issue
Block a user