homebridge-levoit-humidifiers
Version:
Homebridge plugin for Levoit Humidifiers
92 lines • 4.14 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const settings_1 = require("./settings");
const VeSyncAccessory_1 = __importDefault(require("./VeSyncAccessory"));
const debugMode_1 = __importDefault(require("./debugMode"));
const VeSync_1 = __importDefault(require("./api/VeSync"));
class Platform {
constructor(log, config, api) {
var _a;
this.log = log;
this.config = config;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.cachedAccessories = [];
this.registeredDevices = [];
const { email, password, enableDebugMode } = (_a = this.config) !== null && _a !== void 0 ? _a : {};
this.debugger = new debugMode_1.default(!!enableDebugMode, this.log);
this.debugger.debug('[PLATFORM]', 'Debug mode enabled');
this.client = new VeSync_1.default(email, password, this.config, this.debugger, log);
this.api.on('didFinishLaunching', () => {
this.discoverDevices();
});
}
configureAccessory(accessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
this.cachedAccessories.push(accessory);
}
async discoverDevices() {
var _a;
const { email, password } = (_a = this.config) !== null && _a !== void 0 ? _a : {};
if (!email || !password) {
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!');
}
this.log.info('Connecting to the servers...');
await this.client.startSession();
this.log.info('Discovering devices...');
const devices = await this.client.getDevices();
await Promise.all(devices.map(this.loadDevice.bind(this)));
this.checkOldDevices();
}
async loadDevice(device) {
try {
await device.updateInfo();
const { uuid, name } = device;
const existingAccessory = this.cachedAccessories.find((accessory) => accessory.UUID === uuid);
if (existingAccessory) {
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
existingAccessory.context = {
name,
device,
};
this.registeredDevices.push(new VeSyncAccessory_1.default(this, existingAccessory));
return;
}
this.log.info('Adding new accessory:', name);
const accessory = new this.api.platformAccessory(name, uuid);
accessory.context = {
name,
device,
};
this.registeredDevices.push(new VeSyncAccessory_1.default(this, accessory));
return this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [
accessory,
]);
}
catch (error) {
this.log.error(`Error for device: ${device.name}:${device.uuid} | ${error.message}`);
return null;
}
}
checkOldDevices() {
this.cachedAccessories.map((accessory) => {
const exists = this.registeredDevices.find((device) => device.UUID === accessory.UUID);
if (!exists) {
this.log.info('Remove cached accessory:', accessory.displayName);
this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [
accessory,
]);
}
});
}
}
exports.default = Platform;
//# sourceMappingURL=platform.js.map