ESPHome 2026.11.0-dev
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
13ESPHOME_LOG_TAG(TAG, "mqtt.datetime");
14
15using namespace esphome::datetime;
16
18
20 this->subscribe_json(this->command_topic_source_(), [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
37void MQTTDateComponent::dump_config() { LOG_MQTT_COMPONENT(true, true); }
38
40const EntityBase *MQTTDateComponent::get_entity() const { return this->date_; }
41
43 // Nothing extra to add here
44}
46 if (this->date_->has_state()) {
47 return this->publish_state(this->date_->year, this->date_->month, this->date_->day);
48 } else {
49 return true;
50 }
51}
52bool MQTTDateComponent::publish_state(uint16_t year, uint8_t month, uint8_t day) {
53 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
54 return this->publish_json(this->get_state_topic_to_(topic_buf), [year, month, day](JsonObject root) {
55 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
56 root[ESPHOME_F("year")] = year;
57 root[ESPHOME_F("month")] = month;
58 root[ESPHOME_F("day")] = day;
59 });
60}
61
62} // namespace esphome::mqtt
63
64#endif // USE_DATETIME_DATE
65#endif // USE_MQTT
bool has_state() const
void add_on_state_callback(F &&callback)
MQTTTopicSource command_topic_source_() const
The command topic as a subscription source. A lambda topic is read once and copied,...
void subscribe_json(T &&topic, F &&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.
StringRef get_state_topic_to_(std::span< char, MQTT_DEFAULT_TOPIC_MAX_LEN > buf) const
Get the MQTT state topic into a buffer (no heap allocation for non-lambda custom topics).
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:52
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
ESPHOME_LOG_TAG(TAG, "alarm_control_panel")
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
Simple Helper struct used for Home Assistant MQTT send_discovery().