feat: added support unit systems for pressure and flow rate

This commit is contained in:
Yurii
2024-04-06 18:19:06 +03:00
parent a7a561622e
commit 0a8dd2a076
4 changed files with 79 additions and 16 deletions

View File

@@ -1099,7 +1099,7 @@ public:
return this->publish(this->getTopic(FPSTR(HA_ENTITY_SENSOR), F("modulation")).c_str(), doc); return this->publish(this->getTopic(FPSTR(HA_ENTITY_SENSOR), F("modulation")).c_str(), doc);
} }
bool publishSensorPressure(bool enabledByDefault = true) { bool publishSensorPressure(UnitSystem unit = UnitSystem::METRIC, bool enabledByDefault = true) {
JsonDocument doc; JsonDocument doc;
doc[FPSTR(HA_AVAILABILITY)][0][FPSTR(HA_TOPIC)] = this->getDeviceTopic(F("status")); doc[FPSTR(HA_AVAILABILITY)][0][FPSTR(HA_TOPIC)] = this->getDeviceTopic(F("status"));
doc[FPSTR(HA_AVAILABILITY)][1][FPSTR(HA_TOPIC)] = this->getDeviceTopic(F("state")); doc[FPSTR(HA_AVAILABILITY)][1][FPSTR(HA_TOPIC)] = this->getDeviceTopic(F("state"));
@@ -1111,7 +1111,14 @@ public:
doc[FPSTR(HA_ENTITY_CATEGORY)] = F("diagnostic"); doc[FPSTR(HA_ENTITY_CATEGORY)] = F("diagnostic");
doc[FPSTR(HA_DEVICE_CLASS)] = F("pressure"); doc[FPSTR(HA_DEVICE_CLASS)] = F("pressure");
doc[FPSTR(HA_STATE_CLASS)] = F("measurement"); doc[FPSTR(HA_STATE_CLASS)] = F("measurement");
doc[FPSTR(HA_UNIT_OF_MEASUREMENT)] = F("bar");
if (unit == UnitSystem::METRIC) {
doc[FPSTR(HA_UNIT_OF_MEASUREMENT)] = F("bar");
} else if (unit == UnitSystem::IMPERIAL) {
doc[FPSTR(HA_UNIT_OF_MEASUREMENT)] = F("psi");
}
doc[FPSTR(HA_NAME)] = F("Pressure"); doc[FPSTR(HA_NAME)] = F("Pressure");
doc[FPSTR(HA_ICON)] = F("mdi:gauge"); doc[FPSTR(HA_ICON)] = F("mdi:gauge");
doc[FPSTR(HA_STATE_TOPIC)] = this->getDeviceTopic(F("state")); doc[FPSTR(HA_STATE_TOPIC)] = this->getDeviceTopic(F("state"));
@@ -1122,7 +1129,7 @@ public:
return this->publish(this->getTopic(FPSTR(HA_ENTITY_SENSOR), F("pressure")).c_str(), doc); return this->publish(this->getTopic(FPSTR(HA_ENTITY_SENSOR), F("pressure")).c_str(), doc);
} }
bool publishSensorDhwFlowRate(bool enabledByDefault = true) { bool publishSensorDhwFlowRate(UnitSystem unit = UnitSystem::METRIC, bool enabledByDefault = true) {
JsonDocument doc; JsonDocument doc;
doc[FPSTR(HA_AVAILABILITY)][0][FPSTR(HA_TOPIC)] = this->getDeviceTopic(F("status")); doc[FPSTR(HA_AVAILABILITY)][0][FPSTR(HA_TOPIC)] = this->getDeviceTopic(F("status"));
doc[FPSTR(HA_AVAILABILITY)][1][FPSTR(HA_TOPIC)] = this->getDeviceTopic(F("state")); doc[FPSTR(HA_AVAILABILITY)][1][FPSTR(HA_TOPIC)] = this->getDeviceTopic(F("state"));
@@ -1134,7 +1141,14 @@ public:
doc[FPSTR(HA_ENTITY_CATEGORY)] = F("diagnostic"); doc[FPSTR(HA_ENTITY_CATEGORY)] = F("diagnostic");
doc[FPSTR(HA_DEVICE_CLASS)] = F("volume_flow_rate"); doc[FPSTR(HA_DEVICE_CLASS)] = F("volume_flow_rate");
doc[FPSTR(HA_STATE_CLASS)] = F("measurement"); doc[FPSTR(HA_STATE_CLASS)] = F("measurement");
doc[FPSTR(HA_UNIT_OF_MEASUREMENT)] = F("L/min");
if (unit == UnitSystem::METRIC) {
doc[FPSTR(HA_UNIT_OF_MEASUREMENT)] = F("L/min");
} else if (unit == UnitSystem::IMPERIAL) {
doc[FPSTR(HA_UNIT_OF_MEASUREMENT)] = F("gal/min");
}
doc[FPSTR(HA_NAME)] = F("DHW flow rate"); doc[FPSTR(HA_NAME)] = F("DHW flow rate");
doc[FPSTR(HA_ICON)] = F("mdi:water-pump"); doc[FPSTR(HA_ICON)] = F("mdi:water-pump");
doc[FPSTR(HA_STATE_TOPIC)] = this->getDeviceTopic(F("state")); doc[FPSTR(HA_STATE_TOPIC)] = this->getDeviceTopic(F("state"));

View File

@@ -364,7 +364,7 @@ protected:
// sensors // sensors
this->haHelper->publishSensorModulation(false); this->haHelper->publishSensorModulation(false);
this->haHelper->publishSensorPressure(false); this->haHelper->publishSensorPressure(settings.system.unitSystem, false);
this->haHelper->publishSensorFaultCode(); this->haHelper->publishSensorFaultCode();
this->haHelper->publishSensorRssi(false); this->haHelper->publishSensorRssi(false);
this->haHelper->publishSensorUptime(false); this->haHelper->publishSensorUptime(false);
@@ -416,7 +416,7 @@ protected:
this->haHelper->publishNumberDhwMaxTemp(settings.system.unitSystem, false); this->haHelper->publishNumberDhwMaxTemp(settings.system.unitSystem, false);
this->haHelper->publishBinSensorDhw(); this->haHelper->publishBinSensorDhw();
this->haHelper->publishSensorDhwTemp(settings.system.unitSystem); this->haHelper->publishSensorDhwTemp(settings.system.unitSystem);
this->haHelper->publishSensorDhwFlowRate(false); this->haHelper->publishSensorDhwFlowRate(settings.system.unitSystem, false);
} else { } else {
this->haHelper->deleteSwitchDhw(); this->haHelper->deleteSwitchDhw();

View File

@@ -253,12 +253,13 @@ protected:
fsSettings.update(); fsSettings.update();
Log.snoticeln(FPSTR(L_OT_HEATING), F("Updated max temp: %hhu"), settings.heating.maxTemp); Log.snoticeln(FPSTR(L_OT_HEATING), F("Updated max temp: %hhu"), settings.heating.maxTemp);
} }
} else {
vars.parameters.heatingMinTemp = convertTemp(DEFAULT_HEATING_MIN_TEMP, UnitSystem::METRIC, settings.system.unitSystem);
vars.parameters.heatingMaxTemp = convertTemp(DEFAULT_HEATING_MAX_TEMP, UnitSystem::METRIC, settings.system.unitSystem);
} else { Log.swarningln(FPSTR(L_OT_HEATING), F("Failed get min/max temp"));
vars.parameters.heatingMinTemp = convertTemp(DEFAULT_HEATING_MIN_TEMP, UnitSystem::METRIC, settings.system.unitSystem); }
vars.parameters.heatingMaxTemp = convertTemp(DEFAULT_HEATING_MAX_TEMP, UnitSystem::METRIC, settings.system.unitSystem);
Log.swarningln(FPSTR(L_OT_HEATING), F("Failed get min/max temp"));
} }
if (settings.heating.minTemp >= settings.heating.maxTemp) { if (settings.heating.minTemp >= settings.heating.maxTemp) {
@@ -785,12 +786,17 @@ protected:
if (!CustomOpenTherm::isValidResponse(response)) { if (!CustomOpenTherm::isValidResponse(response)) {
return false; return false;
} }
float value = CustomOpenTherm::getFloat(response); float value = CustomOpenTherm::getFloat(response);
if (value > 16 && this->dhwFlowRateMultiplier != 10) { if (this->dhwFlowRateMultiplier != 10 && value > convertVolume(16, UnitSystem::METRIC, settings.opentherm.unitSystem)) {
this->dhwFlowRateMultiplier = 10; this->dhwFlowRateMultiplier = 10;
} }
vars.sensors.dhwFlowRate = this->dhwFlowRateMultiplier == 1 ? value : value / this->dhwFlowRateMultiplier;
vars.sensors.dhwFlowRate = convertVolume(
value / this->dhwFlowRateMultiplier,
settings.opentherm.unitSystem,
settings.system.unitSystem
);
return true; return true;
} }
@@ -838,10 +844,15 @@ protected:
} }
float value = CustomOpenTherm::getFloat(response); float value = CustomOpenTherm::getFloat(response);
if (value > 5 && this->pressureMultiplier != 10) { if (this->pressureMultiplier != 10 && value > convertPressure(5, UnitSystem::METRIC, settings.opentherm.unitSystem)) {
this->pressureMultiplier = 10; this->pressureMultiplier = 10;
} }
vars.sensors.pressure = this->pressureMultiplier == 1 ? value : value / this->pressureMultiplier;
vars.sensors.pressure = convertPressure(
value / this->pressureMultiplier,
settings.opentherm.unitSystem,
settings.system.unitSystem
);
return true; return true;
} }

