ESPHome  2022.9.1
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 
27 Lock::Lock(const std::string &name) : EntityBase(name), state(LOCK_STATE_NONE) {}
28 Lock::Lock() : Lock("") {}
29 LockCall Lock::make_call() { return LockCall(this); }
30 
31 void Lock::lock() {
32  auto call = this->make_call();
33  call.set_state(LOCK_STATE_LOCKED);
34  this->control(call);
35 }
36 void Lock::unlock() {
37  auto call = this->make_call();
38  call.set_state(LOCK_STATE_UNLOCKED);
39  this->control(call);
40 }
41 void Lock::open() {
42  if (traits.get_supports_open()) {
43  ESP_LOGD(TAG, "'%s' Opening.", this->get_name().c_str());
44  this->open_latch();
45  } else {
46  ESP_LOGW(TAG, "'%s' Does not support Open.", this->get_name().c_str());
47  }
48 }
50  if (!this->publish_dedup_.next(state))
51  return;
52 
53  this->state = state;
54  this->rtc_.save(&this->state);
55  ESP_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), lock_state_to_string(state));
56  this->state_callback_.call();
57 }
58 
59 void Lock::add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
60 
62  ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
63  this->validate_();
64  if (this->state_.has_value()) {
65  const char *state_s = lock_state_to_string(*this->state_);
66  ESP_LOGD(TAG, " State: %s", state_s);
67  }
68  this->parent_->control(*this);
69 }
71  if (this->state_.has_value()) {
72  auto state = *this->state_;
73  if (!this->parent_->traits.supports_state(state)) {
74  ESP_LOGW(TAG, " State %s is not supported by this device!", lock_state_to_string(*this->state_));
75  this->state_.reset();
76  }
77  }
78 }
80  this->state_ = state;
81  return *this;
82 }
84  this->state_ = state;
85  return *this;
86 }
87 LockCall &LockCall::set_state(const std::string &state) {
88  if (str_equals_case_insensitive(state, "LOCKED")) {
89  this->set_state(LOCK_STATE_LOCKED);
90  } else if (str_equals_case_insensitive(state, "UNLOCKED")) {
91  this->set_state(LOCK_STATE_UNLOCKED);
92  } else if (str_equals_case_insensitive(state, "JAMMED")) {
93  this->set_state(LOCK_STATE_JAMMED);
94  } else if (str_equals_case_insensitive(state, "LOCKING")) {
95  this->set_state(LOCK_STATE_LOCKING);
96  } else if (str_equals_case_insensitive(state, "UNLOCKING")) {
97  this->set_state(LOCK_STATE_UNLOCKING);
98  } else if (str_equals_case_insensitive(state, "NONE")) {
99  this->set_state(LOCK_STATE_NONE);
100  } else {
101  ESP_LOGW(TAG, "'%s' - Unrecognized state %s", this->parent_->get_name().c_str(), state.c_str());
102  }
103  return *this;
104 }
105 const optional<LockState> &LockCall::get_state() const { return this->state_; }
106 
107 } // namespace lock
108 } // namespace esphome
LockCall & set_state(LockState state)
Set the state of the lock device.
Definition: lock.cpp:79
const char * name
Definition: stm32flash.h: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:482
LockState state
The current reported state of the lock.
Definition: lock.h:123
const char * lock_state_to_string(LockState state)
Definition: lock.cpp:9
const optional< LockState > & get_state() const
Definition: lock.cpp:105
CallbackManager< void()> state_callback_
Definition: lock.h:170
LockCall make_call()
Make a lock device control call, this is used to control the lock device, see the LockCall descriptio...
Definition: lock.cpp:29
std::string name_
Definition: entity_base.h:54
virtual void control(const LockCall &call)=0
Control the lock device, this is a virtual method that each lock integration must implement...
const std::string & get_name() const
Definition: entity_base.cpp:11
friend LockCall
Definition: lock.h:150
void add_on_state_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition: lock.cpp:59
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:125
Deduplicator< LockState > publish_dedup_
Definition: lock.h:171
void lock()
Turn this lock on.
Definition: lock.cpp:31
virtual void open_latch()
Perform the open latch action with hardware.
Definition: lock.h:158
void open()
Open (unlatch) this lock.
Definition: lock.cpp:41
Definition: a4988.cpp:4
void unlock()
Turn this lock off.
Definition: lock.cpp:36
LockState
Enum for all states a lock can be in.
Definition: lock.h:26
bool get_supports_open() const
Definition: lock.h:40
ESPPreferenceObject rtc_
Definition: lock.h:172
Base class for all locks.
Definition: lock.h:103
void publish_state(LockState state)
Publish a state to the front-end from the back-end.
Definition: lock.cpp:49
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:112