ESPHome 2025.12.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"
5
6namespace esphome {
7namespace lock {
8
9static const char *const TAG = "lock";
10
12 switch (state) {
14 return "LOCKED";
16 return "UNLOCKED";
18 return "JAMMED";
20 return "LOCKING";
22 return "UNLOCKING";
23 case LOCK_STATE_NONE:
24 default:
25 return "UNKNOWN";
26 }
27}
28
31
32void Lock::lock() {
33 auto call = this->make_call();
34 call.set_state(LOCK_STATE_LOCKED);
35 this->control(call);
36}
38 auto call = this->make_call();
39 call.set_state(LOCK_STATE_UNLOCKED);
40 this->control(call);
41}
42void Lock::open() {
44 ESP_LOGD(TAG, "'%s' Opening.", this->get_name().c_str());
45 this->open_latch();
46 } else {
47 ESP_LOGW(TAG, "'%s' Does not support Open.", this->get_name().c_str());
48 }
49}
51 if (!this->publish_dedup_.next(state))
52 return;
53
54 this->state = state;
55 this->rtc_.save(&this->state);
56 ESP_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), lock_state_to_string(state));
57 this->state_callback_.call();
58#if defined(USE_LOCK) && defined(USE_CONTROLLER_REGISTRY)
60#endif
61}
62
63void Lock::add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
64
66 ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
67 this->validate_();
68 if (this->state_.has_value()) {
69 const char *state_s = lock_state_to_string(*this->state_);
70 ESP_LOGD(TAG, " State: %s", state_s);
71 }
72 this->parent_->control(*this);
73}
75 if (this->state_.has_value()) {
76 auto state = *this->state_;
77 if (!this->parent_->traits.supports_state(state)) {
78 ESP_LOGW(TAG, " State %s is not supported by this device!", lock_state_to_string(*this->state_));
79 this->state_.reset();
80 }
81 }
82}
84 this->state_ = state;
85 return *this;
86}
91LockCall &LockCall::set_state(const std::string &state) {
92 if (str_equals_case_insensitive(state, "LOCKED")) {
94 } else if (str_equals_case_insensitive(state, "UNLOCKED")) {
96 } else if (str_equals_case_insensitive(state, "JAMMED")) {
98 } else if (str_equals_case_insensitive(state, "LOCKING")) {
100 } else if (str_equals_case_insensitive(state, "UNLOCKING")) {
102 } else if (str_equals_case_insensitive(state, "NONE")) {
104 } else {
105 ESP_LOGW(TAG, "'%s' - Unrecognized state %s", this->parent_->get_name().c_str(), state.c_str());
106 }
107 return *this;
108}
109const optional<LockState> &LockCall::get_state() const { return this->state_; }
110
111} // namespace lock
112} // namespace esphome
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:893
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:77
Lock *const parent_
Definition lock.h:95
const optional< LockState > & get_state() const
Definition lock.cpp:109
LockCall & set_state(LockState state)
Set the state of the lock device.
Definition lock.cpp:83
optional< LockState > state_
Definition lock.h:96
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:177
Deduplicator< LockState > publish_dedup_
Definition lock.h:176
virtual void open_latch()
Perform the open latch action with hardware.
Definition lock.h:163
LockCall make_call()
Make a lock device control call, this is used to control the lock device, see the LockCall descriptio...
Definition lock.cpp:30
void lock()
Turn this lock on.
Definition lock.cpp:32
LockTraits traits
Definition lock.h:130
void publish_state(LockState state)
Publish a state to the front-end from the back-end.
Definition lock.cpp:50
void add_on_state_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition lock.cpp:63
CallbackManager< void()> state_callback_
Definition lock.h:175
LockState state
The current reported state of the lock.
Definition lock.h:128
void unlock()
Turn this lock off.
Definition lock.cpp:37
friend LockCall
Definition lock.h:155
void open()
Open (unlatch) this lock.
Definition lock.cpp:42
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:0
LockState
Enum for all states a lock can be in.
Definition lock.h:26
@ LOCK_STATE_LOCKING
Definition lock.h:31
@ LOCK_STATE_NONE
Definition lock.h:27
@ LOCK_STATE_UNLOCKING
Definition lock.h:32
@ LOCK_STATE_JAMMED
Definition lock.h:30
@ LOCK_STATE_UNLOCKED
Definition lock.h:29
@ LOCK_STATE_LOCKED
Definition lock.h:28
const char * lock_state_to_string(LockState state)
Definition lock.cpp:11
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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