View File

@@ -1,5 +1,43 @@
#include <Arduino.h> #include <Arduino.h>
inline float liter2gallon(float value) {
return value / 4.546091879f;
}
inline float gallon2liter(float value) {
return value * 4.546091879f;
}
float convertVolume(float value, const UnitSystem unitFrom, const UnitSystem unitTo) {
if (unitFrom == UnitSystem::METRIC && unitTo == UnitSystem::IMPERIAL) {
value = liter2gallon(value);
} else if (unitFrom == UnitSystem::IMPERIAL && unitTo == UnitSystem::METRIC) {
value = gallon2liter(value);
}
return value;
}
inline float bar2psi(float value) {
return value * 14.5038f;
}
inline float psi2bar(float value) {
return value / 14.5038f;
}
float convertPressure(float value, const UnitSystem unitFrom, const UnitSystem unitTo) {
if (unitFrom == UnitSystem::METRIC && unitTo == UnitSystem::IMPERIAL) {
value = bar2psi(value);
} else if (unitFrom == UnitSystem::IMPERIAL && unitTo == UnitSystem::METRIC) {
value = psi2bar(value);
}
return value;
}
inline float c2f(float value) { inline float c2f(float value) {
return (9.0f / 5.0f) * value + 32.0f; return (9.0f / 5.0f) * value + 32.0f;
} }