Slight bug fixes for ezo_orp_i2c component

Added: A new time_modifier for chlorine_pump,
Used to set maximums and stuff for chlorine pump pump
This commit is contained in:
2024-06-04 08:42:37 +02:00
parent f0be663618
commit 30db0802a0
4 changed files with 87 additions and 3 deletions

View File

@@ -2,6 +2,7 @@
#include "esphome/core/automation.h"
#include "esphome/components/output/binary_output.h"
#include "Arduino.h"
static const char *const TAG = "chlorine_pump";
@@ -26,13 +27,18 @@ class ChlorinePump : public Component {
float tOn_divider_;
std::function<float()> target_lambda_ = nullptr;
int min_on_time_ = 0;
int max_on_time_ = -1;
float on_time_multiplier_ = 1;
int on_time_offset_ = 0;
int ignore_max_x_below_target_ = -1;
public:
ChlorinePump(){
last_action = 0;
}
void disable_clock(bool disable_clock){
this->disable_clock_ = disable_clock;
}
@@ -55,6 +61,23 @@ class ChlorinePump : public Component {
this->target_ = target;
}
// Cycle Time Modifiers
void set_min_on_time(int min_on_time){
this->min_on_time_ = min_on_time;
}
void set_max_on_time(int max_on_time){
this->max_on_time_ = max_on_time;
}
void set_time_multiplier(float time_multi){
this->on_time_multiplier_ = time_multi;
}
void set_time_offset(int offset){
this->on_time_offset_ = offset;
}
void set_ignore_max_x_below_target(int set_below){
this->ignore_max_x_below_target_ = set_below;
}
void setup() override {
last_action = millis();
if(disable_clock_ && sensor_ != nullptr){
@@ -120,7 +143,19 @@ class ChlorinePump : public Component {
return (int) timeInSeconds;
}
int calculate_changed_on_time(int tOn, float last_mesurement){
if(ignore_max_x_below_target_ != -1 && (float)(target_ - ignore_max_x_below_target_) >= last_mesurement){
return cycle_time;
}
tOn = (int)((float) tOn * on_time_multiplier_);
tOn += on_time_offset_;
if(tOn < min_on_time_) tOn = min_on_time_;
if(max_on_time_ != -1 && tOn > max_on_time_) tOn = max_on_time_;
return tOn;
}
void add_on_pump_callback(std::function<void(bool, int)> &&callback){
this->callback_pump_.add(std::move(callback));
@@ -134,7 +169,10 @@ class ChlorinePump : public Component {
if(tOn == 0 && tOff == 0 && state){
int seconds = calculatePumpTimeSeconds(last_mesurement);
tOn = seconds;
if(seconds != 0)
tOn = calculate_changed_on_time(seconds, last_mesurement);
else
tOn = 0;
tOff = cycle_time - tOn;
if(seconds == 0){