This commit is contained in:
Lucio Tarantino 2024-05-16 08:21:02 +02:00 committed by GitHub
commit d0579d90af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 0 deletions

View File

@ -180,6 +180,13 @@ void PulseCounterSensor::update() {
ESP_LOGD(TAG, "'%s': Total : %" PRIu32 " pulses", this->get_name().c_str(), current_total_);
this->total_sensor_->publish_state(current_total_);
}
if (this->binary_sensor_ != nullptr) {
bool level = this->storage_.pin->digital_read();
ESP_LOGD(TAG, "'%s': Sending state %s", this->get_name().c_str(), ONOFF(level));
this->binary_sensor_->publish_state(level);
}
this->last_time_ = now;
}

View File

@ -69,6 +69,7 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent {
void set_falling_edge_mode(PulseCounterCountMode mode) { storage_.falling_edge_mode = mode; }
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_binary_sensor(sensor::Sensor *binary_sensor) { binary_sensor_ = binary_sensor; }
void set_total_pulses(uint32_t pulses);
@ -84,6 +85,7 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent {
uint32_t last_time_{0};
uint32_t current_total_{0};
sensor::Sensor *total_sensor_{nullptr};
sensor::Sensor *binary_sensor_{nullptr};
};
} // namespace pulse_counter

View File

@ -3,6 +3,7 @@ import esphome.config_validation as cv
from esphome import automation, pins
from esphome.components import sensor
from esphome.const import (
CONF_BINARY_SENSOR,
CONF_COUNT_MODE,
CONF_FALLING_EDGE,
CONF_ID,
@ -115,6 +116,7 @@ CONFIG_SCHEMA = cv.All(
accuracy_decimals=0,
state_class=STATE_CLASS_TOTAL_INCREASING,
),
cv.Optional(CONF_BINARY_SENSOR): sensor.sensor_schema(),
},
)
.extend(cv.polling_component_schema("60s")),
@ -137,6 +139,10 @@ async def to_code(config):
sens = await sensor.new_sensor(config[CONF_TOTAL])
cg.add(var.set_total_sensor(sens))
if CONF_BINARY_SENSOR in config:
bsens = await sensor.new_sensor(config[CONF_BINARY_SENSOR])
cg.add(var.set_binary_sensor(bsens))
@automation.register_action(
"pulse_counter.set_total_pulses",