pulse_counter_ulp: Remove total sensor automation

This automation doesn't preserve state over deep sleep, making it unhelpful for
the ULP pulse counter.
This commit is contained in:
brisk 2024-09-29 20:24:02 +09:30
parent 67b746693f
commit 40b18666c9
4 changed files with 5 additions and 82 deletions

View file

@ -1,24 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/pulse_counter_ulp/pulse_counter_ulp_sensor.h"
namespace esphome {
namespace pulse_counter_ulp {
template<typename... Ts> class SetTotalPulsesAction : public Action<Ts...> {
public:
SetTotalPulsesAction(PulseCounterUlpSensor *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:
PulseCounterUlpSensor *pulse_counter_;
};
} // namespace pulse_counter_ulp
} // namespace esphome

View file

@ -121,11 +121,6 @@ void PulseCounterUlpSensor::setup() {
} }
} }
void PulseCounterUlpSensor::set_total_pulses(uint32_t pulses) {
this->current_total_ = pulses;
this->total_sensor_->publish_state(pulses);
}
void PulseCounterUlpSensor::dump_config() { void PulseCounterUlpSensor::dump_config() {
LOG_SENSOR("", "Pulse Counter", this); LOG_SENSOR("", "Pulse Counter", this);
LOG_PIN(" Pin: ", this->config_.pin_); LOG_PIN(" Pin: ", this->config_.pin_);
@ -151,11 +146,6 @@ void PulseCounterUlpSensor::update() {
this->publish_state(value); this->publish_state(value);
} }
if (this->total_sensor_ != nullptr) {
this->current_total_ += raw.edge_count;
ESP_LOGD(TAG, "'%s': Total : %" PRIu32 " pulses", this->get_name().c_str(), current_total_);
this->total_sensor_->publish_state(this->current_total_);
}
this->last_time_ = now; this->last_time_ = now;
} }

View file

@ -48,9 +48,6 @@ class PulseCounterUlpSensor : public sensor::Sensor, public PollingComponent {
void set_falling_edge_mode(CountMode mode) { this->config_.falling_edge_mode_ = mode; } void set_falling_edge_mode(CountMode mode) { this->config_.falling_edge_mode_ = mode; }
void set_sleep_duration(uint32_t duration_us) { this->config_.sleep_duration_ = duration_us * microseconds{1}; } void set_sleep_duration(uint32_t duration_us) { this->config_.sleep_duration_ = duration_us * microseconds{1}; }
void set_debounce(uint16_t debounce) { this->config_.debounce_ = debounce; } void set_debounce(uint16_t debounce) { this->config_.debounce_ = debounce; }
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". /// Unit of measurement is "pulses/min".
void setup() override; void setup() override;
@ -60,11 +57,8 @@ class PulseCounterUlpSensor : public sensor::Sensor, public PollingComponent {
protected: protected:
UlpProgram::Config config_{}; UlpProgram::Config config_{};
sensor::Sensor *total_sensor_{nullptr};
std::unique_ptr<UlpProgram> storage_{}; std::unique_ptr<UlpProgram> storage_{};
clock::time_point last_time_{}; clock::time_point last_time_{};
uint32_t current_total_{0};
}; };
} // namespace pulse_counter_ulp } // namespace pulse_counter_ulp

View file

@ -1,25 +1,20 @@
import os import os
from esphome import pins
import esphome.codegen as cg import esphome.codegen as cg
from esphome.components import esp32, sensor
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import automation, pins
from esphome.components import sensor
from esphome.components import esp32
from esphome.const import ( from esphome.const import (
CONF_COUNT_MODE, CONF_COUNT_MODE,
CONF_DEBOUNCE,
CONF_FALLING_EDGE, CONF_FALLING_EDGE,
CONF_ID, CONF_NUMBER,
CONF_PIN, CONF_PIN,
CONF_RISING_EDGE, CONF_RISING_EDGE,
CONF_NUMBER,
CONF_SLEEP_DURATION, CONF_SLEEP_DURATION,
CONF_DEBOUNCE,
CONF_TOTAL,
CONF_VALUE,
ICON_PULSE, ICON_PULSE,
STATE_CLASS_MEASUREMENT, STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
UNIT_PULSES_PER_MINUTE, UNIT_PULSES_PER_MINUTE,
UNIT_PULSES,
) )
from esphome.core import CORE from esphome.core import CORE
@ -37,10 +32,6 @@ PulseCounterUlpSensor = pulse_counter_ulp_ns.class_(
"PulseCounterUlpSensor", sensor.Sensor, cg.PollingComponent "PulseCounterUlpSensor", sensor.Sensor, cg.PollingComponent
) )
SetTotalPulsesAction = pulse_counter_ulp_ns.class_(
"SetTotalPulsesAction", automation.Action
)
def validate_pulse_counter_pin(value): def validate_pulse_counter_pin(value):
value = pins.internal_gpio_input_pin_schema(value) value = pins.internal_gpio_input_pin_schema(value)
@ -92,12 +83,6 @@ CONFIG_SCHEMA = cv.All(
CONF_SLEEP_DURATION, default="20000us" CONF_SLEEP_DURATION, default="20000us"
): cv.positive_time_period_microseconds, ): cv.positive_time_period_microseconds,
cv.Optional(CONF_DEBOUNCE, default=3): cv.positive_int, cv.Optional(CONF_DEBOUNCE, default=3): cv.positive_int,
cv.Optional(CONF_TOTAL): sensor.sensor_schema(
unit_of_measurement=UNIT_PULSES,
icon=ICON_PULSE,
accuracy_decimals=0,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
}, },
) )
.extend(cv.polling_component_schema("60s")), .extend(cv.polling_component_schema("60s")),
@ -127,25 +112,3 @@ async def to_code(config):
cg.add(var.set_falling_edge_mode(count[CONF_FALLING_EDGE])) cg.add(var.set_falling_edge_mode(count[CONF_FALLING_EDGE]))
cg.add(var.set_sleep_duration(config[CONF_SLEEP_DURATION])) cg.add(var.set_sleep_duration(config[CONF_SLEEP_DURATION]))
cg.add(var.set_debounce(config[CONF_DEBOUNCE])) cg.add(var.set_debounce(config[CONF_DEBOUNCE]))
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_ulp.set_total_pulses",
SetTotalPulsesAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(PulseCounterUlpSensor),
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