mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 21:44:55 +01:00
Add rotary_encoder.set_value action (#747)
* Add rotary_encoder.set_value action Fixes https://github.com/esphome/feature-requests/issues/389 * Fix
This commit is contained in:
parent
95c883ae9b
commit
996c50e8f2
3 changed files with 42 additions and 2 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/esphal.h"
|
#include "esphome/core/esphal.h"
|
||||||
|
#include "esphome/core/automation.h"
|
||||||
#include "esphome/components/sensor/sensor.h"
|
#include "esphome/components/sensor/sensor.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
@ -43,6 +44,12 @@ class RotaryEncoderSensor : public sensor::Sensor, public Component {
|
||||||
*/
|
*/
|
||||||
void set_resolution(RotaryEncoderResolution mode);
|
void set_resolution(RotaryEncoderResolution mode);
|
||||||
|
|
||||||
|
/// Manually set the value of the counter.
|
||||||
|
void set_value(int value) {
|
||||||
|
this->store_.counter = value;
|
||||||
|
this->loop();
|
||||||
|
}
|
||||||
|
|
||||||
void set_reset_pin(GPIOPin *pin_i) { this->pin_i_ = pin_i; }
|
void set_reset_pin(GPIOPin *pin_i) { this->pin_i_ = pin_i; }
|
||||||
void set_min_value(int32_t min_value);
|
void set_min_value(int32_t min_value);
|
||||||
void set_max_value(int32_t max_value);
|
void set_max_value(int32_t max_value);
|
||||||
|
@ -63,5 +70,15 @@ class RotaryEncoderSensor : public sensor::Sensor, public Component {
|
||||||
RotaryEncoderSensorStore store_{};
|
RotaryEncoderSensorStore store_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename... Ts> class RotaryEncoderSetValueAction : public Action<Ts...> {
|
||||||
|
public:
|
||||||
|
RotaryEncoderSetValueAction(RotaryEncoderSensor *encoder) : encoder_(encoder) {}
|
||||||
|
TEMPLATABLE_VALUE(int, value)
|
||||||
|
void play(Ts... x) override { this->encoder_->set_value(this->value_.value(x...)); }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
RotaryEncoderSensor *encoder_;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace rotary_encoder
|
} // namespace rotary_encoder
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome import pins
|
from esphome import pins, automation
|
||||||
from esphome.components import sensor
|
from esphome.components import sensor
|
||||||
from esphome.const import CONF_ID, CONF_RESOLUTION, CONF_MIN_VALUE, CONF_MAX_VALUE, UNIT_STEPS, \
|
from esphome.const import CONF_ID, CONF_RESOLUTION, CONF_MIN_VALUE, CONF_MAX_VALUE, UNIT_STEPS, \
|
||||||
ICON_ROTATE_RIGHT
|
ICON_ROTATE_RIGHT, CONF_VALUE
|
||||||
|
|
||||||
rotary_encoder_ns = cg.esphome_ns.namespace('rotary_encoder')
|
rotary_encoder_ns = cg.esphome_ns.namespace('rotary_encoder')
|
||||||
RotaryEncoderResolution = rotary_encoder_ns.enum('RotaryEncoderResolution')
|
RotaryEncoderResolution = rotary_encoder_ns.enum('RotaryEncoderResolution')
|
||||||
|
@ -18,6 +18,8 @@ CONF_PIN_B = 'pin_b'
|
||||||
CONF_PIN_RESET = 'pin_reset'
|
CONF_PIN_RESET = 'pin_reset'
|
||||||
|
|
||||||
RotaryEncoderSensor = rotary_encoder_ns.class_('RotaryEncoderSensor', sensor.Sensor, cg.Component)
|
RotaryEncoderSensor = rotary_encoder_ns.class_('RotaryEncoderSensor', sensor.Sensor, cg.Component)
|
||||||
|
RotaryEncoderSetValueAction = rotary_encoder_ns.class_('RotaryEncoderSetValueAction',
|
||||||
|
automation.Action)
|
||||||
|
|
||||||
|
|
||||||
def validate_min_max_value(config):
|
def validate_min_max_value(config):
|
||||||
|
@ -60,3 +62,16 @@ def to_code(config):
|
||||||
cg.add(var.set_min_value(config[CONF_MIN_VALUE]))
|
cg.add(var.set_min_value(config[CONF_MIN_VALUE]))
|
||||||
if CONF_MAX_VALUE in config:
|
if CONF_MAX_VALUE in config:
|
||||||
cg.add(var.set_max_value(config[CONF_MAX_VALUE]))
|
cg.add(var.set_max_value(config[CONF_MAX_VALUE]))
|
||||||
|
|
||||||
|
|
||||||
|
@automation.register_action('sensor.rotary_encoder.set_value', RotaryEncoderSetValueAction,
|
||||||
|
cv.Schema({
|
||||||
|
cv.Required(CONF_ID): cv.use_id(sensor.Sensor),
|
||||||
|
cv.Required(CONF_VALUE): cv.templatable(cv.int_),
|
||||||
|
}))
|
||||||
|
def sensor_template_publish_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_value(template_))
|
||||||
|
yield var
|
||||||
|
|
|
@ -491,6 +491,7 @@ sensor:
|
||||||
update_interval: 15s
|
update_interval: 15s
|
||||||
- platform: rotary_encoder
|
- platform: rotary_encoder
|
||||||
name: "Rotary Encoder"
|
name: "Rotary Encoder"
|
||||||
|
id: rotary_encoder1
|
||||||
pin_a: GPIO23
|
pin_a: GPIO23
|
||||||
pin_b: GPIO24
|
pin_b: GPIO24
|
||||||
pin_reset: GPIO25
|
pin_reset: GPIO25
|
||||||
|
@ -501,6 +502,13 @@ sensor:
|
||||||
resolution: 4
|
resolution: 4
|
||||||
min_value: -10
|
min_value: -10
|
||||||
max_value: 30
|
max_value: 30
|
||||||
|
on_value:
|
||||||
|
- sensor.rotary_encoder.set_value:
|
||||||
|
id: rotary_encoder1
|
||||||
|
value: 10
|
||||||
|
- sensor.rotary_encoder.set_value:
|
||||||
|
id: rotary_encoder1
|
||||||
|
value: !lambda 'return -1;'
|
||||||
- platform: pulse_width
|
- platform: pulse_width
|
||||||
name: Pulse Width
|
name: Pulse Width
|
||||||
pin: GPIO12
|
pin: GPIO12
|
||||||
|
|
Loading…
Reference in a new issue