UNPKG

homebridge-daikin-smart-ac

Version:

A Homebridge plugin for Daikin Wifi Adapter AC using Local API

110 lines 4.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Platform = void 0; const settings_1 = require("./settings"); const Accessory_1 = require("./Accessory"); const DaikinAC_1 = require("./DaikinAC"); class Platform { 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.cachedAccessories = []; this.registeredDevices = []; 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; try { if (!((_a = this.config.ips) === null || _a === void 0 ? void 0 : _a.length)) { this.cleanAccessories(); return; } const devices = Array.from(new Set(this.config.ips)).map(async (ip) => { const daikinAC = new DaikinAC_1.DaikinAC(this.log, this.api, ip); const success = await daikinAC.setup(); if (!success) { this.log.warn(`Failed to setup Daikin AC (${ip})`); return; } this.log.info(`Successfully setup Daikin AC (${ip})`); this.loadDevice(daikinAC); }); await Promise.all(devices); this.checkOldDevices(); } catch (error) { this.log.error(`Error for discoverDevices: ${error === null || error === void 0 ? void 0 : error.message}`); } } checkOldDevices() { this.cachedAccessories.map((accessory) => { var _a, _b; try { if (!((_a = accessory.context) === null || _a === void 0 ? void 0 : _a.ip)) { return; } const exists = (_b = this.config.ips) === null || _b === void 0 ? void 0 : _b.find((ip) => { var _a; return ip === ((_a = accessory.context) === null || _a === void 0 ? void 0 : _a.ip); }); if (!exists) { this.log.info('Remove cached accessory:', accessory.displayName); this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [ accessory ]); } } catch (error) { this.log.error(`Error for device: ${accessory.displayName} | ${error === null || error === void 0 ? void 0 : error.message}`); } }); } cleanAccessories() { try { if (this.cachedAccessories.length > 0) { this.log.debug('[PLATFORM]', 'Removing cached accessories (Count:', `${this.cachedAccessories.length})`); this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, this.cachedAccessories); } } catch (error) { this.log.error(`Error for cached accessories: ${error === null || error === void 0 ? void 0 : error.message}`); } } loadDevice(daikinAC) { try { const existingAccessory = this.cachedAccessories.find((accessory) => accessory.UUID === daikinAC.UUID); if (existingAccessory) { this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName); existingAccessory.context = { name: existingAccessory.displayName, ip: daikinAC.ip, daikinAC }; this.registeredDevices.push(new Accessory_1.Accessory(this, existingAccessory)); return; } this.log.info('Adding new accessory:', daikinAC.ip); const accessory = new this.api.platformAccessory(daikinAC.DefaultName, daikinAC.UUID); accessory.context = { name: accessory.displayName, ip: daikinAC.ip, daikinAC }; this.registeredDevices.push(new Accessory_1.Accessory(this, accessory)); return this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [ accessory ]); } catch (error) { this.log.error(`Error loadDevice for ${daikinAC.ip}: ${error === null || error === void 0 ? void 0 : error.message}`); } } } exports.Platform = Platform; //# sourceMappingURL=platform.js.map