ESPHome  2024.11.2
lock.cpp
Go to the documentation of this file.
1 #include "lock.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace lock {
6 
7 static const char *const TAG = "lock";
8 
10  switch (state) {
11  case LOCK_STATE_LOCKED:
12  return "LOCKED";
14  return "UNLOCKED";
15  case LOCK_STATE_JAMMED:
16  return "JAMMED";
17  case LOCK_STATE_LOCKING:
18  return "LOCKING";
20  return "UNLOCKING";
21  case LOCK_STATE_NONE:
22  default:
23  return "UNKNOWN";
24  }
25 }
26 
28 LockCall Lock::make_call() { return LockCall(this); }
29 
30 void Lock::lock() {
31  auto call = this->make_call();
32  call.set_state(LOCK_STATE_LOCKED);
33  this->control(call);
34 }
35 void Lock::unlock() {
36  auto call = this->make_call();
37  call.set_state(LOCK_STATE_UNLOCKED);
38  this->control(call);
39 }
40 void Lock::open() {
41  if (traits.get_supports_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 
58 void 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 }
83  this->state_ = state;
84  return *this;
85 }
86 LockCall &LockCall::set_state(const std::string &state) {
87  if (str_equals_case_insensitive(state, "LOCKED")) {
88  this->set_state(LOCK_STATE_LOCKED);
89  } else if (str_equals_case_insensitive(state, "UNLOCKED")) {
90  this->set_state(LOCK_STATE_UNLOCKED);
91  } else if (str_equals_case_insensitive(state, "JAMMED")) {
92  this->set_state(LOCK_STATE_JAMMED);
93  } else if (str_equals_case_insensitive(state, "LOCKING")) {
94  this->set_state(LOCK_STATE_LOCKING);
95  } else if (str_equals_case_insensitive(state, "UNLOCKING")) {
96  this->set_state(LOCK_STATE_UNLOCKING);
97  } else if (str_equals_case_insensitive(state, "NONE")) {
98  this->set_state(LOCK_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 }
104 const optional<LockState> &LockCall::get_state() const { return this->state_; }
105 
106 } // namespace lock
107 } // namespace esphome
LockCall & set_state(LockState state)
Set the state of the lock device.
Definition: lock.cpp:78
bool next(T value)
Feeds the next item in the series to the deduplicator and returns whether this is a duplicate...
Definition: helpers.h:504
LockState state
The current reported state of the lock.
Definition: lock.h:122
const char * lock_state_to_string(LockState state)
Definition: lock.cpp:9
const optional< LockState > & get_state() const
Definition: lock.cpp:104
CallbackManager< void()> state_callback_
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:28
virtual void control(const LockCall &call)=0
Control the lock device, this is a virtual method that each lock integration must implement...
friend LockCall
Definition: lock.h:149
void add_on_state_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition: lock.cpp:58
bool save(const T *src)
Definition: preferences.h:21
This class is used to encode all control actions on a lock device.
Definition: lock.h:71
LockTraits traits
Definition: lock.h:124
Deduplicator< LockState > publish_dedup_
Definition: lock.h:170
void lock()
Turn this lock on.
Definition: lock.cpp:30
constexpr const char * c_str() const
Definition: string_ref.h:68
virtual void open_latch()
Perform the open latch action with hardware.
Definition: lock.h:157
void open()
Open (unlatch) this lock.
Definition: lock.cpp:40
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void unlock()
Turn this lock off.
Definition: lock.cpp:35
LockState
Enum for all states a lock can be in.
Definition: lock.h:26
bool get_supports_open() const
Definition: lock.h:40
const StringRef & get_name() const
Definition: entity_base.cpp:10
ESPPreferenceObject rtc_
Definition: lock.h:171
void publish_state(LockState state)
Publish a state to the front-end from the back-end.
Definition: lock.cpp:48
bool state
Definition: fan.h:34
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition: helpers.cpp:259