mirror of
https://github.com/mtan93/homebridge.git
synced 2026-05-23 22:07:32 +01:00
Require plugin name during registration
This commit is contained in:
@@ -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
|
||||||
|
|
||||||
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.
|
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
|
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
|
# 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.
|
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.
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ module.exports = function(homebridge) {
|
|||||||
Service = homebridge.hap.Service;
|
Service = homebridge.hap.Service;
|
||||||
Characteristic = homebridge.hap.Characteristic;
|
Characteristic = homebridge.hap.Characteristic;
|
||||||
|
|
||||||
homebridge.registerAccessory("Lockitron", LockitronAccessory);
|
homebridge.registerAccessory("homebridge-lockitron", "Lockitron", LockitronAccessory);
|
||||||
}
|
}
|
||||||
|
|
||||||
function LockitronAccessory(log, config) {
|
function LockitronAccessory(log, config) {
|
||||||
|
|||||||
+68
-18
@@ -9,8 +9,8 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function API() {
|
function API() {
|
||||||
this._accessories = {}; // this._accessories[name] = accessory constructor
|
this._accessories = {}; // this._accessories[pluginName.accessoryName] = accessory constructor
|
||||||
this._platforms = {}; // this._platforms[name] = platform constructor
|
this._platforms = {}; // this._platforms[pluginName.platformName] = platform constructor
|
||||||
|
|
||||||
// expose HAP-NodeJS in its entirely for plugins to use instead of making Plugins
|
// 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
|
// 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) {
|
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) {
|
API.prototype.registerAccessory = function(pluginName, accessoryName, constructor) {
|
||||||
if (this._accessories[name])
|
var fullName = pluginName + "." + accessoryName;
|
||||||
throw new Error("Attempting to register an accessory '" + name + "' which has already been registered!");
|
|
||||||
|
|
||||||
log.info("Registering accessory '%s'", name);
|
if (this._accessories[fullName])
|
||||||
|
throw new Error("Attempting to register an accessory '" + fullName + "' which has already been registered!");
|
||||||
|
|
||||||
this._accessories[name] = constructor;
|
log.info("Registering accessory '%s'", fullName);
|
||||||
|
|
||||||
|
this._accessories[fullName] = constructor;
|
||||||
}
|
}
|
||||||
|
|
||||||
API.prototype.platform = function(name) {
|
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) {
|
API.prototype.registerPlatform = function(pluginName, platformName, constructor) {
|
||||||
if (this._platforms[name])
|
var fullName = pluginName + "." + platformName;
|
||||||
throw new Error("Attempting to register a platform '" + name + "' which has already been registered!");
|
|
||||||
|
|
||||||
log.info("Registering platform '%s'", name);
|
if (this._platforms[fullName])
|
||||||
|
throw new Error("Attempting to register a platform '" + fullName + "' which has already been registered!");
|
||||||
|
|
||||||
this._platforms[name] = constructor;
|
log.info("Registering platform '%s'", fullName);
|
||||||
|
|
||||||
|
this._platforms[fullName] = constructor;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user