ESPHome 2026.1.4
Loading...
Searching...
No Matches
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
11namespace esphome::mqtt {
12
13static const char *const TAG = "mqtt.datetime";
14
15using namespace esphome::datetime;
16
18
20 this->subscribe_json(this->get_command_topic_(), [this](const std::string &topic, JsonObject root) {
21 auto call = this->date_->make_call();
22 if (root[ESPHOME_F("year")].is<uint16_t>()) {
23 call.set_year(root[ESPHOME_F("year")]);
24 }
25 if (root[ESPHOME_F("month")].is<uint8_t>()) {
26 call.set_month(root[ESPHOME_F("month")]);
27 }
28 if (root[ESPHOME_F("day")].is<uint8_t>()) {
29 call.set_day(root[ESPHOME_F("day")]);
30 }
31 call.perform();
32 });
34 [this]() { this->publish_state(this->date_->year, this->date_->month, this->date_->day); });
35}
36
38 ESP_LOGCONFIG(TAG, "MQTT Date '%s':", this->date_->get_name().c_str());
39 LOG_MQTT_COMPONENT(true, true)
40}
41
43const EntityBase *MQTTDateComponent::get_entity() const { return this->date_; }
44
46 // Nothing extra to add here
47}
49 if (this->date_->has_state()) {
50 return this->publish_state(this->date_->year, this->date_->month, this->date_->day);
51 } else {
52 return true;
53 }
54}
55bool MQTTDateComponent::publish_state(uint16_t year, uint8_t month, uint8_t day) {
56 return this->publish_json(this->get_state_topic_(), [year, month, day](JsonObject root) {
57 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
58 root[ESPHOME_F("year")] = year;
59 root[ESPHOME_F("month")] = month;
60 root[ESPHOME_F("day")] = day;
61 });
62}
63
64} // namespace esphome::mqtt
65
66#endif // USE_DATETIME_DATE
67#endif // USE_MQTT
const StringRef & get_name() const
bool has_state() const
constexpr const char * c_str() const
Definition string_ref.h:73
void add_on_state_callback(std::function< void()> &&callback)
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.
bool publish_json(const std::string &topic, const json::json_build_t &f)
Construct and send a JSON MQTT message.
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:17
datetime::DateEntity * date_
Definition mqtt_date.h:37
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
bool publish_state(uint16_t year, uint8_t month, uint8_t day)
Definition mqtt_date.cpp:55
void setup() override
Override setup.
Definition mqtt_date.cpp:19
uint8_t month
Definition date_entity.h:1
uint16_t year
Definition date_entity.h:0
uint8_t day
Definition date_entity.h:2
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
Simple Helper struct used for Home Assistant MQTT send_discovery().