ESPHome  2024.11.0
template_lock.cpp
Go to the documentation of this file.
1 #include "template_lock.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace template_ {
6 
7 using namespace esphome::lock;
8 
9 static const char *const TAG = "template.lock";
10 
12  : lock_trigger_(new Trigger<>()), unlock_trigger_(new Trigger<>()), open_trigger_(new Trigger<>()) {}
13 
15  if (!this->f_.has_value())
16  return;
17  auto val = (*this->f_)();
18  if (!val.has_value())
19  return;
20 
21  this->publish_state(*val);
22 }
24  if (this->prev_trigger_ != nullptr) {
25  this->prev_trigger_->stop_action();
26  }
27 
28  auto state = *call.get_state();
29  if (state == LOCK_STATE_LOCKED) {
30  this->prev_trigger_ = this->lock_trigger_;
31  this->lock_trigger_->trigger();
32  } else if (state == LOCK_STATE_UNLOCKED) {
33  this->prev_trigger_ = this->unlock_trigger_;
34  this->unlock_trigger_->trigger();
35  }
36 
37  if (this->optimistic_)
38  this->publish_state(state);
39 }
41  if (this->prev_trigger_ != nullptr) {
42  this->prev_trigger_->stop_action();
43  }
44  this->prev_trigger_ = this->open_trigger_;
45  this->open_trigger_->trigger();
46 }
47 void TemplateLock::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
48 void TemplateLock::set_state_lambda(std::function<optional<lock::LockState>()> &&f) { this->f_ = f; }
54  LOG_LOCK("", "Template Lock", this);
55  ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
56 }
57 
58 } // namespace template_
59 } // namespace esphome
LockState state
The current reported state of the lock.
Definition: lock.h:122
const optional< LockState > & get_state() const
Definition: lock.cpp:104
mopeka_std_values val[4]
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
This class is used to encode all control actions on a lock device.
Definition: lock.h:71
const char *const TAG
Definition: spi.cpp:8
void set_optimistic(bool optimistic)
void set_state_lambda(std::function< optional< lock::LockState >()> &&f)
Trigger * get_unlock_trigger() const
float get_setup_priority() const override
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void control(const lock::LockCall &call) override
optional< std::function< optional< lock::LockState >)> > f_
Definition: template_lock.h:29
void stop_action()
Stop any action connected to this trigger.
Definition: automation.h:103
void publish_state(LockState state)
Publish a state to the front-end from the back-end.
Definition: lock.cpp:48