ESPHome 2025.5.0
Loading...
Searching...
No Matches
lock.cpp
Go to the documentation of this file.
1#include "lock.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace lock {
6
7static const char *const TAG = "lock";
8
10 switch (state) {
12 return "LOCKED";
14 return "UNLOCKED";
16 return "JAMMED";
18 return "LOCKING";
20 return "UNLOCKING";
21 case LOCK_STATE_NONE:
22 default:
23 return "UNKNOWN";
24 }
25}
26
29
30void Lock::lock() {
31 auto call = this->make_call();
32 call.set_state(LOCK_STATE_LOCKED);
33 this->control(call);
34}
36 auto call = this->make_call();
37 call.set_state(LOCK_STATE_UNLOCKED);
38 this->control(call);
39}
40void Lock::open() {
42 ESP_LOGD(TAG, "'%s' Opening.", this->get_name().c_str());
43 this->open_latch();
44 } else {
45 ESP_LOGW(TAG, "'%s' Does not support Open.", this->get_name().c_str());
46 }
47}
49 if (!this->publish_dedup_.next(state))
50 return;
51
52 this->state = state;
53 this->rtc_.save(&this->state);
54 ESP_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), lock_state_to_string(state));
55 this->state_callback_.call();
56}
57
58void Lock::add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
59
61 ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
62 this->validate_();
63 if (this->state_.has_value()) {
64 const char *state_s = lock_state_to_string(*this->state_);
65 ESP_LOGD(TAG, " State: %s", state_s);
66 }
67 this->parent_->control(*this);
68}
70 if (this->state_.has_value()) {
71 auto state = *this->state_;
72 if (!this->parent_->traits.supports_state(state)) {
73 ESP_LOGW(TAG, " State %s is not supported by this device!", lock_state_to_string(*this->state_));
74 this->state_.reset();
75 }
76 }
77}
79 this->state_ = state;
80 return *this;
81}
86LockCall &LockCall::set_state(const std::string &state) {
87 if (str_equals_case_insensitive(state, "LOCKED")) {
89 } else if (str_equals_case_insensitive(state, "UNLOCKED")) {
91 } else if (str_equals_case_insensitive(state, "JAMMED")) {
93 } else if (str_equals_case_insensitive(state, "LOCKING")) {
95 } else if (str_equals_case_insensitive(state, "UNLOCKING")) {
97 } else if (str_equals_case_insensitive(state, "NONE")) {
99 } else {
100 ESP_LOGW(TAG, "'%s' - Unrecognized state %s", this->parent_->get_name().c_str(), state.c_str());
101 }
102 return *this;
103}
104const optional<LockState> &LockCall::get_state() const { return this->state_; }
105
106} // namespace lock
107} // namespace esphome
bool next(T value)
Feeds the next item in the series to the deduplicator and returns whether this is a duplicate.
Definition helpers.h:520
bool save(const T *src)
Definition preferences.h:21
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:68
This class is used to encode all control actions on a lock device.
Definition lock.h:71
Lock *const parent_
Definition lock.h:89
const optional< LockState > & get_state() const
Definition lock.cpp:104
LockCall & set_state(LockState state)
Set the state of the lock device.
Definition lock.cpp:78
optional< LockState > state_
Definition lock.h:90
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:171
Deduplicator< LockState > publish_dedup_
Definition lock.h:170
virtual void open_latch()
Perform the open latch action with hardware.
Definition lock.h:157
LockCall make_call()
Make a lock device control call, this is used to control the lock device, see the LockCall descriptio...
Definition lock.cpp:28
void lock()
Turn this lock on.
Definition lock.cpp:30
LockTraits traits
Definition lock.h:124
void publish_state(LockState state)
Publish a state to the front-end from the back-end.
Definition lock.cpp:48
void add_on_state_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition lock.cpp:58
CallbackManager< void()> state_callback_
Definition lock.h:169
LockState state
The current reported state of the lock.
Definition lock.h:122
void unlock()
Turn this lock off.
Definition lock.cpp:35
friend LockCall
Definition lock.h:149
void open()
Open (unlatch) this lock.
Definition lock.cpp:40
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:87
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:9
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:262