ESPHome 2026.6.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.
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", "OPENING",
14 "OPEN");
15
17 return LockStateStrings::get_log_str(static_cast<uint8_t>(state), 0);
18}
19
22
24 auto call = this->make_call();
25 call.set_state(state);
26 this->control(call);
27}
28
31void Lock::open() {
33 ESP_LOGD(TAG, "'%s' Opening.", this->get_name().c_str());
34 this->open_latch();
35 } else {
36 ESP_LOGW(TAG, "'%s' Does not support Open.", this->get_name().c_str());
37 }
38}
40 if (!this->publish_dedup_.next(state))
41 return;
42
43 this->state = state;
44 this->rtc_.save(&this->state);
45 ESP_LOGV(TAG, "'%s' >> %s", this->name_.c_str(), LOG_STR_ARG(lock_state_to_string(state)));
46 this->state_callback_.call(state);
47#if defined(USE_LOCK) && defined(USE_CONTROLLER_REGISTRY)
48 ControllerRegistry::notify_lock_update(this);
49#endif
50}
51
53 ESP_LOGV(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
54 this->validate_();
55 if (this->state_.has_value()) {
56 ESP_LOGV(TAG, " State: %s", LOG_STR_ARG(lock_state_to_string(*this->state_)));
57 }
58 this->parent_->control(*this);
59}
61 if (this->state_.has_value()) {
62 auto state = *this->state_;
63 if (!this->parent_->traits.supports_state(state)) {
64 ESP_LOGW(TAG, " State %s is not supported by this device!", LOG_STR_ARG(lock_state_to_string(*this->state_)));
65 this->state_.reset();
66 }
67 }
68}
70 this->state_ = state;
71 return *this;
72}
73LockCall &LockCall::set_state(optional<LockState> state) {
74 this->state_ = state;
75 return *this;
76}
78 if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("OPEN")) == 0) {
80 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("LOCKED")) == 0) {
82 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("UNLOCKED")) == 0) {
84 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("JAMMED")) == 0) {
86 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("OPENING")) == 0) {
88 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("LOCKING")) == 0) {
90 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("UNLOCKING")) == 0) {
92 } else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("NONE")) == 0) {
94 } else {
95 ESP_LOGW(TAG, "'%s' - Unrecognized state %s", this->parent_->get_name().c_str(), state);
96 }
97 return *this;
98}
99const optional<LockState> &LockCall::get_state() const { return this->state_; }
100
101} // 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:1836
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:79
Lock *const parent_
Definition lock.h:98
const optional< LockState > & get_state() const
Definition lock.cpp:99
LockCall & set_state(LockState state)
Set the state of the lock device.
Definition lock.cpp:69
optional< LockState > state_
Definition lock.h:99
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:185
Deduplicator< LockState > publish_dedup_
Definition lock.h:184
void set_state_(LockState state)
Helper for lock/unlock convenience methods.
Definition lock.cpp:23
virtual void open_latch()
Perform the open latch action with hardware.
Definition lock.h:171
LockCall make_call()
Make a lock device control call, this is used to control the lock device, see the LockCall descriptio...
Definition lock.cpp:21
void lock()
Turn this lock on.
Definition lock.cpp:29
LockTraits traits
Definition lock.h:133
void publish_state(LockState state)
Publish a state to the front-end from the back-end.
Definition lock.cpp:39
LockState state
The current reported state of the lock.
Definition lock.h:131
void unlock()
Turn this lock off.
Definition lock.cpp:30
LazyCallbackManager< void(LockState)> state_callback_
Definition lock.h:183
friend LockCall
Definition lock.h:160
void open()
Open (unlatch) this lock.
Definition lock.cpp:31
bool supports_state(LockState state) const
Definition lock.h:49
bool get_supports_open() const
Definition lock.h:42
bool state
Definition fan.h:2
const LogString * lock_state_to_string(LockState state)
Definition lock.cpp:16
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_OPENING
Definition lock.h:30
@ LOCK_STATE_OPEN
Definition lock.h:31
@ 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", "OPENING", "OPEN")