#pragma once #include "esphome/core/component.h" #include "esphome/core/automation.h" #include "cover.h" namespace esphome::cover { template class OpenAction : public Action { public: explicit OpenAction(Cover *cover) : cover_(cover) {} void play(const Ts &...x) override { this->cover_->make_call().set_command_open().perform(); } protected: Cover *cover_; }; template class CloseAction : public Action { public: explicit CloseAction(Cover *cover) : cover_(cover) {} void play(const Ts &...x) override { this->cover_->make_call().set_command_close().perform(); } protected: Cover *cover_; }; template class StopAction : public Action { public: explicit StopAction(Cover *cover) : cover_(cover) {} void play(const Ts &...x) override { this->cover_->make_call().set_command_stop().perform(); } protected: Cover *cover_; }; template class ToggleAction : public Action { public: explicit ToggleAction(Cover *cover) : cover_(cover) {} void play(const Ts &...x) override { this->cover_->make_call().set_command_toggle().perform(); } protected: Cover *cover_; }; template class ControlAction : public Action { public: explicit ControlAction(Cover *cover) : cover_(cover) {} TEMPLATABLE_VALUE(bool, stop) TEMPLATABLE_VALUE(float, position) TEMPLATABLE_VALUE(float, tilt) void play(const Ts &...x) override { auto call = this->cover_->make_call(); if (this->stop_.has_value()) call.set_stop(this->stop_.value(x...)); if (this->position_.has_value()) call.set_position(this->position_.value(x...)); if (this->tilt_.has_value()) call.set_tilt(this->tilt_.value(x...)); call.perform(); } protected: Cover *cover_; }; template class CoverPublishAction : public Action { public: CoverPublishAction(Cover *cover) : cover_(cover) {} TEMPLATABLE_VALUE(float, position) TEMPLATABLE_VALUE(float, tilt) TEMPLATABLE_VALUE(CoverOperation, current_operation) void play(const Ts &...x) override { if (this->position_.has_value()) this->cover_->position = this->position_.value(x...); if (this->tilt_.has_value()) this->cover_->tilt = this->tilt_.value(x...); if (this->current_operation_.has_value()) this->cover_->current_operation = this->current_operation_.value(x...); this->cover_->publish_state(); } protected: Cover *cover_; }; template class CoverPositionCondition : public Condition { public: CoverPositionCondition(Cover *cover) : cover_(cover) {} bool check(const Ts &...x) override { return this->cover_->position == (OPEN ? COVER_OPEN : COVER_CLOSED); } protected: Cover *cover_; }; template using CoverIsOpenCondition = CoverPositionCondition; template using CoverIsClosedCondition = CoverPositionCondition; template class CoverPositionTrigger : public Trigger<> { public: CoverPositionTrigger(Cover *a_cover) : cover_(a_cover) { a_cover->add_on_state_callback([this]() { if (this->cover_->position != this->last_position_) { this->last_position_ = this->cover_->position; if (this->cover_->position == (OPEN ? COVER_OPEN : COVER_CLOSED)) this->trigger(); } }); } protected: Cover *cover_; float last_position_{NAN}; }; using CoverOpenedTrigger = CoverPositionTrigger; using CoverClosedTrigger = CoverPositionTrigger; template class CoverTrigger : public Trigger<> { public: CoverTrigger(Cover *a_cover) : cover_(a_cover) { a_cover->add_on_state_callback([this]() { auto current_op = this->cover_->current_operation; if (current_op == OP) { if (!this->last_operation_.has_value() || this->last_operation_.value() != OP) { this->trigger(); } } this->last_operation_ = current_op; }); } protected: Cover *cover_; optional last_operation_{}; }; } // namespace esphome::cover