From d21229fa7377cda001dc191a6c782a086e693a7b Mon Sep 17 00:00:00 2001 From: Nicolas Bachschwell Date: Mon, 27 May 2024 14:43:10 +0200 Subject: [PATCH] Fixed a bug for chlorPump.yaml where the target would not properly load on start --- chlorPump.yaml | 15 +++--- external_components/chlorine_pump/__init__.py | 51 ++++++++++++------- .../chlorine_pump/pump_cycle.h | 27 ++++++++-- 3 files changed, 63 insertions(+), 30 deletions(-) diff --git a/chlorPump.yaml b/chlorPump.yaml index 60f413a..c5f257c 100644 --- a/chlorPump.yaml +++ b/chlorPump.yaml @@ -6,7 +6,10 @@ esphome: on_boot: priority: 600 then: - output.turn_on: wifi_status_led + - output.turn_on: wifi_status_led + - chlorine_pump.set_target: + target: !lambda return id(chlorine_target).state; + esp8266: board: nodemcuv2 @@ -175,19 +178,15 @@ sensor: - 47608 -> 0 - 590566 -> 100 unit_of_measurement: "%" - - platform: template - name: debug_last_raw_value - id: debug_last_raw_value - accuracy_decimals: 0 - unit_of_measurement: "points" chlorine_pump: pump: pump_switch sensor: chlorine_sensor id: chlorine_pump_component - target: 600 disable_clock: false - proportional_band: 400 + proportional_band: 200 + target: 650 +# get_target: !lambda return id(chlorine_target).state; on_pump_value: - lambda: |- if(pState != id(last_pump_state)){ diff --git a/external_components/chlorine_pump/__init__.py b/external_components/chlorine_pump/__init__.py index 6028e90..4c93cfb 100644 --- a/external_components/chlorine_pump/__init__.py +++ b/external_components/chlorine_pump/__init__.py @@ -21,6 +21,7 @@ CONF_PUMP_VALUE = "on_pump_value" CONF_CYCLE_START = "on_cycle_start" CONF_DISABLE_CLOCK="disable_clock" CONF_TARGET="target" +CONF_TARGET_LAMBDA="get_target" CONF_PUMP_TIME_DIVIDER = "pump_time_divider" def to_proportional_band(value): @@ -44,24 +45,33 @@ StopAction = chlorine_pump_ns.class_("ChlorineStop", automation.Action) SetTargetAction = chlorine_pump_ns.class_("ChlorineSetTarget", automation.Action) ManualDoseAction = chlorine_pump_ns.class_("ChlorineDose", automation.Action) -CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend({ - cv.GenerateID(): cv.declare_id(ChlorinePump), - cv.Required(CONF_PUMP_OUT): cv.use_id(output.BinaryOutput), - cv.Optional(CONF_SENSOR): cv.use_id(sensor.Sensor), - cv.Optional(CONF_PUMP_CYCLE_TIME, default=360): cv.int_range(min=30, max=1400), - cv.Optional(CONF_PUMP_PROPORTIONAL_BAND, default=200): to_proportional_band, - cv.Optional(CONF_DISABLE_CLOCK, default=False): cv.boolean, - cv.Optional(CONF_TARGET, 700): cv.int_range(300, 1400), - cv.Optional(CONF_PUMP_TIME_DIVIDER, 2): cv.float_range(1, 5), - cv.Optional(CONF_PUMP_VALUE): automation.validate_automation({ - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorSensorOutputTrigger), - } - ), - cv.Optional(CONF_CYCLE_START): automation.validate_automation({ - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorSensorCycleTrigger), - } - ), -}) +def validate_config(config): + if (config.get(CONF_TARGET_LAMBDA, None) is not None) is not (config.get(CONF_TARGET, None) not in config): + raise cv.Invalid("Either a fixed target or an get_target lambda must be set to get a target") + return config + +CONFIG_SCHEMA = cv.All( + cv.COMPONENT_SCHEMA.extend({ + cv.GenerateID(): cv.declare_id(ChlorinePump), + cv.Required(CONF_PUMP_OUT): cv.use_id(output.BinaryOutput), + cv.Optional(CONF_SENSOR): cv.use_id(sensor.Sensor), + cv.Optional(CONF_PUMP_CYCLE_TIME, default=360): cv.int_range(min=30, max=1400), + cv.Optional(CONF_PUMP_PROPORTIONAL_BAND, default=200): to_proportional_band, + cv.Optional(CONF_DISABLE_CLOCK, default=False): cv.boolean, + cv.Optional(CONF_TARGET): cv.int_range(300, 1400), + cv.Optional(CONF_PUMP_TIME_DIVIDER, 2): cv.float_range(1, 5), + cv.Optional(CONF_TARGET_LAMBDA, 2): cv.templatable(cv.float_), + cv.Optional(CONF_PUMP_VALUE): automation.validate_automation({ + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorSensorOutputTrigger), + } + ), + cv.Optional(CONF_CYCLE_START): automation.validate_automation({ + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorSensorCycleTrigger), + } + ), + }), + validate_config +) @@ -123,6 +133,11 @@ async def to_code(config): cg.add(var.set_prop_band(config[CONF_PUMP_PROPORTIONAL_BAND])) #cg.add(var.set_state(config[CONF_STATE])) + if CONF_TARGET not in config: + template_ = await cg.templatable(config[CONF_TARGET_LAMBDA], [], float) + cg.add(var.set_target_lambda(template_)) + else: + cg.add(var.set_target(config[CONF_TARGET])) if CONF_PUMP_VALUE in config: diff --git a/external_components/chlorine_pump/pump_cycle.h b/external_components/chlorine_pump/pump_cycle.h index 97f9baf..8a64a76 100644 --- a/external_components/chlorine_pump/pump_cycle.h +++ b/external_components/chlorine_pump/pump_cycle.h @@ -22,8 +22,9 @@ class ChlorinePump : public Component { CallbackManager callback_cycle_; int tOn = 0; int tOff = 0; - float target_ = 700.0f; + float target_ = -1; float tOn_divider_; + std::function target_lambda_ = nullptr; public: @@ -62,6 +63,11 @@ class ChlorinePump : public Component { }); } } + + void set_target_lambda(std::function callback){ + target_lambda_ = std::move(callback); + } + void prime() { tOn = 30; tOff = 2; @@ -79,6 +85,7 @@ class ChlorinePump : public Component { bool get_state(){ return state; } + void setMillisPrecies(unsigned long waitPeriod){ @@ -93,10 +100,18 @@ class ChlorinePump : public Component { } int calculatePumpTimeSeconds(float last_mesurement){ - - if(last_mesurement >= target_) return 0; - float currentVal = target_ - last_mesurement; + float used_target = -1; + if(target_ != -1){ + used_target = target_; + } + else{ + used_target = target_lambda_(); + } + + if(last_mesurement >= used_target) return 0; + + float currentVal = used_target - last_mesurement; float proportionalValue = (currentVal / (float) prop_band); float timeInSeconds = proportionalValue * (float) cycle_time; @@ -154,6 +169,10 @@ class ChlorinePump : public Component { setMillisPrecies(1000); } } + + void dump_config() override { + + } }; class ChlorSensorOutputTrigger : public Trigger {