mirror of
https://github.com/esphome/esphome.git
synced 2025-10-30 06:33:51 +00:00
Add action to set total pulses on pulse_meter (#1757)
This commit is contained in:
24
esphome/components/pulse_meter/automation.h
Normal file
24
esphome/components/pulse_meter/automation.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/pulse_meter/pulse_meter_sensor.h"
|
||||
|
||||
namespace esphome {
|
||||
|
||||
namespace pulse_meter {
|
||||
|
||||
template<typename... Ts> class SetTotalPulsesAction : public Action<Ts...> {
|
||||
public:
|
||||
SetTotalPulsesAction(PulseMeterSensor *pulse_meter) : pulse_meter_(pulse_meter) {}
|
||||
|
||||
TEMPLATABLE_VALUE(uint32_t, total_pulses)
|
||||
|
||||
void play(Ts... x) override { this->pulse_meter_->set_total_pulses(this->total_pulses_.value(x...)); }
|
||||
|
||||
protected:
|
||||
PulseMeterSensor *pulse_meter_;
|
||||
};
|
||||
|
||||
} // namespace pulse_meter
|
||||
} // namespace esphome
|
||||
@@ -48,6 +48,8 @@ void PulseMeterSensor::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
void PulseMeterSensor::set_total_pulses(uint32_t pulses) { this->total_pulses_ = pulses; }
|
||||
|
||||
void PulseMeterSensor::dump_config() {
|
||||
LOG_SENSOR("", "Pulse Meter", this);
|
||||
LOG_PIN(" Pin: ", this->pin_);
|
||||
|
||||
@@ -15,6 +15,8 @@ class PulseMeterSensor : public sensor::Sensor, public Component {
|
||||
void set_timeout_us(uint32_t timeout) { this->timeout_us_ = timeout; }
|
||||
void set_total_sensor(sensor::Sensor *sensor) { this->total_sensor_ = sensor; }
|
||||
|
||||
void set_total_pulses(uint32_t pulses);
|
||||
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
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_ID,
|
||||
@@ -9,6 +9,7 @@ from esphome.const import (
|
||||
CONF_NUMBER,
|
||||
CONF_TIMEOUT,
|
||||
CONF_TOTAL,
|
||||
CONF_VALUE,
|
||||
ICON_PULSE,
|
||||
UNIT_PULSES,
|
||||
UNIT_PULSES_PER_MINUTE,
|
||||
@@ -24,6 +25,8 @@ PulseMeterSensor = pulse_meter_ns.class_(
|
||||
"PulseMeterSensor", sensor.Sensor, cg.Component
|
||||
)
|
||||
|
||||
SetTotalPulsesAction = pulse_meter_ns.class_("SetTotalPulsesAction", automation.Action)
|
||||
|
||||
|
||||
def validate_internal_filter(value):
|
||||
return cv.positive_time_period_microseconds(value)
|
||||
@@ -73,3 +76,21 @@ def to_code(config):
|
||||
if CONF_TOTAL in config:
|
||||
sens = yield sensor.new_sensor(config[CONF_TOTAL])
|
||||
cg.add(var.set_total_sensor(sens))
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"pulse_meter.set_total_pulses",
|
||||
SetTotalPulsesAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(PulseMeterSensor),
|
||||
cv.Required(CONF_VALUE): cv.templatable(cv.uint32_t),
|
||||
}
|
||||
),
|
||||
)
|
||||
def set_total_action_to_code(config, action_id, template_arg, args):
|
||||
paren = yield cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
template_ = yield cg.templatable(config[CONF_VALUE], args, int)
|
||||
cg.add(var.set_total_pulses(template_))
|
||||
yield var
|
||||
|
||||
Reference in New Issue
Block a user