UNPKG

homebridge-overda-uranus

Version:

Exposes sensors data from Uranus or Retus air-monitor stations.

80 lines 4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OverdaHomebridgePlatform = void 0; const settings_1 = require("./settings"); const platformAccessory_1 = require("./platformAccessory"); const AirPressure = require("./overda/customCharacteristic"); let IAirPressureLevel; class OverdaHomebridgePlatform { constructor(log, config, api) { this.log = log; this.config = config; this.api = api; this.Service = this.api.hap.Service; this.accessories = []; this.log.debug('Finished initializing platform:', this.config.name); this.AirPressureLevel = AirPressure(this.api); IAirPressureLevel = this.AirPressureLevel; this.api.on('didFinishLaunching', () => { log.debug('Executed didFinishLaunching callback'); try { this.discoverSensors(); } catch (error) { this.log.error(`[${settings_1.PLATFORM_NAME}] ${settings_1.PLUGIN_NAME} has failed to discoverSensors:`, error.message); } }); // Extends Characteristic for hap with custom AirPressureLevel. this.Characteristic = Object.defineProperty(this.api.hap.Characteristic, 'AirPressureLevel', { value: this.AirPressureLevel }); } configureAccessory(accessory) { this.log.info('Loading accessory from cache:', accessory.displayName); this.accessories.push(accessory); } discoverSensors() { const overdaSensors = this.config.sensors; for (const sensor of overdaSensors) { const uuid = this.api.hap.uuid.generate(sensor.serialNumber); const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid); this.setDisplayName(sensor); if (existingAccessory) { this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName); if (existingAccessory.displayName === sensor.displayName) { existingAccessory.context.sensor = sensor; try { new platformAccessory_1.OverdaPlatformAccessory(this, existingAccessory); this.api.updatePlatformAccessories([existingAccessory]); } catch (error) { this.log.error(`[${sensor.displayName}] has failed to updatePlatformAccessories:`, error.message); } continue; } // unregister cached accessory if name from config not identical try { this.log.info('Unregistering existing accessory:', `${existingAccessory.displayName} -> ${sensor.displayName}`); this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [existingAccessory]); } catch (error) { this.log.error(`[${sensor.displayName}] has failed to unregisterPlatformAccessories:`, error.message); } } this.log.info('Adding new accessory:', sensor.displayName); const accessory = new this.api.platformAccessory(sensor.displayName, uuid); accessory.context.sensor = sensor; try { new platformAccessory_1.OverdaPlatformAccessory(this, accessory); this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]); } catch (error) { this.log.error(`[${sensor.displayName}] has failed to registerPlatformAccessory:`, error.message); } } } // setDisplayName sets displayName as per config value or serialNumber setDisplayName(sensor) { sensor.displayName = sensor.displayName || sensor.serialNumber; } } exports.OverdaHomebridgePlatform = OverdaHomebridgePlatform; //# sourceMappingURL=platform.js.map