ESPHome  2022.11.4
lock.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "esphome/core/helpers.h"
7 #include "esphome/core/log.h"
8 #include <set>
9 
10 namespace esphome {
11 namespace lock {
12 
13 class Lock;
14 
15 #define LOG_LOCK(prefix, type, obj) \
16  if ((obj) != nullptr) { \
17  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
18  if (!(obj)->get_icon().empty()) { \
19  ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
20  } \
21  if ((obj)->traits.get_assumed_state()) { \
22  ESP_LOGCONFIG(TAG, "%s Assumed State: YES", prefix); \
23  } \
24  }
25 enum LockState : uint8_t {
33 };
35 
36 class LockTraits {
37  public:
38  LockTraits() = default;
39 
40  bool get_supports_open() const { return this->supports_open_; }
41  void set_supports_open(bool supports_open) { this->supports_open_ = supports_open; }
42  bool get_requires_code() const { return this->requires_code_; }
43  void set_requires_code(bool requires_code) { this->requires_code_ = requires_code; }
44  bool get_assumed_state() const { return this->assumed_state_; }
45  void set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
46 
47  bool supports_state(LockState state) const { return supported_states_.count(state); }
48  std::set<LockState> get_supported_states() const { return supported_states_; }
49  void set_supported_states(std::set<LockState> states) { supported_states_ = std::move(states); }
51 
52  protected:
53  bool supports_open_{false};
54  bool requires_code_{false};
55  bool assumed_state_{false};
57 };
58 
71 class LockCall {
72  public:
73  LockCall(Lock *parent) : parent_(parent) {}
74 
76  LockCall &set_state(LockState state);
78  LockCall &set_state(optional<LockState> state);
80  LockCall &set_state(const std::string &state);
81 
82  void perform();
83 
84  const optional<LockState> &get_state() const;
85 
86  protected:
87  void validate_();
88 
89  Lock *const parent_;
91 };
92 
103 class Lock : public EntityBase {
104  public:
105  explicit Lock();
106  explicit Lock(const std::string &name);
107 
112  LockCall make_call();
113 
120  void publish_state(LockState state);
121 
124 
126 
131  void lock();
136  void unlock();
141  void open();
142 
147  void add_on_state_callback(std::function<void()> &&callback);
148 
149  protected:
150  friend LockCall;
151 
158  virtual void open_latch() { unlock(); };
159 
168  virtual void control(const LockCall &call) = 0;
169 
170  CallbackManager<void()> state_callback_{};
173 };
174 
175 } // namespace lock
176 } // namespace esphome
void set_supported_states(std::set< LockState > states)
Definition: lock.h:49
const char * name
Definition: stm32flash.h:78
std::set< LockState > get_supported_states() const
Definition: lock.h:48
LockCall(Lock *parent)
Definition: lock.h:73
optional< LockState > state_
Definition: lock.h:90
Lock *const parent_
Definition: lock.h:89
const char * lock_state_to_string(LockState state)
Definition: lock.cpp:9
void set_supports_open(bool supports_open)
Definition: lock.h:41
friend LockCall
Definition: lock.h:150
void set_assumed_state(bool assumed_state)
Definition: lock.h:45
This class is used to encode all control actions on a lock device.
Definition: lock.h:71
LockTraits traits
Definition: lock.h:125
std::set< LockState > supported_states_
Definition: lock.h:56
Deduplicator< LockState > publish_dedup_
Definition: lock.h:171
bool supports_state(LockState state) const
Definition: lock.h:47
void add_supported_state(LockState state)
Definition: lock.h:50
bool get_assumed_state() const
Definition: lock.h:44
virtual void open_latch()
Perform the open latch action with hardware.
Definition: lock.h:158
bool get_requires_code() const
Definition: lock.h:42
Definition: a4988.cpp:4
LockState
Enum for all states a lock can be in.
Definition: lock.h:26
void set_requires_code(bool requires_code)
Definition: lock.h:43
bool get_supports_open() const
Definition: lock.h:40
ESPPreferenceObject rtc_
Definition: lock.h:172
Base class for all locks.
Definition: lock.h:103
bool state
Definition: fan.h:34