UNPKG

homebridge-lg-ac

Version:

A Homebridge plugin for controlling/monitoring LG AirConditioning device via LG ThinQ platform.

174 lines 8.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LGAcHomebridgePlatform = void 0; const settings_1 = require("./settings"); const helper_1 = require("./helper"); const ThinQ_1 = require("./lib/ThinQ"); const events_1 = require("events"); const constants_1 = require("./lib/constants"); const errors_1 = require("./errors"); /** * 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 LGAcHomebridgePlatform { 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.events = new events_1.EventEmitter(); this.config.devices = this.config.devices || []; this.ThinQ = new ThinQ_1.ThinQ(this, config, log); if (!config.country || !config.language || !((config.username && config.password) || config.refresh_token)) { this.log.error('Missing required config parameter.'); return; } const didFinishLaunching = () => { // run the method to discover / register your devices as accessories this.ThinQ.isReady().then(() => { this.log.info('Successfully connected to the ThinQ API.'); const discoverDevices = () => { this.discoverDevices().then(async () => { await this.startMonitor(); }).catch(err => { if (err instanceof errors_1.NotConnectedError) { setTimeout(() => { discoverDevices(); }, 30000); } else { this.log.error(err.message); this.log.debug(err); } }); }; discoverDevices(); }).catch(err => { var _a; if (err.message === 'Internal Server Error' || ((_a = err.code) === null || _a === void 0 ? void 0 : _a.indexOf('ECONN')) === 0 || err instanceof errors_1.NotConnectedError) { this.log.error('LG ThinQ internal server error, try again later.'); } else { this.log.error('ThinQ API is not ready. please check configuration and try again.'); } this.log.error(err.message); this.log.debug(err); }); }; this.api.on('didFinishLaunching', async () => { this.log.debug('Executed didFinishLaunching callback'); didFinishLaunching(); }); } /** * 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 Homebridge 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); } async discoverDevices() { const accessoriesToRemoveUUID = this.accessories.map(accessory => accessory.UUID); const devices = await this.ThinQ.devices(); if (!devices.length) { this.log.warn('No ThinQ devices in your account.'); } for (const device of devices) { if (device.platform === constants_1.PlatformType.ThinQ1) { this.log.debug('Thinq1 device is skipped: ', device.toString()); continue; } this.log.debug('Device data: ', JSON.stringify(device.data)); if (this.config.devices.length && !this.config.devices.find(enabled => enabled.id === device.id)) { this.log.debug('Device skipped: ', device.id); continue; } this.log.info('[' + device.name + '] Setting up device!'); const setupSuccess = await this.ThinQ.setup(device); if (!setupSuccess) { this.log.warn('[' + device.name + '] Failed to setup device!'); continue; } const accessoryType = helper_1.Helper.make(device); if (accessoryType === null) { this.log.info('Device not supported: ' + device.toString()); continue; } let lgThinQDevice; const existingAccessory = this.accessories.find(accessory => accessory.UUID === device.id); if (existingAccessory) { accessoriesToRemoveUUID.splice(accessoriesToRemoveUUID.indexOf(device.id), 1); this.log.info('Restoring existing accessory:', device.toString()); existingAccessory.context.device = device; lgThinQDevice = new accessoryType(this, existingAccessory); } else { this.log.info('Adding new accessory:', device.toString()); const category = helper_1.Helper.category(device); // create a new accessory const accessory = new this.api.platformAccessory(device.name, device.id, category); accessory.context.device = device; lgThinQDevice = new accessoryType(this, accessory); // link the accessory to your platform this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]); this.accessories.push(accessory); } this.events.on(device.id, lgThinQDevice.update.bind(lgThinQDevice)); // first time update lgThinQDevice.updateAccessoryCharacteristic(device); } const accessoriesToRemove = this.accessories.filter(accessory => accessoriesToRemoveUUID.includes(accessory.UUID)); if (accessoriesToRemove.length) { accessoriesToRemove.map(accessory => { this.log.info('Removing accessory:', accessory.displayName); this.accessories.splice(this.accessories.indexOf(accessory), 1); }); this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, accessoriesToRemove); } } async startMonitor() { // thinq2 device const thinq2devices = this.accessories.filter(accessory => accessory.context.device.platform === constants_1.PlatformType.ThinQ2); if (thinq2devices.length) { setInterval(() => { this.ThinQ.devices().then((devices) => { devices.filter(device => device.platform === constants_1.PlatformType.ThinQ2).forEach(device => { // only emit if device online if (device.snapshot.online) { this.events.emit('refresh.' + device.id, device.snapshot); } }); }); }, 300000); // every 5 minute const refreshList = {}; thinq2devices.forEach(accessory => { const device = accessory.context.device; refreshList[device.id] = setTimeout(() => { this.events.once('refresh.' + device.id, (snapshot) => { this.events.emit(device.id, snapshot); refreshList[device.id].refresh(); }); }, 150000); }); this.log.info('Start MQTT listener for thinq2 device'); await this.ThinQ.registerMQTTListener((data) => { var _a, _b; if ('data' in data && 'deviceId' in data) { this.events.emit(data.deviceId, (_b = (_a = data.data) === null || _a === void 0 ? void 0 : _a.state) === null || _b === void 0 ? void 0 : _b.reported); if (data.deviceId in refreshList) { refreshList[data.deviceId].refresh(); } } }); } } } exports.LGAcHomebridgePlatform = LGAcHomebridgePlatform; //# sourceMappingURL=platform.js.map