ESPHome  2024.12.2
mqtt_date.cpp
Go to the documentation of this file.
1 #include "mqtt_date.h"
2 
3 #include <utility>
4 #include "esphome/core/log.h"
5 
6 #include "mqtt_const.h"
7 
8 #ifdef USE_MQTT
9 #ifdef USE_DATETIME_DATE
10 
11 namespace esphome {
12 namespace mqtt {
13 
14 static const char *const TAG = "mqtt.datetime";
15 
16 using namespace esphome::datetime;
17 
19 
21  this->subscribe_json(this->get_command_topic_(), [this](const std::string &topic, JsonObject root) {
22  auto call = this->date_->make_call();
23  if (root.containsKey("year")) {
24  call.set_year(root["year"]);
25  }
26  if (root.containsKey("month")) {
27  call.set_month(root["month"]);
28  }
29  if (root.containsKey("day")) {
30  call.set_day(root["day"]);
31  }
32  call.perform();
33  });
35  [this]() { this->publish_state(this->date_->year, this->date_->month, this->date_->day); });
36 }
37 
39  ESP_LOGCONFIG(TAG, "MQTT Date '%s':", this->date_->get_name().c_str());
40  LOG_MQTT_COMPONENT(true, true)
41 }
42 
43 std::string MQTTDateComponent::component_type() const { return "date"; }
44 const EntityBase *MQTTDateComponent::get_entity() const { return this->date_; }
45 
47  // Nothing extra to add here
48 }
50  if (this->date_->has_state()) {
51  return this->publish_state(this->date_->year, this->date_->month, this->date_->day);
52  } else {
53  return true;
54  }
55 }
56 bool MQTTDateComponent::publish_state(uint16_t year, uint8_t month, uint8_t day) {
57  return this->publish_json(this->get_state_topic_(), [year, month, day](JsonObject root) {
58  root["year"] = year;
59  root["month"] = month;
60  root["day"] = day;
61  });
62 }
63 
64 } // namespace mqtt
65 } // namespace esphome
66 
67 #endif // USE_DATETIME_DATE
68 #endif // USE_MQTT
uint16_t year
Definition: date_entity.h:122
bool publish_state(uint16_t year, uint8_t month, uint8_t day)
Definition: mqtt_date.cpp:56
void setup() override
Override setup.
Definition: mqtt_date.cpp:20
datetime::DateEntity * date_
Definition: mqtt_date.h:38
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
void subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos=0)
Subscribe to a MQTT topic and automatically parse JSON payload.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition: mqtt_date.cpp:46
uint8_t day
Definition: date_entity.h:124
bool has_state() const
Return whether this Datetime has gotten a full state yet.
Definition: datetime_base.h:17
Simple Helper struct used for Home Assistant MQTT send_discovery().
const EntityBase * get_entity() const override
Definition: mqtt_date.cpp:44
bool send_initial_state() override
Definition: mqtt_date.cpp:49
void add_on_state_callback(std::function< void()> &&callback)
Definition: datetime_base.h:21
constexpr const char * c_str() const
Definition: string_ref.h:68
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::string component_type() const override
Definition: mqtt_date.cpp:43
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.
MQTTDateComponent(datetime::DateEntity *date)
Construct this MQTTDateComponent instance with the provided friendly_name and date.
Definition: mqtt_date.cpp:18
const StringRef & get_name() const
Definition: entity_base.cpp:10
uint8_t month
Definition: date_entity.h:123