ESPHome  2024.11.1
mqtt_lock.cpp
Go to the documentation of this file.
1 #include "mqtt_lock.h"
2 #include "esphome/core/log.h"
3 
4 #include "mqtt_const.h"
5 
6 #ifdef USE_MQTT
7 #ifdef USE_LOCK
8 
9 namespace esphome {
10 namespace mqtt {
11 
12 static const char *const TAG = "mqtt.lock";
13 
14 using namespace esphome::lock;
15 
17 
19  this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
20  if (strcasecmp(payload.c_str(), "LOCK") == 0) {
21  this->lock_->lock();
22  } else if (strcasecmp(payload.c_str(), "UNLOCK") == 0) {
23  this->lock_->unlock();
24  } else if (strcasecmp(payload.c_str(), "OPEN") == 0) {
25  this->lock_->open();
26  } else {
27  ESP_LOGW(TAG, "'%s': Received unknown status payload: %s", this->friendly_name().c_str(), payload.c_str());
28  this->status_momentary_warning("state", 5000);
29  }
30  });
31  this->lock_->add_on_state_callback([this]() { this->defer("send", [this]() { this->publish_state(); }); });
32 }
34  ESP_LOGCONFIG(TAG, "MQTT Lock '%s': ", this->lock_->get_name().c_str());
35  LOG_MQTT_COMPONENT(true, true);
36 }
37 
38 std::string MQTTLockComponent::component_type() const { return "lock"; }
39 const EntityBase *MQTTLockComponent::get_entity() const { return this->lock_; }
41  if (this->lock_->traits.get_assumed_state())
42  root[MQTT_OPTIMISTIC] = true;
43  if (this->lock_->traits.get_supports_open())
44  root[MQTT_PAYLOAD_OPEN] = "OPEN";
45 }
47 
49  std::string payload = lock_state_to_string(this->lock_->state);
50  return this->publish(this->get_state_topic_(), payload);
51 }
52 
53 } // namespace mqtt
54 } // namespace esphome
55 
56 #endif
57 #endif // USE_MQTT
constexpr const char *const MQTT_PAYLOAD_OPEN
Definition: mqtt_const.h:152
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
void status_momentary_warning(const std::string &name, uint32_t length=5000)
Definition: component.cpp:178
std::string component_type() const override
"lock" component type.
Definition: mqtt_lock.cpp:38
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition: component.cpp:130
constexpr const char *const MQTT_OPTIMISTIC
Definition: mqtt_const.h:124
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
void add_on_state_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition: lock.cpp:58
LockTraits traits
Definition: lock.h:124
void lock()
Turn this lock on.
Definition: lock.cpp:30
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
Simple Helper struct used for Home Assistant MQTT send_discovery().
const EntityBase * get_entity() const override
Definition: mqtt_lock.cpp:39
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition: mqtt_lock.cpp:40
bool get_assumed_state() const
Definition: lock.h:44
MQTTLockComponent(lock::Lock *a_lock)
Definition: mqtt_lock.cpp:16
constexpr const char * c_str() const
Definition: string_ref.h:68
void open()
Open (unlatch) this lock.
Definition: lock.cpp:40
bool send_initial_state() override
Definition: mqtt_lock.cpp:46
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void unlock()
Turn this lock off.
Definition: lock.cpp:35
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
bool get_supports_open() const
Definition: lock.h:40
const StringRef & get_name() const
Definition: entity_base.cpp:10
Base class for all locks.
Definition: lock.h:103