@pietrolubini/homebridge-ecoflow
Version:
Homebridge plugin for EcoFlow devices
115 lines • 5.91 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PowerStreamAccessory = void 0;
const ecoFlowAccessoryWithQuotaBase_1 = require("@ecoflow/accessories/ecoFlowAccessoryWithQuotaBase");
const powerStreamMqttApiContracts_1 = require("@ecoflow/accessories/powerstream/interfaces/powerStreamMqttApiContracts");
const brightnessService_1 = require("@ecoflow/accessories/powerstream/services/brightnessService");
const powerDemandService_1 = require("@ecoflow/accessories/powerstream/services/powerDemandService");
const characteristicContracts_1 = require("@ecoflow/characteristics/characteristicContracts");
const config_1 = require("@ecoflow/config");
const outletReadOnlyService_1 = require("@ecoflow/services/outletReadOnlyService");
class PowerStreamAccessory extends ecoFlowAccessoryWithQuotaBase_1.EcoFlowAccessoryWithQuotaBase {
inverterOutletService;
solarOutletService;
batteryOutletService;
inverterBrightnessService;
inverterPowerDemandService;
constructor(platform, accessory, config, log, httpApiManager, mqttApiManager, batteryStatusProvider) {
super(platform, accessory, config, log, httpApiManager, mqttApiManager);
this.inverterOutletService = new outletReadOnlyService_1.OutletReadOnlyService(this, batteryStatusProvider, 'INV', config.powerStream?.inverterAdditionalCharacteristics);
this.solarOutletService = new outletReadOnlyService_1.OutletReadOnlyService(this, batteryStatusProvider, 'PV', config.powerStream?.pvAdditionalCharacteristics);
this.batteryOutletService = new outletReadOnlyService_1.OutletReadOnlyService(this, batteryStatusProvider, 'BAT', config.powerStream?.batteryAdditionalCharacteristics);
this.inverterBrightnessService = new brightnessService_1.BrightnessService(this, 1023);
this.inverterPowerDemandService = new powerDemandService_1.PowerDemandService(this, (config.powerStream?.type ?? config_1.PowerStreamConsumptionType.W600) * 10);
}
getServices() {
return [
this.inverterOutletService,
this.solarOutletService,
this.batteryOutletService,
this.inverterBrightnessService,
this.inverterPowerDemandService,
];
}
processQuotaMessage(message) {
const powerStreamMessage = message;
if (powerStreamMessage.cmdFunc === powerStreamMqttApiContracts_1.PowerStreamMqttMessageFuncType.Func20 &&
powerStreamMessage.cmdId === powerStreamMqttApiContracts_1.PowerStreamMqttMessageType.Heartbeat) {
const heartbeat = message.param;
Object.assign(this.quota['20_1'], heartbeat);
this.updateHeartbeatValues(heartbeat);
}
}
initializeQuota(quota) {
const result = quota ?? {};
if (!result['20_1']) {
result['20_1'] = {};
}
return result;
}
updateInitialValues(initialData) {
this.updateHeartbeatInitialValues(initialData['20_1']);
}
updateHeartbeatInitialValues(params) {
const message = {
cmdFunc: powerStreamMqttApiContracts_1.PowerStreamMqttMessageFuncType.Func20,
cmdId: powerStreamMqttApiContracts_1.PowerStreamMqttMessageType.Heartbeat,
param: params,
};
this.processQuotaMessage(message);
}
updateHeartbeatValues(params) {
this.updateSolarValues(params);
this.updateBatteryValues(params);
this.updateInverterValues(params);
}
updateSolarValues(params) {
if (params.pv1InputWatts !== undefined || params.pv2InputWatts !== undefined) {
const pvWatts = this.sum(params.pv1InputWatts, params.pv2InputWatts) * 0.1;
this.solarOutletService.updateState(pvWatts > 0);
this.solarOutletService.updateOutputConsumption(pvWatts);
}
}
updateBatteryValues(params) {
if (params.batInputWatts !== undefined) {
const batInputWatts = params.batInputWatts * 0.1;
if (batInputWatts >= 0) {
this.batteryOutletService.updateState(batInputWatts > 0);
this.batteryOutletService.updateChargingState(false);
this.batteryOutletService.updateOutputConsumption(batInputWatts);
}
if (batInputWatts <= 0) {
const watts = Math.abs(batInputWatts);
this.batteryOutletService.updateChargingState(watts > 0);
this.batteryOutletService.updateInputConsumption(watts);
}
}
if (params.batSoc !== undefined && params.lowerLimit !== undefined) {
this.batteryOutletService.updateBatteryLevel(params.batSoc, params.lowerLimit);
}
}
updateInverterValues(params) {
if (params.invOutputWatts !== undefined) {
const invOutputWatts = params.invOutputWatts * 0.1;
if (invOutputWatts >= 0) {
this.inverterOutletService.updateOutputConsumption(invOutputWatts);
}
if (invOutputWatts <= 0) {
this.inverterOutletService.updateInputConsumption(Math.abs(invOutputWatts));
}
}
if (params.invOnOff !== undefined) {
this.inverterOutletService.updateState(params.invOnOff === characteristicContracts_1.EnableType.On);
}
if (params.invBrightness !== undefined) {
this.inverterBrightnessService.updateState(params.invBrightness > 0);
this.inverterBrightnessService.updateBrightness(params.invBrightness);
}
if (params.permanentWatts !== undefined) {
this.inverterPowerDemandService.updateState(params.permanentWatts > 0);
this.inverterPowerDemandService.updateRotationSpeed(params.permanentWatts);
}
}
}
exports.PowerStreamAccessory = PowerStreamAccessory;
//# sourceMappingURL=powerStreamAccessory.js.map