ESPHome  2022.11.3
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 }
45 
47  std::string payload = lock_state_to_string(this->lock_->state);
48  return this->publish(this->get_state_topic_(), payload);
49 }
50 
51 } // namespace mqtt
52 } // namespace esphome
53 
54 #endif
55 #endif // USE_MQTT
LockState state
The current reported state of the lock.
Definition: lock.h:123
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:151
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:124
const std::string & get_name() const
Definition: entity_base.cpp:11
constexpr const char *const MQTT_OPTIMISTIC
Definition: mqtt_const.h:114
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:59
LockTraits traits
Definition: lock.h:125
void lock()
Turn this lock on.
Definition: lock.cpp:31
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
void open()
Open (unlatch) this lock.
Definition: lock.cpp:41
bool send_initial_state() override
Definition: mqtt_lock.cpp:44
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
Definition: a4988.cpp:4
void unlock()
Turn this lock off.
Definition: lock.cpp:36
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.
Base class for all locks.
Definition: lock.h:103