UNPKG

homebridge-hivehome-control

Version:

Allows control of Hive devices, including water heaters, through Homebridge.

131 lines 6.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HiveHomeControllerPlatform = void 0; const hiveHeatingAccessory_1 = require("./hivehome/accessories/hiveHeatingAccessory"); const hiveHotWaterAccessory_1 = require("./hivehome/accessories/hiveHotWaterAccessory"); const hive_api_1 = require("./hivehome/hive-api"); const hive_helpers_1 = require("./hivehome/hive-helpers"); const settings_1 = require("./settings"); const log_1 = require("./util/log"); // How long we wait after a failed discovery attempt before retrying. const kDiscoveryRefreshInterval = 5000; /** * This class is the entry point for the plugin. It is responsible for parsing * the user config, discovering accessories, and registering them. */ class HiveHomeControllerPlatform { constructor(logger, config, api) { this.logger = logger; this.config = config; this.api = api; this.Service = this.api.hap.Service; this.Characteristic = this.api.hap.Characteristic; // This array is used to track restored cached accessories. this.cachedAccessories = []; // This array records the handlers which wrap each accessory. this.accessoryHandlers = []; // Classes for reflective instantiation based on Hive type. this.hiveAccessories = { [hive_api_1.HiveType.kHeating]: hiveHeatingAccessory_1.HiveHeatingAccessory, [hive_api_1.HiveType.kRadiator]: hiveHeatingAccessory_1.HiveHeatingAccessory, [hive_api_1.HiveType.kHotWater]: hiveHotWaterAccessory_1.HiveHotWaterAccessory, }; // Configure the custom log with the Homebridge logger and debug config. log_1.Log.configure(logger, config.enableDebugLog); // If the config is not valid, bail out immediately. We will not discover // any new accessories or register any handlers for cached accessories. const validationErrors = this.validateConfig(config); if (validationErrors.length > 0) { log_1.Log.error('Plugin suspended. Invalid configuration:', validationErrors); return; } // Notify the user that we have completed platform initialization. log_1.Log.debug('Finished initializing platform'); // This event is fired when Homebridge has restored all cached accessories. // We must add handlers for these, and check for any new accessories. this.api.on('didFinishLaunching', () => { log_1.Log.debug('Finished restoring all cached accessories from disk'); this.discoverDevices(); }); } // Validate that the plugin configuration conforms to the expected format. validateConfig(config) { const validationErrors = []; if (!config.hiveUsername) { validationErrors.push('No Hive username specified'); } if (!config.hivePassword) { validationErrors.push('No Hive password specified'); } if (!config.deviceGroupKey) { validationErrors.push('No Hive Device Group Key specified'); } if (!config.deviceKey) { validationErrors.push('No Hive Device Key specified'); } if (!config.devicePassword) { validationErrors.push('No Hive Device Password specified'); } if (config.heatingBoostMins <= 0) { validationErrors.push('Heating Boost Duration is not > 0'); } if (config.heatingBoostTemp <= 0) { validationErrors.push('Heating Boost Temperature is not > 0'); } if (config.hotWaterBoostMins <= 0) { validationErrors.push('Hot Water Boost Duration is not > 0'); } return validationErrors; } /** * This function is invoked for each cached accessory that homebridge restores * from disk at startup. Here we add the cached accessories to a list which * will be examined later during the 'discoverDevices' phase. */ configureAccessory(accessory) { log_1.Log.info('Loading accessory from cache:', accessory.displayName); this.cachedAccessories.push(accessory); } /** * Discover and register accessories. Accessories must only be registered * once; previously created accessories must not be registered again, to * avoid "duplicate UUID" errors. */ async discoverDevices() { // Discover accessories. If we fail to discover anything, schedule another // discovery attempt in the future. const hiveSession = await (0, hive_helpers_1.startHiveSession)(this.config); if (!hiveSession) { log_1.Log.error('Login failed. Please check your credentials.'); return; } const deviceList = await (0, hive_helpers_1.getHiveDeviceList)(hiveSession); log_1.Log.debug('Discovered devices:', deviceList); // Iterate over the discovered devices and create handlers for each. for await (const hiveDevice of deviceList) { // Generate a unique id for the accessory from its device ID. const uuid = this.api.hap.uuid.generate(await hiveDevice[hive_api_1.HiveData.kId]); const deviceType = await hiveDevice[hive_api_1.HiveData.kType]; const displayName = `${await hiveDevice[hive_api_1.HiveData.kName]} ${hive_api_1.HiveTypeName[deviceType]}`; // See if an accessory with the same uuid already exists. let accessory = this.cachedAccessories.find(accessory => accessory.UUID === uuid); // If the accessory does not yet exist, we need to create it. if (!accessory) { log_1.Log.info('Adding new accessory:', displayName); accessory = new this.api.platformAccessory(displayName, uuid); this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]); } // Make sure the accessory stays in sync with any device config changes. accessory.context.device = hiveDevice; this.api.updatePlatformAccessories([accessory]); // Create the accessory handler for this accessory. this.accessoryHandlers.push(new this.hiveAccessories[deviceType](this, accessory, hiveSession)); } if (this.accessoryHandlers.length === 0) { log_1.Log.warn('Failed to find devices. Retry in', kDiscoveryRefreshInterval, 'ms'); setTimeout(() => this.discoverDevices(), kDiscoveryRefreshInterval); } } } exports.HiveHomeControllerPlatform = HiveHomeControllerPlatform; //# sourceMappingURL=platform.js.map