From 7bbb9b820e12fe6ceedf7ceedc4bd0bdc26fa59e Mon Sep 17 00:00:00 2001 From: dianlight Date: Wed, 3 Jan 2024 12:23:10 +0100 Subject: [PATCH] PulseCounter binary_sensor --- esphome/components/pulse_counter/pulse_counter_sensor.cpp | 7 +++++++ esphome/components/pulse_counter/pulse_counter_sensor.h | 2 ++ 2 files changed, 9 insertions(+) diff --git a/esphome/components/pulse_counter/pulse_counter_sensor.cpp b/esphome/components/pulse_counter/pulse_counter_sensor.cpp index 281a61a66a..d127f63b6a 100644 --- a/esphome/components/pulse_counter/pulse_counter_sensor.cpp +++ b/esphome/components/pulse_counter/pulse_counter_sensor.cpp @@ -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; } diff --git a/esphome/components/pulse_counter/pulse_counter_sensor.h b/esphome/components/pulse_counter/pulse_counter_sensor.h index ef9f73f95c..c7f97f8496 100644 --- a/esphome/components/pulse_counter/pulse_counter_sensor.h +++ b/esphome/components/pulse_counter/pulse_counter_sensor.h @@ -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