ESPHome 2026.1.4
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"
3
4#include "mqtt_const.h"
5
6#ifdef USE_MQTT
7#ifdef USE_LOCK
8
9namespace esphome::mqtt {
10
11static const char *const TAG = "mqtt.lock";
12
13using namespace esphome::lock;
14
16
18 this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
19 if (strcasecmp(payload.c_str(), "LOCK") == 0) {
20 this->lock_->lock();
21 } else if (strcasecmp(payload.c_str(), "UNLOCK") == 0) {
22 this->lock_->unlock();
23 } else if (strcasecmp(payload.c_str(), "OPEN") == 0) {
24 this->lock_->open();
25 } else {
26 ESP_LOGW(TAG, "'%s': Received unknown status payload: %s", this->friendly_name_().c_str(), payload.c_str());
27 this->status_momentary_warning("state", 5000);
28 }
29 });
30 this->lock_->add_on_state_callback([this]() { this->defer("send", [this]() { this->publish_state(); }); });
31}
33 ESP_LOGCONFIG(TAG, "MQTT Lock '%s': ", this->lock_->get_name().c_str());
34 LOG_MQTT_COMPONENT(true, true);
35}
36
38const EntityBase *MQTTLockComponent::get_entity() const { return this->lock_; }
40 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
41 if (this->lock_->traits.get_assumed_state()) {
42 root[MQTT_OPTIMISTIC] = true;
43 }
44 if (this->lock_->traits.get_supports_open())
45 root[MQTT_PAYLOAD_OPEN] = "OPEN";
46}
48
50#ifdef USE_STORE_LOG_STR_IN_FLASH
51 char buf[LOCK_STATE_STR_SIZE];
52 strncpy_P(buf, (PGM_P) lock_state_to_string(this->lock_->state), sizeof(buf) - 1);
53 buf[sizeof(buf) - 1] = '\0';
54 return this->publish(this->get_state_topic_(), buf);
55#else
56 return this->publish(this->get_state_topic_(), LOG_STR_ARG(lock_state_to_string(this->lock_->state)));
57#endif
58}
59
60} // namespace esphome::mqtt
61
62#endif
63#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:492
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:111
void lock()
Turn this lock on.
Definition lock.cpp:31
LockTraits traits
Definition lock.h:132
void add_on_state_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition lock.cpp:62
LockState state
The current reported state of the lock.
Definition lock.h:130
void unlock()
Turn this lock off.
Definition lock.cpp:36
void open()
Open (unlatch) this lock.
Definition lock.cpp:41
bool get_assumed_state() const
Definition lock.h:46
bool get_supports_open() const
Definition lock.h:42
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
std::string 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.
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:15
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
const LogString * lock_state_to_string(LockState state)
Definition lock.cpp:10
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
Simple Helper struct used for Home Assistant MQTT send_discovery().