Require plugin name during registration

This commit is contained in:
Nick Farina
2015-11-03 15:02:52 -08:00
parent 52d3a9a96e
commit abc6ccfae7
3 changed files with 77 additions and 19 deletions

View File

@@ -1,4 +1,8 @@
# IMPORTANT
Homebridge has recently spun off its included accessories into a new module [homebridge-legacy-plugins](https://github.com/nfarina/homebridge-legacy-plugins). Please do not open any issues related to specific devices in this repository; go there instead.
# Homebridge
Homebridge is a lightweight NodeJS server you can run on your home network that emulates the iOS HomeKit API. It supports Plugins, which are community-contributed modules that provide a basic bridge from HomeKit to various 3rd-party APIs provided by manufacturers of "smart home" devices.
@@ -61,6 +65,10 @@ You install Plugins the same way you installed Homebridge - as a global NPM modu
npm install -g homebridge-lockitron
You can explore all available plugins at the NPM website by [searching for the keyword `homebridge-plugin`](https://www.npmjs.com/browse/keyword/homebridge-plugin).
**IMPORTANT**: Many of the plugins that Homebridge used to include with its default installation have been moved to the single plugin [homebridge-legacy-plugins](https://www.npmjs.com/package/homebridge-legacy-plugins).
# Adding Homebridge to iOS
HomeKit is actually not an app; it's a "database" similar to HealthKit and PassKit. But where HealthKit has the companion _Health_ app and PassKit has _Passbook_, Apple has supplied no app for managing your HomeKit database (at least [not yet](http://9to5mac.com/2015/05/20/apples-planned-ios-9-home-app-uses-virtual-rooms-to-manage-homekit-accessories/)). However, the HomeKit API is open for developers to write their own apps for adding devices to HomeKit.

View File

@@ -5,7 +5,7 @@ module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("Lockitron", LockitronAccessory);
homebridge.registerAccessory("homebridge-lockitron", "Lockitron", LockitronAccessory);
}
function LockitronAccessory(log, config) {

View File

@@ -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;
}