UNPKG

homebridge-lirc-tv

Version:
80 lines 4.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LIRC = void 0; const settings_1 = require("./settings"); const platformAccessory_1 = require("./platformAccessory"); /** * HomebridgePlatform * This class is the main constructor for your plugin, this is where you should * parse the user config and discover/register accessories with Homebridge. */ class LIRC { constructor(log, config, api) { this.log = log; this.config = config; this.api = api; this.Service = this.api.hap.Service; this.Characteristic = this.api.hap.Characteristic; // this is used to track restored cached accessories this.accessories = []; this.log.debug('Finished initializing platform:', settings_1.PLATFORM_NAME); // When this event is fired it means Homebridge has restored all cached accessories from disk. // Dynamic Platform plugins should only register new accessories after this event was fired, // in order to ensure they weren't added to homebridge already. This event can also be used // to start discovery of new accessories. this.api.on("didFinishLaunching" /* DID_FINISH_LAUNCHING */, () => { log.debug('Executed didFinishLaunching callback'); // run the method to discover / register your devices as accessories this.discoverDevices(); }); } /** * This function is invoked when homebridge restores cached accessories from disk at startup. * It should be used to setup event handlers for characteristics and update respective values. */ configureAccessory(accessory) { this.log.info('Restoring accessory from cache:', accessory.displayName); // create the accessory handler // this is imported from `platformAccessory.ts` new platformAccessory_1.LIRCTelevision(this, accessory); // add the restored accessory to the accessories cache so we can track if it has already been registered this.accessories.push(accessory); } /** * This is an example method showing how to register discovered accessories. * Accessories must only be registered once, previously created accessories * must not be registered again to prevent "duplicate UUID" errors. */ discoverDevices() { // loop over the configured devices and register each one if it has not already been registered for (const device of this.config.devices) { // generate a unique id for the accessory this should be generated from // something globally unique, but constant, for example, the device serial // number or MAC address const uuid = this.api.hap.uuid.generate(device.name); // check that the device has not already been registered by checking the // cached devices we stored in the `configureAccessory` method above if (!this.accessories.find((accessory) => accessory.UUID === uuid)) { this.log.info('Registering new accessory:', device.name); // create a new accessory const accessory = new this.api.platformAccessory(device.name, uuid); // set the accessory category accessory.category = 31 /* TELEVISION */; // store a copy of the device object in the `accessory.context` // the `context` property can be used to store any data about the accessory you may need accessory.context.device = device; // create the accessory handler // this is imported from `platformAccessory.ts` new platformAccessory_1.LIRCTelevision(this, accessory); // link the accessory to your platform this.api.publishExternalAccessories(settings_1.PLUGIN_NAME, [accessory]); // push into accessory cache this.accessories.push(accessory); // it is possible to remove platform accessories at any time using `api.unregisterPlatformAccessories`, eg.: // this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]); } } } } exports.LIRC = LIRC; //# sourceMappingURL=platform.js.map