UNPKG

homebridge-purpleair-sensor

Version:
87 lines 4.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PurpleAirPlatform = void 0; const platformAccessory_1 = require("./platformAccessory"); const settings_1 = require("./settings"); /** * 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 PurpleAirPlatform { 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:', this.config.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', () => { log.debug('Executed didFinishLaunching callback'); 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('Loading accessory from cache:', accessory.displayName); // 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() { const registeredAccessories = []; // Loop through the platform config and register each sensor as an accessory const sensors = this.config.sensors || []; for (const sensor of sensors) { this.log.debug('Configuring sensor:', JSON.stringify(sensor)); const uuid = this.api.hap.uuid.generate(sensor.name); registeredAccessories.push(uuid); // see if an accessory with the same uuid has already been registered and restored from // the cached devices we stored in the `configureAccessory` method above const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid); if (existingAccessory) { // the accessory already exists this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName); // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.: existingAccessory.context.sensor = sensor; this.api.updatePlatformAccessories([existingAccessory]); // create the accessory handler for the restored accessory new platformAccessory_1.PurpleAirPlatformAccessory(this, existingAccessory); } else { // the accessory does not yet exist, so we need to create it this.log.info('Adding new accessory:', sensor.name); // create a new accessory const accessory = new this.api.platformAccessory(sensor.name, uuid); // 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.sensor = sensor; // create the accessory handler for the newly created accessory new platformAccessory_1.PurpleAirPlatformAccessory(this, accessory); // link the accessory to your platform this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]); } } // Remove any existing accessories that are no longer configured const accessoriesToRemove = this.accessories.filter(accessory => !registeredAccessories.includes(accessory.UUID)); if (accessoriesToRemove.length > 0) { this.log.info(`Unregistering ${accessoriesToRemove.length} accessories no longer in config file`); this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, accessoriesToRemove); } } } exports.PurpleAirPlatform = PurpleAirPlatform; //# sourceMappingURL=platform.js.map