[pulse_meter] Use register_apply_action for pulse_meter.set_total_pulses (#19533)

This commit is contained in:
J. Nick Koston
2026-09-23 16:52:00 -05:00
committed by GitHub
parent 4a7fe83a17
commit fa7ccc934e
3 changed files with 6 additions and 39 deletions
@@ -1,21 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/pulse_meter/pulse_meter_sensor.h"
namespace esphome::pulse_meter {
template<typename... Ts> class SetTotalPulsesAction final : public Action<Ts...> {
public:
SetTotalPulsesAction(PulseMeterSensor *pulse_meter) : pulse_meter_(pulse_meter) {}
TEMPLATABLE_VALUE(uint32_t, total_pulses)
void play(const Ts &...x) override { this->pulse_meter_->set_total_pulses(this->total_pulses_.value(x...)); }
protected:
PulseMeterSensor *pulse_meter_;
};
} // namespace esphome::pulse_meter
+3 -18
View File
@@ -19,8 +19,7 @@ from esphome.const import (
UNIT_PULSES,
UNIT_PULSES_PER_MINUTE,
)
from esphome.core import CORE, ID, TimePeriodMicroseconds
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.core import CORE, TimePeriodMicroseconds
from esphome.types import ConfigType
CODEOWNERS = ["@stevebaxter", "@cstaahl", "@TrentHouliston"]
@@ -38,8 +37,6 @@ FILTER_MODES = {
"PULSE": PulseMeterInternalFilterMode.FILTER_PULSE,
}
SetTotalPulsesAction = pulse_meter_ns.class_("SetTotalPulsesAction", automation.Action)
def validate_internal_filter(value: Any) -> TimePeriodMicroseconds:
return cv.positive_time_period_microseconds(value)
@@ -100,25 +97,13 @@ async def to_code(config: ConfigType) -> None:
cg.add(var.set_total_sensor(sens))
@automation.register_action(
automation.register_apply_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),
}
),
synchronous=True,
automation.ApplyField(CONF_VALUE, "set_total_pulses", cg.uint32),
)
async def set_total_action_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
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, cg.uint32)
cg.add(var.set_total_pulses(template_))
return var
+3
View File
@@ -9,5 +9,8 @@ sensor:
- pulse_meter.set_total_pulses:
id: pulse_meter_sensor
value: 12345
- pulse_meter.set_total_pulses:
id: pulse_meter_sensor
value: !lambda return (uint32_t) x;
total:
name: Pulse Meter Total