ESPHome 2025.5.0
Loading...
Searching...
No Matches
template_lock.cpp
Go to the documentation of this file.
1#include "template_lock.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace template_ {
6
7using namespace esphome::lock;
8
9static 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) {
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) {
43 }
44 this->prev_trigger_ = this->open_trigger_;
45 this->open_trigger_->trigger();
46}
47void TemplateLock::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
48void 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
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:104
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition automation.h:96
This class is used to encode all control actions on a lock device.
Definition lock.h:71
const optional< LockState > & get_state() const
Definition lock.cpp:104
void publish_state(LockState state)
Publish a state to the front-end from the back-end.
Definition lock.cpp:48
LockState state
The current reported state of the lock.
Definition lock.h:122
float get_setup_priority() const override
void set_optimistic(bool optimistic)
void control(const lock::LockCall &call) override
void set_state_lambda(std::function< optional< lock::LockState >()> &&f)
optional< std::function< optional< lock::LockState >()> > f_
mopeka_std_values val[4]
@ LOCK_STATE_UNLOCKED
Definition lock.h:29
@ LOCK_STATE_LOCKED
Definition lock.h:28
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:18
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7