mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-01 02:28:28 +02:00
Cleanup dashboard JS (#491)
* Cleanup dashboard JS * Add vscode * Save start_mark/end_mark * Updates * Updates * Remove need for cv.nameable It's a bit hacky but removes so much bloat from integrations * Add enum helper * Document APIs, and Improvements * Fixes * Fixes * Update PULL_REQUEST_TEMPLATE.md * Updates * Updates * Updates
This commit is contained in:
@@ -1,19 +1,20 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.automation import ACTION_REGISTRY, maybe_simple_id
|
||||
from esphome import automation
|
||||
from esphome.automation import maybe_simple_id
|
||||
from esphome.components.output import FloatOutput
|
||||
from esphome.const import CONF_ID, CONF_IDLE_LEVEL, CONF_MAX_LEVEL, CONF_MIN_LEVEL, CONF_OUTPUT, \
|
||||
CONF_LEVEL
|
||||
|
||||
servo_ns = cg.esphome_ns.namespace('servo')
|
||||
Servo = servo_ns.class_('Servo', cg.Component)
|
||||
ServoWriteAction = servo_ns.class_('ServoWriteAction', cg.Action)
|
||||
ServoDetachAction = servo_ns.class_('ServoDetachAction', cg.Action)
|
||||
ServoWriteAction = servo_ns.class_('ServoWriteAction', automation.Action)
|
||||
ServoDetachAction = servo_ns.class_('ServoDetachAction', automation.Action)
|
||||
|
||||
MULTI_CONF = True
|
||||
CONFIG_SCHEMA = cv.Schema({
|
||||
cv.Required(CONF_ID): cv.declare_variable_id(Servo),
|
||||
cv.Required(CONF_OUTPUT): cv.use_variable_id(FloatOutput),
|
||||
cv.Required(CONF_ID): cv.declare_id(Servo),
|
||||
cv.Required(CONF_OUTPUT): cv.use_id(FloatOutput),
|
||||
cv.Optional(CONF_MIN_LEVEL, default='3%'): cv.percentage,
|
||||
cv.Optional(CONF_IDLE_LEVEL, default='7.5%'): cv.percentage,
|
||||
cv.Optional(CONF_MAX_LEVEL, default='12%'): cv.percentage,
|
||||
@@ -21,34 +22,31 @@ CONFIG_SCHEMA = cv.Schema({
|
||||
|
||||
|
||||
def to_code(config):
|
||||
out = yield cg.get_variable(config[CONF_OUTPUT])
|
||||
var = cg.new_Pvariable(config[CONF_ID], out)
|
||||
cg.register_component(var, config)
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield cg.register_component(var, config)
|
||||
|
||||
out = yield cg.get_variable(config[CONF_OUTPUT])
|
||||
cg.add(var.set_output(out))
|
||||
cg.add(var.set_min_level(config[CONF_MIN_LEVEL]))
|
||||
cg.add(var.set_idle_level(config[CONF_IDLE_LEVEL]))
|
||||
cg.add(var.set_max_level(config[CONF_MAX_LEVEL]))
|
||||
|
||||
|
||||
@ACTION_REGISTRY.register('servo.write', cv.Schema({
|
||||
cv.Required(CONF_ID): cv.use_variable_id(Servo),
|
||||
@automation.register_action('servo.write', ServoWriteAction, cv.Schema({
|
||||
cv.Required(CONF_ID): cv.use_id(Servo),
|
||||
cv.Required(CONF_LEVEL): cv.templatable(cv.possibly_negative_percentage),
|
||||
}))
|
||||
def servo_write_to_code(config, action_id, template_arg, args):
|
||||
var = yield cg.get_variable(config[CONF_ID])
|
||||
type = ServoWriteAction.template(template_arg)
|
||||
rhs = type.new(var)
|
||||
action = cg.Pvariable(action_id, rhs, type=type)
|
||||
paren = yield cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
template_ = yield cg.templatable(config[CONF_LEVEL], args, float)
|
||||
cg.add(action.set_value(template_))
|
||||
yield action
|
||||
cg.add(var.set_value(template_))
|
||||
yield var
|
||||
|
||||
|
||||
@ACTION_REGISTRY.register('servo.detach', maybe_simple_id({
|
||||
cv.Required(CONF_ID): cv.use_variable_id(Servo),
|
||||
@automation.register_action('servo.detach', ServoDetachAction, maybe_simple_id({
|
||||
cv.Required(CONF_ID): cv.use_id(Servo),
|
||||
}))
|
||||
def servo_detach_to_code(config, action_id, template_arg, args):
|
||||
var = yield cg.get_variable(config[CONF_ID])
|
||||
type = ServoDetachAction.template(template_arg)
|
||||
rhs = type.new(var)
|
||||
yield cg.Pvariable(action_id, rhs, type=type)
|
||||
paren = yield cg.get_variable(config[CONF_ID])
|
||||
yield cg.new_Pvariable(action_id, template_arg, paren)
|
||||
|
||||
@@ -13,7 +13,7 @@ extern uint32_t global_servo_id;
|
||||
|
||||
class Servo : public Component {
|
||||
public:
|
||||
Servo(output::FloatOutput *output) : output_(output) {}
|
||||
void set_output(output::FloatOutput *output) { output_ = output; }
|
||||
void write(float value) {
|
||||
value = clamp(value, -1.0f, 1.0f);
|
||||
|
||||
@@ -63,10 +63,7 @@ template<typename... Ts> class ServoWriteAction : public Action<Ts...> {
|
||||
public:
|
||||
ServoWriteAction(Servo *servo) : servo_(servo) {}
|
||||
TEMPLATABLE_VALUE(float, value)
|
||||
void play(Ts... x) override {
|
||||
this->servo_->write(this->value_.value(x...));
|
||||
this->play_next(x...);
|
||||
}
|
||||
void play(Ts... x) override { this->servo_->write(this->value_.value(x...)); }
|
||||
|
||||
protected:
|
||||
Servo *servo_;
|
||||
@@ -75,10 +72,7 @@ template<typename... Ts> class ServoWriteAction : public Action<Ts...> {
|
||||
template<typename... Ts> class ServoDetachAction : public Action<Ts...> {
|
||||
public:
|
||||
ServoDetachAction(Servo *servo) : servo_(servo) {}
|
||||
void play(Ts... x) override {
|
||||
this->servo_->detach();
|
||||
this->play_next(x...);
|
||||
}
|
||||
void play(Ts... x) override { this->servo_->detach(); }
|
||||
|
||||
protected:
|
||||
Servo *servo_;
|
||||
|
||||
Reference in New Issue
Block a user