ESPHome  2024.4.0
automation.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace lock {
9 
10 template<typename... Ts> class LockAction : public Action<Ts...> {
11  public:
12  explicit LockAction(Lock *a_lock) : lock_(a_lock) {}
13 
14  void play(Ts... x) override { this->lock_->lock(); }
15 
16  protected:
18 };
19 
20 template<typename... Ts> class UnlockAction : public Action<Ts...> {
21  public:
22  explicit UnlockAction(Lock *a_lock) : lock_(a_lock) {}
23 
24  void play(Ts... x) override { this->lock_->unlock(); }
25 
26  protected:
28 };
29 
30 template<typename... Ts> class OpenAction : public Action<Ts...> {
31  public:
32  explicit OpenAction(Lock *a_lock) : lock_(a_lock) {}
33 
34  void play(Ts... x) override { this->lock_->open(); }
35 
36  protected:
38 };
39 
40 template<typename... Ts> class LockCondition : public Condition<Ts...> {
41  public:
42  LockCondition(Lock *parent, bool state) : parent_(parent), state_(state) {}
43  bool check(Ts... x) override {
44  auto check_state = this->state_ ? LockState::LOCK_STATE_LOCKED : LockState::LOCK_STATE_UNLOCKED;
45  return this->parent_->state == check_state;
46  }
47 
48  protected:
50  bool state_;
51 };
52 
53 class LockLockTrigger : public Trigger<> {
54  public:
55  LockLockTrigger(Lock *a_lock) {
56  a_lock->add_on_state_callback([this, a_lock]() {
57  if (a_lock->state == LockState::LOCK_STATE_LOCKED) {
58  this->trigger();
59  }
60  });
61  }
62 };
63 
64 class LockUnlockTrigger : public Trigger<> {
65  public:
67  a_lock->add_on_state_callback([this, a_lock]() {
68  if (a_lock->state == LockState::LOCK_STATE_UNLOCKED) {
69  this->trigger();
70  }
71  });
72  }
73 };
74 
75 template<typename... Ts> class LockPublishAction : public Action<Ts...> {
76  public:
77  LockPublishAction(Lock *a_lock) : lock_(a_lock) {}
79 
80  void play(Ts... x) override { this->lock_->publish_state(this->state_.value(x...)); }
81 
82  protected:
84 };
85 
86 } // namespace lock
87 } // namespace esphome
void play(Ts... x) override
Definition: automation.h:24
bool check(Ts... x) override
Definition: automation.h:43
void play(Ts... x) override
Definition: automation.h:34
LockState state
The current reported state of the lock.
Definition: lock.h:122
void play(Ts... x) override
Definition: automation.h:14
uint16_t x
Definition: tt21100.cpp:17
TEMPLATABLE_VALUE(LockState, state) void play(Ts... x) override
Definition: automation.h:78
LockCondition(Lock *parent, bool state)
Definition: automation.h:42
void add_on_state_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition: lock.cpp:58
LockAction(Lock *a_lock)
Definition: automation.h:12
void lock()
Turn this lock on.
Definition: lock.cpp:30
Base class for all automation conditions.
Definition: automation.h:74
OpenAction(Lock *a_lock)
Definition: automation.h:32
UnlockAction(Lock *a_lock)
Definition: automation.h:22
void open()
Open (unlatch) this lock.
Definition: lock.cpp:40
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void unlock()
Turn this lock off.
Definition: lock.cpp:35
LockState
Enum for all states a lock can be in.
Definition: lock.h:26
Base class for all locks.
Definition: lock.h:103
void publish_state(LockState state)
Publish a state to the front-end from the back-end.
Definition: lock.cpp:48
bool state
Definition: fan.h:34