homebridge-framework
Version:
Framework for easy creation of homebridge plugins.
242 lines (241 loc) • 11.3 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var accessory_1 = require("./accessory");
/**
* Represents the base class for a platform on homebridge. It exposes the homebridge API, logging, configuration and lifecycle events.
*/
var HomebridgePlatform = /** @class */ (function () {
function HomebridgePlatform() {
/**
* Contains the homebridge API.
*/
this._api = null;
/**
* Contains the logger.
*/
this._logger = null;
/**
* Contains the platform configuration.
*/
this._configuration = null;
/**
* Contains the cached accessories.
*/
this._cachedPlatformAccessories = null;
/**
* Contains the accessories.
*/
this._accessories = new Array();
}
/**
* Is called when the platform is registered.
* @param registration The registration object that is created during platform registration.
* @internal
*/
HomebridgePlatform.prototype.register = function (registration) {
var _this = this;
if (registration.api == null || registration.logger == null) {
throw new Error("Error while registering the platform.");
}
// Checks if a configuration is provided, i.e. the plugin should be loaded
if (registration.configuration == null) {
return;
}
// Sets the api, logger and configuration
this._api = registration.api;
this._logger = registration.logger;
this._configuration = registration.configuration;
this._cachedPlatformAccessories = registration.cachedPlatformAccessories;
// Subscribes for the events of the API
this.api.on('didFinishLaunching', function () { return __awaiter(_this, void 0, void 0, function () {
var result, _i, _a, accessory;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
result = this.initialize();
if (!(result instanceof Promise)) return [3 /*break*/, 2];
return [4 /*yield*/, result];
case 1:
_b.sent();
_b.label = 2;
case 2:
// Removes accessories that are unused at this point
this.removeUnusedAccessories();
// Removes services that are unused at this point
for (_i = 0, _a = this.accessories; _i < _a.length; _i++) {
accessory = _a[_i];
accessory.removeUnusedServices();
}
// Publishes the external accessories, which must be done at this point, as all services and characteristics have to be configured before publishing
if (this.accessories.some(function (a) { return a.isExternal; })) {
this.api.publishExternalAccessories(this.pluginName, this.accessories.filter(function (a) { return a.isExternal; }).map(function (a) { return a.platformAccessory; }));
}
return [2 /*return*/];
}
});
}); });
this.api.on('shutdown', function () { return __awaiter(_this, void 0, void 0, function () {
var result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
result = this.destroy();
if (!(result instanceof Promise)) return [3 /*break*/, 2];
return [4 /*yield*/, result];
case 1:
_a.sent();
_a.label = 2;
case 2: return [2 /*return*/];
}
});
}); });
};
/**
* Is called when the platform is registered and cached accessories are loaded. Overwrite this function to add new accessories.
* @returns Returns either void or a promise if you want to intialize accessories asynchronously.
*/
HomebridgePlatform.prototype.initialize = function () { };
/**
* Is called when the platform is destroyed and homebridge is shut down. Overwrite this function to close open connections.
* @returns Returns either void or a promise if you want to close connections asynchronously.
*/
HomebridgePlatform.prototype.destroy = function () { };
Object.defineProperty(HomebridgePlatform.prototype, "api", {
/**
* Gets the homebridge API.
* @internal
*/
get: function () {
if (this._api == null) {
throw new Error("The platform has not been registered yet.");
}
return this._api;
},
enumerable: true,
configurable: true
});
Object.defineProperty(HomebridgePlatform.prototype, "logger", {
/**
* Gets the logger.
*/
get: function () {
if (this._logger == null) {
throw new Error("The platform has not been registered yet.");
}
return this._logger;
},
enumerable: true,
configurable: true
});
Object.defineProperty(HomebridgePlatform.prototype, "configuration", {
/**
* Gets the platform configuration.
*/
get: function () {
if (this._configuration == null) {
throw new Error("The platform has not been registered yet.");
}
return this._configuration;
},
enumerable: true,
configurable: true
});
Object.defineProperty(HomebridgePlatform.prototype, "cachedPlatformAccessories", {
/**
* Gets the cached accessories.
* @internal
*/
get: function () {
if (this._cachedPlatformAccessories == null) {
throw new Error("The platform has not been registered yet.");
}
return this._cachedPlatformAccessories;
},
enumerable: true,
configurable: true
});
Object.defineProperty(HomebridgePlatform.prototype, "accessories", {
/**
* Gets the accessories.
*/
get: function () {
return this._accessories;
},
enumerable: true,
configurable: true
});
/**
* Defines an accessory for usage with the platform. When defining an accessory, it is marked as used and thus not removed from HomeKit after the initialization.
* @param name The name that should be displayed in HomeKit.
* @param id The identifier of the accessory.
* @param subType The sub type of the accessory. May be omitted if the ID is already unique.
* @param category The category of the accessory, which determines the icon in the Apple Home app.
* @param isExternal Determines whether the accessory is an external accessory (in contrast to bridged accessories).
*/
HomebridgePlatform.prototype.useAccessory = function (name, id, subType, category, isExternal) {
// Checks if the accessory has already been defined for usage
var accessory = this.accessories.find(function (a) { return a.id === id && a.subType === (subType || null); });
if (accessory) {
return accessory;
}
// Creates a new accessory and returns it
accessory = new accessory_1.Accessory(this, name, id, subType, category, isExternal);
this.accessories.push(accessory);
return accessory;
};
/**
* Defines an external accessory for usage with the platform.
* @param name The name that should be displayed in HomeKit.
* @param id The identifier of the accessory.
* @param subType The sub type of the accessory. May be omitted if the ID is already unique.
* @param category The category of the accessory, which determines the icon in the Apple Home app.
*/
HomebridgePlatform.prototype.useExternalAccessory = function (name, id, subType, category) {
return this.useAccessory(name, id, subType, category, true);
};
/**
* Unregisters all cached accessories that have not been defined for usage.
*/
HomebridgePlatform.prototype.removeUnusedAccessories = function () {
var _this = this;
this.api.unregisterPlatformAccessories(this.pluginName, this.platformName, this.cachedPlatformAccessories.filter(function (c) { return !_this.accessories.some(function (d) { return c.context.id === d.id && c.context.subType === d.subType; }); }));
};
return HomebridgePlatform;
}());
exports.HomebridgePlatform = HomebridgePlatform;