Add action to set total pulses on pulse_meter (#1757)

This commit is contained in:
Jesse Hills 2021-05-14 20:06:31 +12:00 committed by GitHub
parent 00c144daeb
commit 4f6982fbc5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 5 deletions

View file

@ -0,0 +1,24 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/pulse_meter/pulse_meter_sensor.h"
namespace esphome {
namespace pulse_meter {
template<typename... Ts> class SetTotalPulsesAction : public Action<Ts...> {
public:
SetTotalPulsesAction(PulseMeterSensor *pulse_meter) : pulse_meter_(pulse_meter) {}
TEMPLATABLE_VALUE(uint32_t, total_pulses)
void play(Ts... x) override { this->pulse_meter_->set_total_pulses(this->total_pulses_.value(x...)); }
protected:
PulseMeterSensor *pulse_meter_;
};
} // namespace pulse_meter
} // namespace esphome

View file

@ -48,6 +48,8 @@ void PulseMeterSensor::loop() {
}
}
void PulseMeterSensor::set_total_pulses(uint32_t pulses) { this->total_pulses_ = pulses; }
void PulseMeterSensor::dump_config() {
LOG_SENSOR("", "Pulse Meter", this);
LOG_PIN(" Pin: ", this->pin_);

View file

@ -15,6 +15,8 @@ class PulseMeterSensor : public sensor::Sensor, public Component {
void set_timeout_us(uint32_t timeout) { this->timeout_us_ = timeout; }
void set_total_sensor(sensor::Sensor *sensor) { this->total_sensor_ = sensor; }
void set_total_pulses(uint32_t pulses);
void setup() override;
void loop() override;
float get_setup_priority() const override { return setup_priority::DATA; }

View file

@ -1,6 +1,6 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome import automation, pins
from esphome.components import sensor
from esphome.const import (
CONF_ID,
@ -9,6 +9,7 @@ from esphome.const import (
CONF_NUMBER,
CONF_TIMEOUT,
CONF_TOTAL,
CONF_VALUE,
ICON_PULSE,
UNIT_PULSES,
UNIT_PULSES_PER_MINUTE,
@ -24,6 +25,8 @@ PulseMeterSensor = pulse_meter_ns.class_(
"PulseMeterSensor", sensor.Sensor, cg.Component
)
SetTotalPulsesAction = pulse_meter_ns.class_("SetTotalPulsesAction", automation.Action)
def validate_internal_filter(value):
return cv.positive_time_period_microseconds(value)
@ -73,3 +76,21 @@ def to_code(config):
if CONF_TOTAL in config:
sens = yield sensor.new_sensor(config[CONF_TOTAL])
cg.add(var.set_total_sensor(sens))
@automation.register_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),
}
),
)
def set_total_action_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = yield cg.templatable(config[CONF_VALUE], args, int)
cg.add(var.set_total_pulses(template_))
yield var

View file

@ -264,7 +264,7 @@ mcp23s17:
sensor:
- platform: ble_client
ble_client_id: ble_foo
name: "Green iTag btn"
name: 'Green iTag btn'
service_uuid: 'ffe0'
characteristic_uuid: 'ffe1'
descriptor_uuid: 'ffe2'
@ -658,9 +658,14 @@ sensor:
update_interval: 15s
- platform: pulse_meter
name: 'Pulse Meter'
id: pulse_meter_sensor
pin: GPIO12
internal_filter: 100ms
timeout: 2 min
on_value:
- pulse_meter.set_total_pulses:
id: pulse_meter_sensor
value: 12345
total:
name: 'Pulse Meter Total'
- platform: rotary_encoder
@ -2038,7 +2043,6 @@ tca9548a:
id: multiplex0
channel: 0
pcf8574:
- id: 'pcf8574_hub'
address: 0x21