Support toggle action for template cover (#5917)

This commit is contained in:
jochenvg 2023-12-14 05:01:01 +00:00 committed by GitHub
parent 8c37066ed9
commit a3cc551856
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 0 deletions

View file

@ -31,6 +31,7 @@ RESTORE_MODES = {
}
CONF_HAS_POSITION = "has_position"
CONF_TOGGLE_ACTION = "toggle_action"
CONFIG_SCHEMA = cover.COVER_SCHEMA.extend(
{
@ -44,6 +45,7 @@ CONFIG_SCHEMA = cover.COVER_SCHEMA.extend(
cv.Optional(CONF_STOP_ACTION): automation.validate_automation(single=True),
cv.Optional(CONF_TILT_ACTION): automation.validate_automation(single=True),
cv.Optional(CONF_TILT_LAMBDA): cv.returning_lambda,
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="RESTORE"): cv.enum(
RESTORE_MODES, upper=True
@ -74,6 +76,11 @@ async def to_code(config):
var.get_stop_trigger(), [], config[CONF_STOP_ACTION]
)
cg.add(var.set_has_stop(True))
if CONF_TOGGLE_ACTION in config:
await automation.build_automation(
var.get_toggle_trigger(), [], config[CONF_TOGGLE_ACTION]
)
cg.add(var.set_has_toggle(True))
if CONF_TILT_ACTION in config:
await automation.build_automation(
var.get_tilt_trigger(), [(float, "tilt")], config[CONF_TILT_ACTION]

View file

@ -12,6 +12,7 @@ TemplateCover::TemplateCover()
: open_trigger_(new Trigger<>()),
close_trigger_(new Trigger<>),
stop_trigger_(new Trigger<>()),
toggle_trigger_(new Trigger<>()),
position_trigger_(new Trigger<float>()),
tilt_trigger_(new Trigger<float>()) {}
void TemplateCover::setup() {
@ -68,6 +69,7 @@ float TemplateCover::get_setup_priority() const { return setup_priority::HARDWAR
Trigger<> *TemplateCover::get_open_trigger() const { return this->open_trigger_; }
Trigger<> *TemplateCover::get_close_trigger() const { return this->close_trigger_; }
Trigger<> *TemplateCover::get_stop_trigger() const { return this->stop_trigger_; }
Trigger<> *TemplateCover::get_toggle_trigger() const { return this->toggle_trigger_; }
void TemplateCover::dump_config() { LOG_COVER("", "Template Cover", this); }
void TemplateCover::control(const CoverCall &call) {
if (call.get_stop()) {
@ -76,6 +78,12 @@ void TemplateCover::control(const CoverCall &call) {
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_();
@ -110,6 +118,7 @@ CoverTraits TemplateCover::get_traits() {
auto traits = CoverTraits();
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_);
traits.set_supports_tilt(this->has_tilt_);
return traits;
@ -118,6 +127,7 @@ Trigger<float> *TemplateCover::get_position_trigger() const { return this->posit
Trigger<float> *TemplateCover::get_tilt_trigger() const { return this->tilt_trigger_; }
void TemplateCover::set_tilt_lambda(std::function<optional<float>()> &&tilt_f) { this->tilt_f_ = tilt_f; }
void TemplateCover::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; }
void TemplateCover::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; }
void TemplateCover::set_has_position(bool has_position) { this->has_position_ = has_position; }
void TemplateCover::set_has_tilt(bool has_tilt) { this->has_tilt_ = has_tilt; }
void TemplateCover::stop_prev_trigger_() {

View file

@ -21,6 +21,7 @@ class TemplateCover : public cover::Cover, public Component {
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;
Trigger<float> *get_tilt_trigger() const;
void set_optimistic(bool optimistic);
@ -29,6 +30,7 @@ class TemplateCover : public cover::Cover, public Component {
void set_has_stop(bool has_stop);
void set_has_position(bool has_position);
void set_has_tilt(bool has_tilt);
void set_has_toggle(bool has_toggle);
void set_restore_mode(TemplateCoverRestoreMode restore_mode) { restore_mode_ = restore_mode; }
void setup() override;
@ -50,7 +52,9 @@ class TemplateCover : public cover::Cover, public Component {
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};