ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
mqtt_lock.cpp
Go to the documentation of this file.
1#include "mqtt_lock.h"
2#include "esphome/core/log.h"
4
5#include "mqtt_const.h"
6
7#ifdef USE_MQTT
8#ifdef USE_LOCK
9
10namespace esphome::mqtt {
11
12static const char *const TAG = "mqtt.lock";
13
14using namespace esphome::lock;
15
17
19 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
20 if (ESPHOME_strcasecmp_P(payload.c_str(), ESPHOME_PSTR("LOCK")) == 0) {
21 this->lock_->lock();
22 } else if (ESPHOME_strcasecmp_P(payload.c_str(), ESPHOME_PSTR("UNLOCK")) == 0) {
23 this->lock_->unlock();
24 } else if (ESPHOME_strcasecmp_P(payload.c_str(), ESPHOME_PSTR("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
39const EntityBase *MQTTLockComponent::get_entity() const { return this->lock_; }
41 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
42 if (this->lock_->traits.get_assumed_state()) {
43 root[MQTT_OPTIMISTIC] = true;
44 }
45 if (this->lock_->traits.get_supports_open())
46 root[MQTT_PAYLOAD_OPEN] = "OPEN";
47}
49
51 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
52#ifdef USE_STORE_LOG_STR_IN_FLASH
53 char buf[LOCK_STATE_STR_SIZE];
54 strncpy_P(buf, (PGM_P) lock_state_to_string(this->lock_->state), sizeof(buf) - 1);
55 buf[sizeof(buf) - 1] = '\0';
56 return this->publish(this->get_state_topic_to_(topic_buf), buf);
57#else
58 return this->publish(this->get_state_topic_to_(topic_buf), LOG_STR_ARG(lock_state_to_string(this->lock_->state)));
59#endif
60}
61
62} // namespace esphome::mqtt
63
64#endif
65#endif // USE_MQTT
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition component.h:493
void status_momentary_warning(const char *name, uint32_t length=5000)
Set warning status flag and automatically clear it after a timeout.
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:73
Base class for all locks.
Definition lock.h:110
void lock()
Turn this lock on.
Definition lock.cpp:28
LockTraits traits
Definition lock.h:131
void add_on_state_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition lock.cpp:51
LockState state
The current reported state of the lock.
Definition lock.h:129
void unlock()
Turn this lock off.
Definition lock.cpp:29
void open()
Open (unlatch) this lock.
Definition lock.cpp:30
bool get_assumed_state() const
Definition lock.h:44
bool get_supports_open() const
Definition lock.h:40
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
StringRef get_state_topic_to_(std::span< char, MQTT_DEFAULT_TOPIC_MAX_LEN > buf) const
Get the MQTT state topic into a buffer (no heap allocation for non-lambda custom topics).
const StringRef & friendly_name_() const
Get the friendly name of this MQTT component.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands (allocates std::string).
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
MQTTLockComponent(lock::Lock *a_lock)
Definition mqtt_lock.cpp:16
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
const LogString * lock_state_to_string(LockState state)
Definition lock.cpp:15
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
Simple Helper struct used for Home Assistant MQTT send_discovery().