ESPHome  2024.12.2
mqtt_datetime.cpp
Go to the documentation of this file.
1 #include "mqtt_datetime.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_DATETIME
10 
11 namespace esphome {
12 namespace mqtt {
13 
14 static const char *const TAG = "mqtt.datetime.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->datetime_->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  if (root.containsKey("hour")) {
33  call.set_hour(root["hour"]);
34  }
35  if (root.containsKey("minute")) {
36  call.set_minute(root["minute"]);
37  }
38  if (root.containsKey("second")) {
39  call.set_second(root["second"]);
40  }
41  call.perform();
42  });
43  this->datetime_->add_on_state_callback([this]() {
44  this->publish_state(this->datetime_->year, this->datetime_->month, this->datetime_->day, this->datetime_->hour,
45  this->datetime_->minute, this->datetime_->second);
46  });
47 }
48 
50  ESP_LOGCONFIG(TAG, "MQTT DateTime '%s':", this->datetime_->get_name().c_str());
51  LOG_MQTT_COMPONENT(true, true)
52 }
53 
54 std::string MQTTDateTimeComponent::component_type() const { return "datetime"; }
55 const EntityBase *MQTTDateTimeComponent::get_entity() const { return this->datetime_; }
56 
58  // Nothing extra to add here
59 }
61  if (this->datetime_->has_state()) {
62  return this->publish_state(this->datetime_->year, this->datetime_->month, this->datetime_->day,
63  this->datetime_->hour, this->datetime_->minute, this->datetime_->second);
64  } else {
65  return true;
66  }
67 }
68 bool MQTTDateTimeComponent::publish_state(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute,
69  uint8_t second) {
70  return this->publish_json(this->get_state_topic_(), [year, month, day, hour, minute, second](JsonObject root) {
71  root["year"] = year;
72  root["month"] = month;
73  root["day"] = day;
74  root["hour"] = hour;
75  root["minute"] = minute;
76  root["second"] = second;
77  });
78 }
79 
80 } // namespace mqtt
81 } // namespace esphome
82 
83 #endif // USE_DATETIME_DATETIME
84 #endif // USE_MQTT
uint16_t year
Definition: date_entity.h:122
bool publish_state(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
void setup() override
Override setup.
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.
uint8_t minute
const EntityBase * get_entity() const override
uint8_t day
Definition: date_entity.h:124
MQTTDateTimeComponent(datetime::DateTimeEntity *datetime)
Construct this MQTTDateTimeComponent instance with the provided friendly_name and time...
uint8_t hour
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().
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
uint8_t second
std::string component_type() const override
datetime::DateTimeEntity * datetime_
Definition: mqtt_datetime.h:38
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 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.
const StringRef & get_name() const
Definition: entity_base.cpp:10
uint8_t month
Definition: date_entity.h:123