ESPHome 2025.12.0
Loading...
Searching...
No Matches
lock.cpp
Go to the documentation of this file.
1#include "lock.h"
4#include "esphome/core/log.h"
5
6namespace esphome::lock {
7
8static const char *const TAG = "lock";
9
11 switch (state) {
13 return LOG_STR("LOCKED");
15 return LOG_STR("UNLOCKED");
17 return LOG_STR("JAMMED");
19 return LOG_STR("LOCKING");
21 return LOG_STR("UNLOCKING");
22 case LOCK_STATE_NONE:
23 default:
24 return LOG_STR("UNKNOWN");
25 }
26}
27
30
31void Lock::lock() {
32 auto call = this->make_call();
33 call.set_state(LOCK_STATE_LOCKED);
34 this->control(call);
35}
37 auto call = this->make_call();
38 call.set_state(LOCK_STATE_UNLOCKED);
39 this->control(call);
40}
41void Lock::open() {
43 ESP_LOGD(TAG, "'%s' Opening.", this->get_name().c_str());
44 this->open_latch();
45 } else {
46 ESP_LOGW(TAG, "'%s' Does not support Open.", this->get_name().c_str());
47 }
48}
50 if (!this->publish_dedup_.next(state))
51 return;
52
53 this->state = state;
54 this->rtc_.save(&this->state);
55 ESP_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), LOG_STR_ARG(lock_state_to_string(state)));
56 this->state_callback_.call();
57#if defined(USE_LOCK) && defined(USE_CONTROLLER_REGISTRY)
59#endif
60}
61
62void Lock::add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
63
65 ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
66 this->validate_();
67 if (this->state_.has_value()) {
68 ESP_LOGD(TAG, " State: %s", LOG_STR_ARG(lock_state_to_string(*this->state_)));
69 }
70 this->parent_->control(*this);
71}
73 if (this->state_.has_value()) {
74 auto state = *this->state_;
75 if (!this->parent_->traits.supports_state(state)) {
76 ESP_LOGW(TAG, " State %s is not supported by this device!", LOG_STR_ARG(lock_state_to_string(*this->state_)));
77 this->state_.reset();
78 }
79 }
80}
82 this->state_ = state;
83 return *this;
84}
89LockCall &LockCall::set_state(const std::string &state) {
90 if (str_equals_case_insensitive(state, "LOCKED")) {
92 } else if (str_equals_case_insensitive(state, "UNLOCKED")) {
94 } else if (str_equals_case_insensitive(state, "JAMMED")) {
96 } else if (str_equals_case_insensitive(state, "LOCKING")) {
98 } else if (str_equals_case_insensitive(state, "UNLOCKING")) {
100 } else if (str_equals_case_insensitive(state, "NONE")) {
102 } else {
103 ESP_LOGW(TAG, "'%s' - Unrecognized state %s", this->parent_->get_name().c_str(), state.c_str());
104 }
105 return *this;
106}
107const optional<LockState> &LockCall::get_state() const { return this->state_; }
108
109} // namespace esphome::lock
static void notify_lock_update(lock::Lock *obj)
bool next(T value)
Feeds the next item in the series to the deduplicator and returns false if this is a duplicate.
Definition helpers.h:907
bool save(const T *src)
Definition preferences.h:21
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:69
This class is used to encode all control actions on a lock device.
Definition lock.h:79
Lock *const parent_
Definition lock.h:97
const optional< LockState > & get_state() const
Definition lock.cpp:107
LockCall & set_state(LockState state)
Set the state of the lock device.
Definition lock.cpp:81
optional< LockState > state_
Definition lock.h:98
virtual void control(const LockCall &call)=0
Control the lock device, this is a virtual method that each lock integration must implement.
ESPPreferenceObject rtc_
Definition lock.h:179
Deduplicator< LockState > publish_dedup_
Definition lock.h:178
virtual void open_latch()
Perform the open latch action with hardware.
Definition lock.h:165
LockCall make_call()
Make a lock device control call, this is used to control the lock device, see the LockCall descriptio...
Definition lock.cpp:29
void lock()
Turn this lock on.
Definition lock.cpp:31
LockTraits traits
Definition lock.h:132
void publish_state(LockState state)
Publish a state to the front-end from the back-end.
Definition lock.cpp:49
void add_on_state_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition lock.cpp:62
CallbackManager< void()> state_callback_
Definition lock.h:177
LockState state
The current reported state of the lock.
Definition lock.h:130
void unlock()
Turn this lock off.
Definition lock.cpp:36
friend LockCall
Definition lock.h:157
void open()
Open (unlatch) this lock.
Definition lock.cpp:41
bool supports_state(LockState state) const
Definition lock.h:49
bool get_supports_open() const
Definition lock.h:42
bool has_value() const
Definition optional.h:92
bool state
Definition fan.h:0
const LogString * lock_state_to_string(LockState state)
Definition lock.cpp:10
LockState
Enum for all states a lock can be in.
Definition lock.h:25
@ LOCK_STATE_LOCKING
Definition lock.h:30
@ LOCK_STATE_NONE
Definition lock.h:26
@ LOCK_STATE_UNLOCKING
Definition lock.h:31
@ LOCK_STATE_JAMMED
Definition lock.h:29
@ LOCK_STATE_UNLOCKED
Definition lock.h:28
@ LOCK_STATE_LOCKED
Definition lock.h:27
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition helpers.cpp:161