Pulse_counter measure total pulses (#1173)

* Draft Pulse_count_total

* Added check if Total sensor is present

* fix lint errors

* fix lint

* Update esphome/components/pulse_counter/sensor.py

Co-authored-by: Otto Winter <otto@otto-winter.com>

Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
Frank Bakker 2020-11-01 08:45:26 +01:00 committed by GitHub
parent 10e7abb579
commit e4636b99f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 2 deletions

View file

@ -158,6 +158,12 @@ void PulseCounterSensor::update() {
ESP_LOGD(TAG, "'%s': Retrieved counter: %0.2f pulses/min", this->get_name().c_str(), value); ESP_LOGD(TAG, "'%s': Retrieved counter: %0.2f pulses/min", this->get_name().c_str(), value);
this->publish_state(value); this->publish_state(value);
if (this->total_sensor_ != nullptr) {
current_total_ += raw;
ESP_LOGD(TAG, "'%s': Total : %i pulses", this->get_name().c_str(), current_total_);
this->total_sensor_->publish_state(current_total_);
}
} }
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32

View file

@ -54,6 +54,7 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent {
void set_rising_edge_mode(PulseCounterCountMode mode) { storage_.rising_edge_mode = mode; } void set_rising_edge_mode(PulseCounterCountMode mode) { storage_.rising_edge_mode = mode; }
void set_falling_edge_mode(PulseCounterCountMode mode) { storage_.falling_edge_mode = mode; } 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_filter_us(uint32_t filter) { storage_.filter_us = filter; }
void set_total_sensor(sensor::Sensor *total_sensor) { total_sensor_ = total_sensor; }
/// Unit of measurement is "pulses/min". /// Unit of measurement is "pulses/min".
void setup() override; void setup() override;
@ -64,6 +65,8 @@ class PulseCounterSensor : public sensor::Sensor, public PollingComponent {
protected: protected:
GPIOPin *pin_; GPIOPin *pin_;
PulseCounterStorage storage_; PulseCounterStorage storage_;
uint32_t current_total_ = 0;
sensor::Sensor *total_sensor_;
}; };
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32

View file

@ -3,8 +3,8 @@ import esphome.config_validation as cv
from esphome import pins from esphome import pins
from esphome.components import sensor from esphome.components import sensor
from esphome.const import CONF_COUNT_MODE, CONF_FALLING_EDGE, CONF_ID, CONF_INTERNAL_FILTER, \ from esphome.const import CONF_COUNT_MODE, CONF_FALLING_EDGE, CONF_ID, CONF_INTERNAL_FILTER, \
CONF_PIN, CONF_RISING_EDGE, CONF_NUMBER, \ CONF_PIN, CONF_RISING_EDGE, CONF_NUMBER, CONF_TOTAL, \
ICON_PULSE, UNIT_PULSES_PER_MINUTE ICON_PULSE, UNIT_PULSES_PER_MINUTE, UNIT_PULSES
from esphome.core import CORE from esphome.core import CORE
pulse_counter_ns = cg.esphome_ns.namespace('pulse_counter') pulse_counter_ns = cg.esphome_ns.namespace('pulse_counter')
@ -58,6 +58,8 @@ CONFIG_SCHEMA = sensor.sensor_schema(UNIT_PULSES_PER_MINUTE, ICON_PULSE, 2).exte
cv.Required(CONF_FALLING_EDGE): COUNT_MODE_SCHEMA, cv.Required(CONF_FALLING_EDGE): COUNT_MODE_SCHEMA,
}), validate_count_mode), }), validate_count_mode),
cv.Optional(CONF_INTERNAL_FILTER, default='13us'): validate_internal_filter, cv.Optional(CONF_INTERNAL_FILTER, default='13us'): validate_internal_filter,
cv.Optional(CONF_TOTAL): sensor.sensor_schema(UNIT_PULSES, ICON_PULSE, 0),
}).extend(cv.polling_component_schema('60s')) }).extend(cv.polling_component_schema('60s'))
@ -72,3 +74,7 @@ def to_code(config):
cg.add(var.set_rising_edge_mode(count[CONF_RISING_EDGE])) cg.add(var.set_rising_edge_mode(count[CONF_RISING_EDGE]))
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_filter_us(config[CONF_INTERNAL_FILTER])) cg.add(var.set_filter_us(config[CONF_INTERNAL_FILTER]))
if CONF_TOTAL in config:
sens = yield sensor.new_sensor(config[CONF_TOTAL])
cg.add(var.set_total_sensor(sens))

View file

@ -525,6 +525,7 @@ CONF_TO = 'to'
CONF_TOLERANCE = 'tolerance' CONF_TOLERANCE = 'tolerance'
CONF_TOPIC = 'topic' CONF_TOPIC = 'topic'
CONF_TOPIC_PREFIX = 'topic_prefix' CONF_TOPIC_PREFIX = 'topic_prefix'
CONF_TOTAL = "total"
CONF_TRANSITION_LENGTH = 'transition_length' CONF_TRANSITION_LENGTH = 'transition_length'
CONF_TRIGGER_ID = 'trigger_id' CONF_TRIGGER_ID = 'trigger_id'
CONF_TRIGGER_PIN = 'trigger_pin' CONF_TRIGGER_PIN = 'trigger_pin'
@ -641,6 +642,7 @@ UNIT_OHM = 'Ω'
UNIT_PARTS_PER_BILLION = 'ppb' UNIT_PARTS_PER_BILLION = 'ppb'
UNIT_PARTS_PER_MILLION = 'ppm' UNIT_PARTS_PER_MILLION = 'ppm'
UNIT_PERCENT = '%' UNIT_PERCENT = '%'
UNIT_PULSES = "pulses"
UNIT_PULSES_PER_MINUTE = 'pulses/min' UNIT_PULSES_PER_MINUTE = 'pulses/min'
UNIT_SECOND = 's' UNIT_SECOND = 's'
UNIT_STEPS = 'steps' UNIT_STEPS = 'steps'