UNPKG

homebridge-framework

Version:
202 lines (201 loc) 8.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var service_1 = require("./service"); /** * Represents a wrapper around HAP accessories with with support for auto-removal of unused services. */ var Accessory = /** @class */ (function () { /** * Initializes a new Accessory instance. * @param platform The homebridge 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. * @param isExternal Determines whether the accessory is an external accessory (in contrast to bridged accessories). */ function Accessory(platform, name, id, subType, category, isExternal) { /** * Contains a value that determines whether the accessory is an external accessory (in contrast to bridged accessories). */ this._isExternal = false; /** * Contains the services. */ this._services = new Array(); this._platform = platform; this._name = name; this._id = id; this._subType = subType || null; this._isExternal = isExternal || false; // Checks if the accessory has been cached if (!this.isExternal) { var platformAccessory = this.platform.cachedPlatformAccessories.find(function (a) { return a.context.id === id && a.context.subType === (subType || null); }); if (platformAccessory) { this._platformAccessory = platformAccessory; return; } } // Creates the new accessory this._platformAccessory = new this.platform.api.platformAccessory(name, this.platform.api.hap.uuid.generate(this.uniqueId), category); this.platformAccessory.context.id = id; this.platformAccessory.context.subType = (subType || null); // Registers the accessory if (!this.isExternal) { this.platform.api.registerPlatformAccessories(this.platform.pluginName, this.platform.platformName, [this.platformAccessory]); } } Object.defineProperty(Accessory.prototype, "platform", { /** * Gets the parent platform. * @internal */ get: function () { return this._platform; }, enumerable: true, configurable: true }); Object.defineProperty(Accessory.prototype, "platformAccessory", { /** * Gets the platform accessory. * @internal */ get: function () { return this._platformAccessory; }, enumerable: true, configurable: true }); Object.defineProperty(Accessory.prototype, "name", { /** * Gets the name that should be displayed in HomeKit. */ get: function () { return this._name; }, enumerable: true, configurable: true }); Object.defineProperty(Accessory.prototype, "id", { /** * Gets the identifier of the accessory. Is unique in combination with the sub type. */ get: function () { return this._id; }, enumerable: true, configurable: true }); Object.defineProperty(Accessory.prototype, "subType", { /** * Gets the sub type of the accessory. Is unique in combination with the ID. */ get: function () { return this._subType; }, enumerable: true, configurable: true }); Object.defineProperty(Accessory.prototype, "isExternal", { /** * Gets a value that determines whether the accessory is an external accessory (in contrast to bridged accessories). */ get: function () { return this._isExternal; }, enumerable: true, configurable: true }); Object.defineProperty(Accessory.prototype, "uniqueId", { /** * Gets the unique identifier of the accessory, which is made up from the ID and the sub type. */ get: function () { return this.id + "-" + (this.subType || ''); }, enumerable: true, configurable: true }); /** * Updates the accessory information service. * @param information The accessory information. */ Accessory.prototype.setInformation = function (information) { // Makes sure the accessory information service is used var accessoryInformationService = this.useService(this.platform.api.hap.Service.AccessoryInformation, this.name); // Updates the information characteristics if (information.manufacturer != null) { accessoryInformationService.useCharacteristic(this.platform.api.hap.Characteristic.Manufacturer, information.manufacturer); } if (information.model != null) { accessoryInformationService.useCharacteristic(this.platform.api.hap.Characteristic.Model, information.model); } if (information.serialNumber != null) { accessoryInformationService.useCharacteristic(this.platform.api.hap.Characteristic.SerialNumber, information.serialNumber); } if (information.firmwareRevision != null) { accessoryInformationService.useCharacteristic(this.platform.api.hap.Characteristic.FirmwareRevision, information.firmwareRevision); } if (information.hardwareRevision != null) { accessoryInformationService.useCharacteristic(this.platform.api.hap.Characteristic.HardwareRevision, information.hardwareRevision); } }; Object.defineProperty(Accessory.prototype, "services", { /** * Gets the services. */ get: function () { return this._services; }, enumerable: true, configurable: true }); /** * Defines a service for usage with the accessory. When defining a service, it is marked as used and thus not removed from HomeKit after the initialization. * @param type The type of the service. * @param name The name that should be displayed in HomeKit. * @param subType The sub type of the service. May be omitted if the type is already unique. */ Accessory.prototype.useService = function (type, name, subType) { // Checks if the service has already been defined for usage var service = this.services.find(function (s) { return s.type === type && s.subType === (subType || null); }); if (service) { return service; } // Creates a new service and returns it service = new service_1.Service(this, type, name, subType); this.services.push(service); return service; }; /** * Removes all cached services that have not been defined for usage. */ Accessory.prototype.removeUnusedServices = function () { var services = this.platformAccessory.services.slice(); var _loop_1 = function (service) { // The accessory information service is always required if (service.UUID === this_1.platform.api.hap.Service.AccessoryInformation.UUID) { return "continue"; } // Removes the unused services if (service.subtype) { if (!this_1.services.some(function (d) { return service.UUID === d.hapService.UUID && service.subtype === d.hapService.subtype; })) { this_1.platformAccessory.removeService(service); } } else { if (!this_1.services.some(function (d) { return service.UUID === d.hapService.UUID && !d.hapService.subtype; })) { this_1.platformAccessory.removeService(service); } } }; var this_1 = this; for (var _i = 0, services_1 = services; _i < services_1.length; _i++) { var service = services_1[_i]; _loop_1(service); } }; return Accessory; }()); exports.Accessory = Accessory;