UNPKG

homebridge-fenix-tft-wifi

Version:

Homebridge plugin which adds a Fenix TFT WiFi thermostat as HomeKit device.

111 lines 5.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FenixTFTWifiPlatform = void 0; const settings_1 = require("./settings"); const platformAccessory_1 = require("./platformAccessory"); const ThermostatApi_1 = __importDefault(require("./Api/ThermostatApi")); const FenixApi_1 = __importDefault(require("./Api/FenixApi")); const TokenManager_1 = __importDefault(require("./TokenManager")); const colors_1 = require("./colors"); class FenixTFTWifiPlatform { constructor(log, config, api) { this.log = log; this.config = config; this.api = api; this.Characteristic = this.api.hap.Characteristic; // this is used to track restored cached accessories this.accessories = []; this.log.debug('Finished initializing platform'); this.api.on('didFinishLaunching', () => { log.debug(`${colors_1.GREY}Executed didFinishLaunching callback${colors_1.RESET}`); this.initAccessories() .then(() => this.log.info(`${colors_1.GREEN}Initialized${colors_1.RESET}`)) .catch((error) => this.log.error(`Initialize of plugin was failed ${error}`)); }); } configureAccessory(accessory) { this.log.info('Loading accessory from cache:', accessory.displayName); this.accessories.push(accessory); } async initAccessories() { const tokenManager = new TokenManager_1.default(this.config.accessToken, this.config.refreshToken, this.log, this.api); await tokenManager.loadInitialTokens(); const fenixApi = new FenixApi_1.default(tokenManager); fenixApi.readMyInformation().then((data) => { const devices = []; for (const home of data.data) { for (const room of home.rooms) { for (const sensor of room.sensors) { devices.push({ 'name': sensor.S2, 'uuid': sensor.S1 }); } } } const activeUUIDs = []; const toRegister = []; const toUpdate = []; const toUnregister = []; for (const device of devices) { const uuid = this.api.hap.uuid.generate(device.uuid); activeUUIDs.push(uuid); const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid); const tsApi = new ThermostatApi_1.default(device.uuid, tokenManager); if (existingAccessory) { this.log.info(this.colorizedThermostatIdentifications(device) + 'Restoring existing Fenix TFT thermostat from cache'); existingAccessory.context.device = device; existingAccessory.displayName = device.name; this.createThermostat(existingAccessory, tsApi); toUpdate.push(existingAccessory); continue; } this.log.info(this.colorizedThermostatIdentifications(device) + 'Adding new Fenix TFT thermostat'); const accessory = new this.api.platformAccessory(device.name, uuid); accessory.context.device = device; this.createThermostat(accessory, tsApi); toRegister.push(accessory); } for (const accessory of this.accessories) { if (!activeUUIDs.includes(accessory.UUID)) { this.log.debug(this.colorizedThermostatIdentifications(accessory.context.device) + `${colors_1.RED}Removing unused Fenix TFT thermostat accessory ${colors_1.RESET}`); toUnregister.push(accessory); } } this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, toRegister); try { this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, toUnregister); } catch (error) { this.log.error(`Error while unregistering accessories: ${error}`); } try { this.api.updatePlatformAccessories(toUpdate); } catch (error) { this.log.error(`Error while updating accessories: ${error}`); } }).catch((error) => this.log.error(`Cannot to retrieve base data. Do you have valid token? ${error}`)); } getTemperatureCheckInterval() { this.log.debug(`${colors_1.GREY}Thermostat check interval is ${this.config.temperatureCheckInterval || 30} minutes${colors_1.RESET}`); return (this.config.temperatureCheckInterval || 30) * 60000; } get temperatureUnit() { if (this.config.temperatureUnit === 1) { return this.Characteristic.TemperatureDisplayUnits.FAHRENHEIT; } return this.Characteristic.TemperatureDisplayUnits.CELSIUS; } createThermostat(accessory, tsApi) { const thermostat = new platformAccessory_1.FenixTFTThermostatPlatformAccessory(this, accessory, tsApi, this.temperatureUnit, this.getTemperatureCheckInterval()); thermostat.initialize(); return thermostat; } colorizedThermostatIdentifications(device) { return `${colors_1.LIGHT_GREY}[${device.uuid}]${colors_1.RESET} ${colors_1.BLUE}[${device.name}]${colors_1.RESET}:`; } } exports.FenixTFTWifiPlatform = FenixTFTWifiPlatform; //# sourceMappingURL=platform.js.map