diff --git a/esphome/components/pulse_counter/automation.h b/esphome/components/pulse_counter/automation.h new file mode 100644 index 0000000000..d749540a95 --- /dev/null +++ b/esphome/components/pulse_counter/automation.h @@ -0,0 +1,24 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/core/automation.h" +#include "esphome/components/pulse_counter/pulse_counter_sensor.h" + +namespace esphome { + +namespace pulse_counter { + +template class SetTotalPulsesAction : public Action { + public: + SetTotalPulsesAction(PulseCounterSensor *pulse_counter) : pulse_counter_(pulse_counter) {} + + TEMPLATABLE_VALUE(uint32_t, total_pulses) + + void play(Ts... x) override { this->pulse_counter_->set_total_pulses(this->total_pulses_.value(x...)); } + + protected: + PulseCounterSensor *pulse_counter_; +}; + +} // namespace pulse_counter +} // namespace esphome diff --git a/esphome/components/pulse_counter/pulse_counter_sensor.cpp b/esphome/components/pulse_counter/pulse_counter_sensor.cpp index 5232ebc427..002f6dcac9 100644 --- a/esphome/components/pulse_counter/pulse_counter_sensor.cpp +++ b/esphome/components/pulse_counter/pulse_counter_sensor.cpp @@ -144,6 +144,11 @@ void PulseCounterSensor::setup() { } } +void PulseCounterSensor::set_total_pulses(uint32_t pulses) { + this->current_total_ = pulses; + this->total_sensor_->publish_state(pulses); +} + void PulseCounterSensor::dump_config() { LOG_SENSOR("", "Pulse Counter", this); LOG_PIN(" Pin: ", this->pin_); diff --git a/esphome/components/pulse_counter/pulse_counter_sensor.h b/esphome/components/pulse_counter/pulse_counter_sensor.h index 86c387d52a..f81d20a646 100644 --- a/esphome/components/pulse_counter/pulse_counter_sensor.h +++ b/esphome/components/pulse_counter/pulse_counter_sensor.h @@ -55,6 +55,8 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent { void set_filter_us(uint32_t filter) { storage_.filter_us = filter; } void set_total_sensor(sensor::Sensor *total_sensor) { total_sensor_ = total_sensor; } + void set_total_pulses(uint32_t pulses); + /// Unit of measurement is "pulses/min". void setup() override; void update() override; diff --git a/esphome/components/pulse_counter/sensor.py b/esphome/components/pulse_counter/sensor.py index 6dcb974a1f..88f53bdf77 100644 --- a/esphome/components/pulse_counter/sensor.py +++ b/esphome/components/pulse_counter/sensor.py @@ -1,15 +1,17 @@ import esphome.codegen as cg import esphome.config_validation as cv -from esphome import pins +from esphome import automation, pins from esphome.components import sensor from esphome.const import ( CONF_COUNT_MODE, CONF_FALLING_EDGE, + CONF_ID, CONF_INTERNAL_FILTER, CONF_PIN, CONF_RISING_EDGE, CONF_NUMBER, CONF_TOTAL, + CONF_VALUE, ICON_PULSE, STATE_CLASS_MEASUREMENT, STATE_CLASS_TOTAL_INCREASING, @@ -32,6 +34,10 @@ PulseCounterSensor = pulse_counter_ns.class_( "PulseCounterSensor", sensor.Sensor, cg.PollingComponent ) +SetTotalPulsesAction = pulse_counter_ns.class_( + "SetTotalPulsesAction", automation.Action +) + def validate_internal_filter(value): value = cv.positive_time_period_microseconds(value) @@ -116,3 +122,21 @@ async def to_code(config): if CONF_TOTAL in config: sens = await sensor.new_sensor(config[CONF_TOTAL]) cg.add(var.set_total_sensor(sens)) + + +@automation.register_action( + "pulse_counter.set_total_pulses", + SetTotalPulsesAction, + cv.Schema( + { + cv.Required(CONF_ID): cv.use_id(PulseCounterSensor), + cv.Required(CONF_VALUE): cv.templatable(cv.uint32_t), + } + ), +) +async def set_total_action_to_code(config, action_id, template_arg, args): + paren = await cg.get_variable(config[CONF_ID]) + var = cg.new_Pvariable(action_id, template_arg, paren) + template_ = await cg.templatable(config[CONF_VALUE], args, int) + cg.add(var.set_total_pulses(template_)) + return var