ESPHome 2026.1.4
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
13static const char *const TAG = "mqtt.datetime.time";
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->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
38 ESP_LOGCONFIG(TAG, "MQTT Time '%s':", this->time_->get_name().c_str());
39 LOG_MQTT_COMPONENT(true, true)
40}
41
43const EntityBase *MQTTTimeComponent::get_entity() const { return this->time_; }
44
46 // Nothing extra to add here
47}
49 if (this->time_->has_state()) {
50 return this->publish_state(this->time_->hour, this->time_->minute, this->time_->second);
51 } else {
52 return true;
53 }
54}
55bool MQTTTimeComponent::publish_state(uint8_t hour, uint8_t minute, uint8_t second) {
56 return this->publish_json(this->get_state_topic_(), [hour, minute, second](JsonObject root) {
57 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
58 root[ESPHOME_F("hour")] = hour;
59 root[ESPHOME_F("minute")] = minute;
60 root[ESPHOME_F("second")] = second;
61 });
62}
63
64} // namespace esphome::mqtt
65
66#endif // USE_DATETIME_TIME
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.
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:55
uint8_t second
uint8_t minute
uint8_t hour
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
Simple Helper struct used for Home Assistant MQTT send_discovery().