ESPHome 2026.5.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_LOGV(TAG, "'%s' >> %s", this->name_.c_str(), LOG_STR_ARG(lock_state_to_string(state)));
45 this->state_callback_.call(state);
46#if defined(USE_LOCK) && defined(USE_CONTROLLER_REGISTRY)
47 ControllerRegistry::notify_lock_update(this);
48#endif
49}
50
52 ESP_LOGV(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
53 this->validate_();
54 if (this->state_.has_value()) {
55 ESP_LOGV(TAG, " State: %s", LOG_STR_ARG(lock_state_to_string(*this->state_)));
56 }
57 this->parent_->control(*this);
58}
60 if (this->state_.has_value()) {
61 auto state = *this->state_;
62 if (!this->parent_->traits.supports_state(state)) {
63 ESP_LOGW(TAG, " State %s is not supported by this device!", LOG_STR_ARG(lock_state_to_string(*this->state_)));
64 this->state_.reset();
65 }
66 }
67}
69 this->state_ = state;
70 return *this;
71}
72LockCall &LockCall::set_state(optional<LockState> state) {
73 this->state_ = state;
74 return *this;
75}
77 if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("LOCKED")) == 0) {
79 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("UNLOCKED")) == 0) {
81 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("JAMMED")) == 0) {
83 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("LOCKING")) == 0) {
85 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("UNLOCKING")) == 0) {
87 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("NONE")) == 0) {
89 } else {
90 ESP_LOGW(TAG, "'%s' - Unrecognized state %s", this->parent_->get_name().c_str(), state);
91 }
92 return *this;
93}
94const optional<LockState> &LockCall::get_state() const { return this->state_; }
95
96} // namespace esphome::lock
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:1988
const StringRef & get_name() const
Definition entity_base.h:71
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:94
LockCall & set_state(LockState state)
Set the state of the lock device.
Definition lock.cpp:68
LockCall & set_state(const char *state)
Set the state of the lock device based on a string.
Definition lock.cpp:76
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:183
Deduplicator< LockState > publish_dedup_
Definition lock.h:182
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:169
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
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(LockState)> state_callback_
Definition lock.h:181
friend LockCall
Definition lock.h:158
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 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")