ESPHome  2024.12.2
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 
111  LockCall make_call();
112 
119  void publish_state(LockState state);
120 
123 
125 
130  void lock();
135  void unlock();
140  void open();
141 
146  void add_on_state_callback(std::function<void()> &&callback);
147 
148  protected:
149  friend LockCall;
150 
157  virtual void open_latch() { unlock(); };
158 
167  virtual void control(const LockCall &call) = 0;
168 
169  CallbackManager<void()> state_callback_{};
172 };
173 
174 } // namespace lock
175 } // namespace esphome
void set_supported_states(std::set< LockState > states)
Definition: lock.h:49
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:149
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:124
std::set< LockState > supported_states_
Definition: lock.h:56
Deduplicator< LockState > publish_dedup_
Definition: lock.h:170
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:157
bool get_requires_code() const
Definition: lock.h:42
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
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:171
Base class for all locks.
Definition: lock.h:103
bool state
Definition: fan.h:34