ESPHome 2025.5.0
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 {
10namespace 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 (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
38std::string MQTTLockComponent::component_type() const { return "lock"; }
39const 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
void status_momentary_warning(const std::string &name, uint32_t length=5000)
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:68
Base class for all locks.
Definition lock.h:103
void lock()
Turn this lock on.
Definition lock.cpp:30
LockTraits traits
Definition lock.h:124
void add_on_state_callback(std::function< void()> &&callback)
Set callback for state changes.
Definition lock.cpp:58
LockState state
The current reported state of the lock.
Definition lock.h:122
void unlock()
Turn this lock off.
Definition lock.cpp:35
void open()
Open (unlatch) this lock.
Definition lock.cpp:40
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.
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
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.
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
Definition mqtt_lock.cpp:40
std::string component_type() const override
"lock" component type.
Definition mqtt_lock.cpp:38
const EntityBase * get_entity() const override
Definition mqtt_lock.cpp:39
const char * lock_state_to_string(LockState state)
Definition lock.cpp:9
constexpr const char *const MQTT_OPTIMISTIC
Definition mqtt_const.h:126
constexpr const char *const MQTT_PAYLOAD_OPEN
Definition mqtt_const.h:154
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Simple Helper struct used for Home Assistant MQTT send_discovery().