mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 01:07:45 +01:00
Add 'set_total_pulses' action to 'pulse_counter' sensor. (#3640)
This commit is contained in:
parent
f33d829ce9
commit
04f4dd8a22
4 changed files with 56 additions and 1 deletions
24
esphome/components/pulse_counter/automation.h
Normal file
24
esphome/components/pulse_counter/automation.h
Normal file
|
@ -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<typename... Ts> class SetTotalPulsesAction : public Action<Ts...> {
|
||||
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
|
|
@ -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_);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue