ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
mqtt_time.cpp
Go to the documentation of this file.
1#include "mqtt_time.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_TIME
10
11namespace esphome::mqtt {
12
13ESPHOME_LOG_TAG(TAG, "mqtt.datetime.time");
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->time_->make_call();
22 if (root[ESPHOME_F("hour")].is<uint8_t>()) {
23 call.set_hour(root[ESPHOME_F("hour")]);
24 }
25 if (root[ESPHOME_F("minute")].is<uint8_t>()) {
26 call.set_minute(root[ESPHOME_F("minute")]);
27 }
28 if (root[ESPHOME_F("second")].is<uint8_t>()) {
29 call.set_second(root[ESPHOME_F("second")]);
30 }
31 call.perform();
32 });
34 [this]() { this->publish_state(this->time_->hour, this->time_->minute, this->time_->second); });
35}
36
37void MQTTTimeComponent::dump_config() { LOG_MQTT_COMPONENT(true, true); }
38
40const EntityBase *MQTTTimeComponent::get_entity() const { return this->time_; }
41
43 // Nothing extra to add here
44}
46 if (this->time_->has_state()) {
47 return this->publish_state(this->time_->hour, this->time_->minute, this->time_->second);
48 } else {
49 return true;
50 }
51}
52bool MQTTTimeComponent::publish_state(uint8_t hour, uint8_t minute, uint8_t second) {
53 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
54 return this->publish_json(this->get_state_topic_to_(topic_buf), [hour, minute, second](JsonObject root) {
55 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
56 root[ESPHOME_F("hour")] = hour;
57 root[ESPHOME_F("minute")] = minute;
58 root[ESPHOME_F("second")] = second;
59 });
60}
61
62} // namespace esphome::mqtt
63
64#endif // USE_DATETIME_TIME
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).
MQTTTimeComponent(datetime::TimeEntity *time)
Construct this MQTTTimeComponent instance with the provided friendly_name and time.
Definition mqtt_time.cpp:17
datetime::TimeEntity * time_
Definition mqtt_time.h:37
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
void setup() override
Override setup.
Definition mqtt_time.cpp:19
bool publish_state(uint8_t hour, uint8_t minute, uint8_t second)
Definition mqtt_time.cpp:52
uint8_t second
uint8_t minute
uint8_t hour
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().