mirror of
https://github.com/esphome/esphome.git
synced 2025-09-19 19:52:20 +01:00
Add valve component (#6447)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
118
esphome/components/template/valve/__init__.py
Normal file
118
esphome/components/template/valve/__init__.py
Normal file
@@ -0,0 +1,118 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import automation
|
||||
from esphome.components import valve
|
||||
from esphome.const import (
|
||||
CONF_ASSUMED_STATE,
|
||||
CONF_CLOSE_ACTION,
|
||||
CONF_CURRENT_OPERATION,
|
||||
CONF_ID,
|
||||
CONF_LAMBDA,
|
||||
CONF_OPEN_ACTION,
|
||||
CONF_OPTIMISTIC,
|
||||
CONF_POSITION,
|
||||
CONF_POSITION_ACTION,
|
||||
CONF_RESTORE_MODE,
|
||||
CONF_STATE,
|
||||
CONF_STOP_ACTION,
|
||||
)
|
||||
from .. import template_ns
|
||||
|
||||
TemplateValve = template_ns.class_("TemplateValve", valve.Valve, cg.Component)
|
||||
|
||||
TemplateValveRestoreMode = template_ns.enum("TemplateValveRestoreMode")
|
||||
RESTORE_MODES = {
|
||||
"NO_RESTORE": TemplateValveRestoreMode.VALVE_NO_RESTORE,
|
||||
"RESTORE": TemplateValveRestoreMode.VALVE_RESTORE,
|
||||
"RESTORE_AND_CALL": TemplateValveRestoreMode.VALVE_RESTORE_AND_CALL,
|
||||
}
|
||||
|
||||
CONF_HAS_POSITION = "has_position"
|
||||
CONF_TOGGLE_ACTION = "toggle_action"
|
||||
|
||||
CONFIG_SCHEMA = valve.VALVE_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(TemplateValve),
|
||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
||||
cv.Optional(CONF_ASSUMED_STATE, default=False): cv.boolean,
|
||||
cv.Optional(CONF_HAS_POSITION, default=False): cv.boolean,
|
||||
cv.Optional(CONF_OPEN_ACTION): automation.validate_automation(single=True),
|
||||
cv.Optional(CONF_CLOSE_ACTION): automation.validate_automation(single=True),
|
||||
cv.Optional(CONF_STOP_ACTION): automation.validate_automation(single=True),
|
||||
cv.Optional(CONF_TOGGLE_ACTION): automation.validate_automation(single=True),
|
||||
cv.Optional(CONF_POSITION_ACTION): automation.validate_automation(single=True),
|
||||
cv.Optional(CONF_RESTORE_MODE, default="NO_RESTORE"): cv.enum(
|
||||
RESTORE_MODES, upper=True
|
||||
),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await valve.new_valve(config)
|
||||
await cg.register_component(var, config)
|
||||
if lambda_config := config.get(CONF_LAMBDA):
|
||||
template_ = await cg.process_lambda(
|
||||
lambda_config, [], return_type=cg.optional.template(float)
|
||||
)
|
||||
cg.add(var.set_state_lambda(template_))
|
||||
if open_action_config := config.get(CONF_OPEN_ACTION):
|
||||
await automation.build_automation(
|
||||
var.get_open_trigger(), [], open_action_config
|
||||
)
|
||||
if close_action_config := config.get(CONF_CLOSE_ACTION):
|
||||
await automation.build_automation(
|
||||
var.get_close_trigger(), [], close_action_config
|
||||
)
|
||||
if stop_action_config := config.get(CONF_STOP_ACTION):
|
||||
await automation.build_automation(
|
||||
var.get_stop_trigger(), [], stop_action_config
|
||||
)
|
||||
cg.add(var.set_has_stop(True))
|
||||
if toggle_action_config := config.get(CONF_TOGGLE_ACTION):
|
||||
await automation.build_automation(
|
||||
var.get_toggle_trigger(), [], toggle_action_config
|
||||
)
|
||||
cg.add(var.set_has_toggle(True))
|
||||
if position_action_config := config.get(CONF_POSITION_ACTION):
|
||||
await automation.build_automation(
|
||||
var.get_position_trigger(), [(float, "pos")], position_action_config
|
||||
)
|
||||
cg.add(var.set_has_position(True))
|
||||
else:
|
||||
cg.add(var.set_has_position(config[CONF_HAS_POSITION]))
|
||||
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
||||
cg.add(var.set_assumed_state(config[CONF_ASSUMED_STATE]))
|
||||
cg.add(var.set_restore_mode(config[CONF_RESTORE_MODE]))
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"valve.template.publish",
|
||||
valve.ValvePublishAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(valve.Valve),
|
||||
cv.Exclusive(CONF_STATE, "pos"): cv.templatable(valve.validate_valve_state),
|
||||
cv.Exclusive(CONF_POSITION, "pos"): cv.templatable(cv.percentage),
|
||||
cv.Optional(CONF_CURRENT_OPERATION): cv.templatable(
|
||||
valve.validate_valve_operation
|
||||
),
|
||||
}
|
||||
),
|
||||
)
|
||||
async def valve_template_publish_to_code(config, action_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
if state_config := config.get(CONF_STATE):
|
||||
template_ = await cg.templatable(state_config, args, float)
|
||||
cg.add(var.set_position(template_))
|
||||
if (position_config := config.get(CONF_POSITION)) is not None:
|
||||
template_ = await cg.templatable(position_config, args, float)
|
||||
cg.add(var.set_position(template_))
|
||||
if current_operation_config := config.get(CONF_CURRENT_OPERATION):
|
||||
template_ = await cg.templatable(
|
||||
current_operation_config, args, valve.ValveOperation
|
||||
)
|
||||
cg.add(var.set_current_operation(template_))
|
||||
return var
|
131
esphome/components/template/valve/template_valve.cpp
Normal file
131
esphome/components/template/valve/template_valve.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
#include "template_valve.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
using namespace esphome::valve;
|
||||
|
||||
static const char *const TAG = "template.valve";
|
||||
|
||||
TemplateValve::TemplateValve()
|
||||
: open_trigger_(new Trigger<>()),
|
||||
close_trigger_(new Trigger<>),
|
||||
stop_trigger_(new Trigger<>()),
|
||||
toggle_trigger_(new Trigger<>()),
|
||||
position_trigger_(new Trigger<float>()) {}
|
||||
|
||||
void TemplateValve::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up template valve '%s'...", this->name_.c_str());
|
||||
switch (this->restore_mode_) {
|
||||
case VALVE_NO_RESTORE:
|
||||
break;
|
||||
case VALVE_RESTORE: {
|
||||
auto restore = this->restore_state_();
|
||||
if (restore.has_value())
|
||||
restore->apply(this);
|
||||
break;
|
||||
}
|
||||
case VALVE_RESTORE_AND_CALL: {
|
||||
auto restore = this->restore_state_();
|
||||
if (restore.has_value()) {
|
||||
restore->to_call(this).perform();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TemplateValve::loop() {
|
||||
bool changed = false;
|
||||
|
||||
if (this->state_f_.has_value()) {
|
||||
auto s = (*this->state_f_)();
|
||||
if (s.has_value()) {
|
||||
auto pos = clamp(*s, 0.0f, 1.0f);
|
||||
if (pos != this->position) {
|
||||
this->position = pos;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (changed)
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void TemplateValve::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||
void TemplateValve::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
|
||||
void TemplateValve::set_state_lambda(std::function<optional<float>()> &&f) { this->state_f_ = f; }
|
||||
float TemplateValve::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
|
||||
Trigger<> *TemplateValve::get_open_trigger() const { return this->open_trigger_; }
|
||||
Trigger<> *TemplateValve::get_close_trigger() const { return this->close_trigger_; }
|
||||
Trigger<> *TemplateValve::get_stop_trigger() const { return this->stop_trigger_; }
|
||||
Trigger<> *TemplateValve::get_toggle_trigger() const { return this->toggle_trigger_; }
|
||||
|
||||
void TemplateValve::dump_config() {
|
||||
LOG_VALVE("", "Template Valve", this);
|
||||
ESP_LOGCONFIG(TAG, " Has position: %s", YESNO(this->has_position_));
|
||||
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
|
||||
}
|
||||
|
||||
void TemplateValve::control(const ValveCall &call) {
|
||||
if (call.get_stop()) {
|
||||
this->stop_prev_trigger_();
|
||||
this->stop_trigger_->trigger();
|
||||
this->prev_command_trigger_ = this->stop_trigger_;
|
||||
this->publish_state();
|
||||
}
|
||||
if (call.get_toggle().has_value()) {
|
||||
this->stop_prev_trigger_();
|
||||
this->toggle_trigger_->trigger();
|
||||
this->prev_command_trigger_ = this->toggle_trigger_;
|
||||
this->publish_state();
|
||||
}
|
||||
if (call.get_position().has_value()) {
|
||||
auto pos = *call.get_position();
|
||||
this->stop_prev_trigger_();
|
||||
|
||||
if (pos == VALVE_OPEN) {
|
||||
this->open_trigger_->trigger();
|
||||
this->prev_command_trigger_ = this->open_trigger_;
|
||||
} else if (pos == VALVE_CLOSED) {
|
||||
this->close_trigger_->trigger();
|
||||
this->prev_command_trigger_ = this->close_trigger_;
|
||||
} else {
|
||||
this->position_trigger_->trigger(pos);
|
||||
}
|
||||
|
||||
if (this->optimistic_) {
|
||||
this->position = pos;
|
||||
}
|
||||
}
|
||||
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
ValveTraits TemplateValve::get_traits() {
|
||||
auto traits = ValveTraits();
|
||||
traits.set_is_assumed_state(this->assumed_state_);
|
||||
traits.set_supports_stop(this->has_stop_);
|
||||
traits.set_supports_toggle(this->has_toggle_);
|
||||
traits.set_supports_position(this->has_position_);
|
||||
return traits;
|
||||
}
|
||||
|
||||
Trigger<float> *TemplateValve::get_position_trigger() const { return this->position_trigger_; }
|
||||
|
||||
void TemplateValve::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
|
||||
void TemplateValve::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
|
||||
void TemplateValve::set_has_position(bool has_position) { this->has_position_ = has_position; }
|
||||
|
||||
void TemplateValve::stop_prev_trigger_() {
|
||||
if (this->prev_command_trigger_ != nullptr) {
|
||||
this->prev_command_trigger_->stop_action();
|
||||
this->prev_command_trigger_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
60
esphome/components/template/valve/template_valve.h
Normal file
60
esphome/components/template/valve/template_valve.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/valve/valve.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace template_ {
|
||||
|
||||
enum TemplateValveRestoreMode {
|
||||
VALVE_NO_RESTORE,
|
||||
VALVE_RESTORE,
|
||||
VALVE_RESTORE_AND_CALL,
|
||||
};
|
||||
|
||||
class TemplateValve : public valve::Valve, public Component {
|
||||
public:
|
||||
TemplateValve();
|
||||
|
||||
void set_state_lambda(std::function<optional<float>()> &&f);
|
||||
Trigger<> *get_open_trigger() const;
|
||||
Trigger<> *get_close_trigger() const;
|
||||
Trigger<> *get_stop_trigger() const;
|
||||
Trigger<> *get_toggle_trigger() const;
|
||||
Trigger<float> *get_position_trigger() const;
|
||||
void set_optimistic(bool optimistic);
|
||||
void set_assumed_state(bool assumed_state);
|
||||
void set_has_stop(bool has_stop);
|
||||
void set_has_position(bool has_position);
|
||||
void set_has_toggle(bool has_toggle);
|
||||
void set_restore_mode(TemplateValveRestoreMode restore_mode) { restore_mode_ = restore_mode; }
|
||||
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
|
||||
float get_setup_priority() const override;
|
||||
|
||||
protected:
|
||||
void control(const valve::ValveCall &call) override;
|
||||
valve::ValveTraits get_traits() override;
|
||||
void stop_prev_trigger_();
|
||||
|
||||
TemplateValveRestoreMode restore_mode_{VALVE_NO_RESTORE};
|
||||
optional<std::function<optional<float>()>> state_f_;
|
||||
bool assumed_state_{false};
|
||||
bool optimistic_{false};
|
||||
Trigger<> *open_trigger_;
|
||||
Trigger<> *close_trigger_;
|
||||
bool has_stop_{false};
|
||||
bool has_toggle_{false};
|
||||
Trigger<> *stop_trigger_;
|
||||
Trigger<> *toggle_trigger_;
|
||||
Trigger<> *prev_command_trigger_{nullptr};
|
||||
Trigger<float> *position_trigger_;
|
||||
bool has_position_{false};
|
||||
};
|
||||
|
||||
} // namespace template_
|
||||
} // namespace esphome
|
Reference in New Issue
Block a user