ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
5#include "cover.h"
6
7namespace esphome::cover {
8
9template<bool OPEN> class CoverPositionTrigger final : public Trigger<> {
10 public:
11 CoverPositionTrigger(Cover *a_cover) : cover_(a_cover) {
12 a_cover->add_on_state_callback([this]() {
13 if (this->cover_->position != this->last_position_) {
14 this->last_position_ = this->cover_->position;
15 if (this->cover_->position == (OPEN ? COVER_OPEN : COVER_CLOSED))
16 this->trigger();
17 }
18 });
19 }
20
21 protected:
23 float last_position_{NAN};
24};
25
28
29template<CoverOperation OP> class CoverTrigger final : public Trigger<> {
30 public:
31 CoverTrigger(Cover *a_cover) : cover_(a_cover) {
32 a_cover->add_on_state_callback([this]() {
33 auto current_op = this->cover_->current_operation;
34 if (current_op == OP) {
35 if (!this->last_operation_.has_value() || this->last_operation_.value() != OP) {
36 this->trigger();
37 }
38 }
39 this->last_operation_ = current_op;
40 });
41 }
42
43 protected:
45 optional<CoverOperation> last_operation_{};
46};
47} // namespace esphome::cover
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:556
Base class for all cover devices.
Definition cover.h:115
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition cover.h:120
void add_on_state_callback(F &&f)
Definition cover.h:133
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:126
optional< CoverOperation > last_operation_
Definition automation.h:45
CoverTrigger(Cover *a_cover)
Definition automation.h:31