UNPKG

homebridge-homewizard-power-consumption

Version:
114 lines (113 loc) 5.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HomewizardPowerConsumption = void 0; const axios_1 = __importDefault(require("axios")); const PowerConsumption_1 = __importDefault(require("./Accessories/PowerConsumption")); const PowerReturn_1 = __importDefault(require("./Accessories/PowerReturn")); class HomewizardPowerConsumption { 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.accessories = []; this.devices = []; this.heartBeatInterval = (config.pollInterval || 60) * 1000; this.api.on('didFinishLaunching', () => { this.initialise(); }); } configureAccessory(accessory) { this.accessories.push(accessory); } validateConfig() { return !!this.config.ip; } async validateIp() { try { const { data } = await axios_1.default.get(`http://${this.config.ip}/api/`, { timeout: 2000 }); if (data && data.product_type === 'HWE-P1') { this.device = data; return true; } return false; } catch (error) { return false; } } async initialise() { if (!this.validateConfig()) { this.log.error('Configuration error. Please provide your Wi-Fi P1 meter\'s IP address'); return; } if (!await this.validateIp()) { this.log.error('Your Wi-Fi P1 meter\'s IP address seems to be incorrect. No connection possible'); return; } this.setupAccessoires(); await this.heartBeat(); setInterval(() => { this.heartBeat(); }, this.heartBeatInterval); } setupAccessoires() { const powerConsumptionName = 'Power Consumption'; const powerConsumptionUuid = this.api.hap.uuid.generate('homewizard-power-consumption'); const powerConsumptionExistingAccessory = this.accessories.find(accessory => accessory.UUID === powerConsumptionUuid); if (this.config.hidePowerConsumptionDevice !== true) { if (powerConsumptionExistingAccessory) { this.devices.push(new PowerConsumption_1.default(this.config, this.log, this.api, powerConsumptionExistingAccessory, this.device)); } else { this.log.info(`${powerConsumptionName} added as accessory`); const accessory = new this.api.platformAccessory(powerConsumptionName, powerConsumptionUuid); this.devices.push(new PowerConsumption_1.default(this.config, this.log, this.api, accessory, this.device)); this.api.registerPlatformAccessories('homebridge-homewizard-power-consumption', 'HomewizardPowerConsumption', [accessory]); } } else { if (powerConsumptionExistingAccessory) { this.api.unregisterPlatformAccessories(powerConsumptionUuid, 'homebridge-homewizard-power-consumption', [powerConsumptionExistingAccessory]); } } const powerReturnName = 'Power Return'; const powerReturnUuid = this.api.hap.uuid.generate('homewizard-power-return'); const powerReturnExistingAccessory = this.accessories.find(accessory => accessory.UUID === powerReturnUuid); if (this.config.hidePowerReturnDevice !== true) { if (powerReturnExistingAccessory) { this.devices.push(new PowerReturn_1.default(this.config, this.log, this.api, powerReturnExistingAccessory, this.device)); } else { this.log.info(`${powerReturnName} added as accessory`); const accessory = new this.api.platformAccessory(powerReturnName, powerReturnUuid); this.devices.push(new PowerReturn_1.default(this.config, this.log, this.api, accessory, this.device)); this.api.registerPlatformAccessories('homebridge-homewizard-power-consumption', 'HomewizardPowerConsumption', [accessory]); } } else { if (powerReturnExistingAccessory) { this.api.unregisterPlatformAccessories(powerReturnUuid, 'homebridge-homewizard-power-consumption', [powerReturnExistingAccessory]); } } } async heartBeat() { try { const { data } = await axios_1.default.get(`http://${this.config.ip}/api/v1/data`); const consumption = data.active_power_w; this.devices.forEach((device) => { device.beat(consumption); }); this.log.debug('heart beat', consumption); } catch (error) { this.log.error('Something went wrong, please double check the Wi-Fi P1 meter\'s IP address'); this.log.debug(error); } } } exports.HomewizardPowerConsumption = HomewizardPowerConsumption;