UNPKG

homebridge-levoit-humidifiers

Version:
197 lines 9.32 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path = __importStar(require("node:path")); const settings_1 = require("./settings"); const VeSyncAccessory_1 = __importDefault(require("./VeSyncAccessory")); const debugMode_1 = __importDefault(require("./debugMode")); const VeSync_1 = __importDefault(require("./api/VeSync")); /** * Platform class that manages the Levoit Humidifiers Homebridge plugin. * Handles device discovery, accessory registration, and lifecycle management. */ class Platform { constructor(log, config, api) { var _a, _b, _c, _d, _e; this.log = log; this.config = config; this.api = api; this.Service = this.api.hap.Service; this.Characteristic = this.api.hap.Characteristic; /** Cached accessories loaded from Homebridge's persistent storage */ this.cachedAccessories = []; /** Currently registered and active device accessories */ this.registeredDevices = []; const { email, password } = (_a = this.config) !== null && _a !== void 0 ? _a : {}; // Support backwards compatibility: check top-level first, then options const enableDebugMode = (_d = (_b = this.config.enableDebugMode) !== null && _b !== void 0 ? _b : (_c = this.config.options) === null || _c === void 0 ? void 0 : _c.enableDebugMode) !== null && _d !== void 0 ? _d : false; this.debugger = new debugMode_1.default(!!enableDebugMode, this.log); this.debugger.debug('[PLATFORM]', 'Debug mode enabled'); const storagePath = this.api.user.storagePath(); const defaultSessionPath = path.join(storagePath, 'homebridge-levoit-humidifiers.session.json'); const sessionPath = ((_e = this.config.options) === null || _e === void 0 ? void 0 : _e.sessionPath) || defaultSessionPath; this.debugger.debug('[PLATFORM]', `Using sessionPath=${sessionPath}`); this.client = new VeSync_1.default(email, password, this.config, this.debugger, log, sessionPath); this.api.on('didFinishLaunching', () => { this.discoverDevices(); }); } /** * Called by Homebridge when it loads cached accessories from persistent storage. * These accessories will be restored if they're still present during device discovery. */ configureAccessory(accessory) { this.log.info('Loading accessory from cache:', accessory.displayName); this.cachedAccessories.push(accessory); } /** * Discovers and loads all Levoit humidifier devices from VeSync. * Called automatically when Homebridge finishes launching. * * Process: * 1. Validates credentials * 2. Authenticates with VeSync API * 3. Fetches device list * 4. Loads/restores each device as an accessory * 5. Removes accessories for devices that no longer exist */ async discoverDevices() { var _a; const { email, password } = (_a = this.config) !== null && _a !== void 0 ? _a : {}; if (!email || !password) { // If credentials are missing, remove all cached accessories if (this.cachedAccessories.length > 0) { this.debugger.debug('[PLATFORM]', 'Removing cached accessories because the email and password are not set (Count:', `${this.cachedAccessories.length})`); this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, this.cachedAccessories); } this.log.error('The email and password are not correct!'); return; } try { this.log.info('Connecting to the VeSync servers...'); const ok = await this.client.startSession(); if (!ok) { this.log.error('VeSync login failed – enable debug mode and check logs for [LOGIN]/[SESSION] messages.'); return; } this.log.info('Discovering devices...'); // Fetch all devices from VeSync and load them as accessories const devices = await this.client.getDevices(); await Promise.all(devices.map(this.loadDevice.bind(this))); // Track which device UUIDs were successfully loaded const loadedDeviceUUIDs = new Set(devices.map((device) => device.uuid)); // Remove accessories for devices that no longer exist this.checkOldDevices(loadedDeviceUUIDs); } catch (err) { const message = err instanceof Error ? err.message : String(err); this.log.error('Unexpected error during device discovery:', message); } } /** * Loads a single device as a Homebridge accessory. * Either restores an existing cached accessory or creates a new one. * * @param device The VeSync device to load */ async loadDevice(device) { try { // Update device info to get current state await device.updateInfo(); const { uuid, name } = device; // Check if this device was previously cached const existingAccessory = this.cachedAccessories.find((accessory) => accessory.UUID === uuid); if (existingAccessory) { // Restore existing accessory from cache this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName); existingAccessory.context = { name, device, }; // Create VeSyncAccessory instance (starts background polling) this.registeredDevices.push(new VeSyncAccessory_1.default(this, existingAccessory)); return; } // Create new accessory for newly discovered device this.log.info('Adding new accessory:', name); const accessory = new this.api.platformAccessory(name, uuid); accessory.context = { name, device, }; // Create VeSyncAccessory instance (starts background polling) and register with Homebridge this.registeredDevices.push(new VeSyncAccessory_1.default(this, accessory)); return this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [ accessory, ]); } catch (error) { const message = error instanceof Error ? error.message : String(error); this.log.error(`Error for device: ${device.name}:${device.uuid} | ${message}`); return null; } } /** * Removes accessories for devices that no longer exist. * Compares cached accessories against currently registered devices * and unregisters any that are no longer present. * * Note: When accessories are unregistered, their polling intervals * will be cleaned up automatically when the VeSyncAccessory instances * are garbage collected. * * @param loadedDeviceUUIDs - Set of UUIDs for devices that were successfully loaded */ checkOldDevices(loadedDeviceUUIDs) { this.cachedAccessories.forEach((accessory) => { const exists = loadedDeviceUUIDs.has(accessory.UUID); if (!exists) { this.log.info('Removing accessory:', accessory.displayName); this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [ accessory, ]); const index = this.cachedAccessories.indexOf(accessory); if (index > -1) { this.cachedAccessories.splice(index, 1); } } }); } } exports.default = Platform; //# sourceMappingURL=platform.js.map