ESPHome  2024.9.2
mqtt_update.cpp
Go to the documentation of this file.
1 #include "mqtt_update.h"
2 #include "esphome/core/log.h"
3 
4 #include "mqtt_const.h"
5 
6 #ifdef USE_MQTT
7 #ifdef USE_UPDATE
8 
9 namespace esphome {
10 namespace mqtt {
11 
12 static const char *const TAG = "mqtt.update";
13 
14 using namespace esphome::update;
15 
17 
19  this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
20  if (payload == "INSTALL") {
21  this->update_->perform();
22  } else {
23  ESP_LOGW(TAG, "'%s': Received unknown update payload: %s", this->friendly_name().c_str(), payload.c_str());
24  this->status_momentary_warning("state", 5000);
25  }
26  });
27 
28  this->update_->add_on_state_callback([this]() { this->defer("send", [this]() { this->publish_state(); }); });
29 }
30 
32  return this->publish_json(this->get_state_topic_(), [this](JsonObject root) {
33  root["installed_version"] = this->update_->update_info.current_version;
34  root["latest_version"] = this->update_->update_info.latest_version;
35  root["title"] = this->update_->update_info.title;
36  if (!this->update_->update_info.summary.empty())
37  root["release_summary"] = this->update_->update_info.summary;
38  if (!this->update_->update_info.release_url.empty())
39  root["release_url"] = this->update_->update_info.release_url;
40  });
41 }
42 
44  root["schema"] = "json";
45  root[MQTT_PAYLOAD_INSTALL] = "INSTALL";
46 }
47 
49 
51  ESP_LOGCONFIG(TAG, "MQTT Update '%s': ", this->update_->get_name().c_str());
52  LOG_MQTT_COMPONENT(true, true);
53 }
54 
55 std::string MQTTUpdateComponent::component_type() const { return "update"; }
56 const EntityBase *MQTTUpdateComponent::get_entity() const { return this->update_; }
57 
58 } // namespace mqtt
59 } // namespace esphome
60 
61 #endif // USE_UPDATE
62 #endif // USE_MQTT
std::string component_type() const override
"update" component type.
Definition: mqtt_update.cpp:55
MQTTUpdateComponent(update::UpdateEntity *update)
Definition: mqtt_update.cpp:16
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition: mqtt_update.cpp:43
void status_momentary_warning(const std::string &name, uint32_t length=5000)
Definition: component.cpp:178
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition: component.cpp:130
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
constexpr const char *const MQTT_PAYLOAD_INSTALL
Definition: mqtt_const.h:141
const EntityBase * get_entity() const override
Definition: mqtt_update.cpp:56
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
const UpdateInfo & update_info
Definition: update_entity.h:39
Simple Helper struct used for Home Assistant MQTT send_discovery().
void add_on_state_callback(std::function< void()> &&callback)
Definition: update_entity.h:42
constexpr const char * c_str() const
Definition: string_ref.h:68
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
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.
update::UpdateEntity * update_
Definition: mqtt_update.h:34
const StringRef & get_name() const
Definition: entity_base.cpp:10