ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
lock.cpp
Go to the documentation of this file.
1#include "lock.h"
4#include "esphome/core/log.h"
6
7namespace esphome::lock {
8
9static const char *const TAG = "lock";
10
11// Lock state strings indexed by LockState enum (0-5): NONE(UNKNOWN), LOCKED, UNLOCKED, JAMMED, LOCKING, UNLOCKING
12// Index 0 is UNKNOWN (for LOCK_STATE_NONE), also used as fallback for out-of-range
13PROGMEM_STRING_TABLE(LockStateStrings, "UNKNOWN", "LOCKED", "UNLOCKED", "JAMMED", "LOCKING", "UNLOCKING");
14
16 return LockStateStrings::get_log_str(static_cast<uint8_t>(state), 0);
17}
18
21
23 auto call = this->make_call();
24 call.set_state(state);
25 this->control(call);
26}
27
30void Lock::open() {
32 ESP_LOGD(TAG, "'%s' Opening.", this->get_name().c_str());
33 this->open_latch();
34 } else {
35 ESP_LOGW(TAG, "'%s' Does not support Open.", this->get_name().c_str());
36 }
37}
39 if (!this->publish_dedup_.next(state))
40 return;
41
42 this->state = state;
43 this->rtc_.save(&this->state);
44 ESP_LOGD(TAG, "'%s' >> %s", this->name_.c_str(), LOG_STR_ARG(lock_state_to_string(state)));
45 this->state_callback_.call();
46#if defined(USE_LOCK) && defined(USE_CONTROLLER_REGISTRY)
48#endif
49}
50
51void Lock::add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
52
54 ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
55 this->validate_();
56 if (this->state_.has_value()) {
57 ESP_LOGD(TAG, " State: %s", LOG_STR_ARG(lock_state_to_string(*this->state_)));
58 }
59 this->parent_->control(*this);
60}
62 if (this->state_.has_value()) {
63 auto state = *this->state_;
64 if (!this->parent_->traits.supports_state(state)) {
65 ESP_LOGW(TAG, " State %s is not supported by this device!", LOG_STR_ARG(lock_state_to_string(*this->state_)));
66 this->state_.reset();
67 }
68 }
69}
71 this->state_ = state;
72 return *this;
73}
79 if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("LOCKED")) == 0) {
81 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("UNLOCKED")) == 0) {
83 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("JAMMED")) == 0) {
85 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("LOCKING")) == 0) {
87 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("UNLOCKING")) == 0) {
89 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("NONE")) == 0) {
91 } else {
92 ESP_LOGW(TAG, "'%s' - Unrecognized state %s", this->parent_->get_name().c_str(), state);
93 }
94 return *this;
95}
96const optional<LockState> &LockCall::get_state() const { return this->state_; }
97
98} // 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:1593
bool save(const T *src)
Definition preferences.h:21
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:73
This class is used to encode all control actions on a lock device.
Definition lock.h:77
Lock *const parent_
Definition lock.h:96
const optional< LockState > & get_state() const
Definition lock.cpp:96
LockCall & set_state(LockState state)
Set the state of the lock device.
Definition lock.cpp:70
LockCall & set_state(const char *state)
Set the state of the lock device based on a string.
Definition lock.cpp:78
optional< LockState > state_
Definition lock.h:97
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:181
Deduplicator< LockState > publish_dedup_
Definition lock.h:180
void set_state_(LockState state)
Helper for lock/unlock convenience methods.
Definition lock.cpp:22
virtual void open_latch()
Perform the open latch action with hardware.
Definition lock.h:167
LockCall make_call()
Make a lock device control call, this is used to control the lock device, see the LockCall descriptio...
Definition lock.cpp:20
void lock()
Turn this lock on.
Definition lock.cpp:28
LockTraits traits
Definition lock.h:131
void publish_state(LockState state)
Publish a state to the front-end from the back-end.
Definition lock.cpp:38
void add_on_state_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition lock.cpp:51
LockState state
The current reported state of the lock.
Definition lock.h:129
void unlock()
Turn this lock off.
Definition lock.cpp:29
LazyCallbackManager< void()> state_callback_
Definition lock.h:179
friend LockCall
Definition lock.h:156
void open()
Open (unlatch) this lock.
Definition lock.cpp:30
bool supports_state(LockState state) const
Definition lock.h:47
bool get_supports_open() const
Definition lock.h:40
bool has_value() const
Definition optional.h:92
bool state
Definition fan.h:2
const LogString * lock_state_to_string(LockState state)
Definition lock.cpp:15
LockState
Enum for all states a lock can be in.
Definition lock.h:23
@ LOCK_STATE_LOCKING
Definition lock.h:28
@ LOCK_STATE_NONE
Definition lock.h:24
@ LOCK_STATE_UNLOCKING
Definition lock.h:29
@ LOCK_STATE_JAMMED
Definition lock.h:27
@ LOCK_STATE_UNLOCKED
Definition lock.h:26
@ LOCK_STATE_LOCKED
Definition lock.h:25
PROGMEM_STRING_TABLE(LockStateStrings, "UNKNOWN", "LOCKED", "UNLOCKED", "JAMMED", "LOCKING", "UNLOCKING")