Init Plugin 2.0

This commit is contained in:
Khaos Tian
2016-01-30 18:36:55 -08:00
parent 1a356a1783
commit e1334c5196
10 changed files with 717 additions and 117 deletions

View File

@@ -1,7 +1,10 @@
var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;
var hap = require("hap-nodejs");
var hapLegacyTypes = require("hap-nodejs/accessories/types.js");
var log = require("./logger")._system;
var User = require("./user").User;
var PlatformAccessory = require("./platformAccessory").PlatformAccessory;
// The official homebridge API is the object we feed the plugin's exported initializer function.
@@ -12,7 +15,13 @@ module.exports = {
function API() {
this._accessories = {}; // this._accessories[pluginName.accessoryName] = accessory constructor
this._platforms = {}; // this._platforms[pluginName.platformName] = platform constructor
this._configurableAccessories = {};
this._dynamicPlatforms = {}; // this._dynamicPlatforms[pluginName.platformName] = platform constructor
// expose the homebridge API version
this.version = 2.0;
// expose the User class methods to plugins to get paths. Example: homebridge.user.storagePath()
this.user = User;
@@ -24,8 +33,12 @@ function API() {
// we also need to "bolt on" the legacy "types" constants for older accessories/platforms
// still using the "object literal" style JSON.
this.hapLegacyTypes = hapLegacyTypes;
this.platformAccessory = PlatformAccessory;
}
inherits(API, EventEmitter);
API.prototype.accessory = function(name) {
// if you passed the "short form" name like "Lockitron" instead of "homebridge-lockitron.Lockitron",
@@ -56,7 +69,7 @@ API.prototype.accessory = function(name) {
}
}
API.prototype.registerAccessory = function(pluginName, accessoryName, constructor) {
API.prototype.registerAccessory = function(pluginName, accessoryName, constructor, configurationRequestHandler) {
var fullName = pluginName + "." + accessoryName;
if (this._accessories[fullName])
@@ -65,6 +78,11 @@ API.prototype.registerAccessory = function(pluginName, accessoryName, constructo
log.info("Registering accessory '%s'", fullName);
this._accessories[fullName] = constructor;
// The plugin supports configuration
if (configurationRequestHandler) {
this._configurableAccessories[fullName] = configurationRequestHandler;
}
}
API.prototype.platform = function(name) {
@@ -97,7 +115,7 @@ API.prototype.platform = function(name) {
}
}
API.prototype.registerPlatform = function(pluginName, platformName, constructor) {
API.prototype.registerPlatform = function(pluginName, platformName, constructor, dynamic) {
var fullName = pluginName + "." + platformName;
if (this._platforms[fullName])
@@ -106,4 +124,31 @@ API.prototype.registerPlatform = function(pluginName, platformName, constructor)
log.info("Registering platform '%s'", fullName);
this._platforms[fullName] = constructor;
if (dynamic) {
this._dynamicPlatforms[fullName] = constructor;
}
}
API.prototype.registerPlatformAccessories = function(pluginName, platformName, accessories) {
for (var index in accessories) {
var accessory = accessories[index];
if (!(accessory instanceof PlatformAccessory)) {
throw new Error(pluginName + " - " + platformName + " attempt to register an accessory that isn\'t PlatformAccessory!");
}
accessory._associatedPlugin = pluginName;
accessory._associatedPlatform = platformName;
}
this.emit('registerPlatformAccessories', accessories);
}
API.prototype.unregisterPlatformAccessories = function(pluginName, platformName, accessories) {
for (var index in accessories) {
var accessory = accessories[index];
if (!(accessory instanceof PlatformAccessory)) {
throw new Error(pluginName + " - " + platformName + " attempt to unregister an accessory that isn\'t PlatformAccessory!");
}
}
this.emit('unregisterPlatformAccessories', accessories);
}