mirror of
https://github.com/esphome/esphome.git
synced 2025-10-29 22:24:26 +00:00
Add support for time entities (#6399)
* Add time entities * Add tests * Add myself to datetime codeowners * Fix publishing times with 0 values * Log performing TimeCall * Implement `on_time` trigger * Rename var * Fix initial value for time * Add arg name for clarity * Remove useless checks
This commit is contained in:
@@ -113,7 +113,8 @@ MQTTSensorComponent = mqtt_ns.class_("MQTTSensorComponent", MQTTComponent)
|
||||
MQTTSwitchComponent = mqtt_ns.class_("MQTTSwitchComponent", MQTTComponent)
|
||||
MQTTTextSensor = mqtt_ns.class_("MQTTTextSensor", MQTTComponent)
|
||||
MQTTNumberComponent = mqtt_ns.class_("MQTTNumberComponent", MQTTComponent)
|
||||
MQTTDatetimeComponent = mqtt_ns.class_("MQTTDatetimeComponent", MQTTComponent)
|
||||
MQTTDateComponent = mqtt_ns.class_("MQTTDateComponent", MQTTComponent)
|
||||
MQTTTimeComponent = mqtt_ns.class_("MQTTTimeComponent", MQTTComponent)
|
||||
MQTTTextComponent = mqtt_ns.class_("MQTTTextComponent", MQTTComponent)
|
||||
MQTTSelectComponent = mqtt_ns.class_("MQTTSelectComponent", MQTTComponent)
|
||||
MQTTButtonComponent = mqtt_ns.class_("MQTTButtonComponent", MQTTComponent)
|
||||
|
||||
@@ -13,9 +13,9 @@ namespace mqtt {
|
||||
|
||||
class MQTTDateComponent : public mqtt::MQTTComponent {
|
||||
public:
|
||||
/** Construct this MQTTDatetimeComponent instance with the provided friendly_name and datetime
|
||||
/** Construct this MQTTDateComponent instance with the provided friendly_name and date
|
||||
*
|
||||
* @param datetime The datetime component.
|
||||
* @param date The date component.
|
||||
*/
|
||||
explicit MQTTDateComponent(datetime::DateEntity *date);
|
||||
|
||||
|
||||
68
esphome/components/mqtt/mqtt_time.cpp
Normal file
68
esphome/components/mqtt/mqtt_time.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "mqtt_time.h"
|
||||
|
||||
#include <utility>
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#include "mqtt_const.h"
|
||||
|
||||
#ifdef USE_MQTT
|
||||
#ifdef USE_DATETIME_TIME
|
||||
|
||||
namespace esphome {
|
||||
namespace mqtt {
|
||||
|
||||
static const char *const TAG = "mqtt.datetime.time";
|
||||
|
||||
using namespace esphome::datetime;
|
||||
|
||||
MQTTTimeComponent::MQTTTimeComponent(TimeEntity *time) : time_(time) {}
|
||||
|
||||
void MQTTTimeComponent::setup() {
|
||||
this->subscribe_json(this->get_command_topic_(), [this](const std::string &topic, JsonObject root) {
|
||||
auto call = this->time_->make_call();
|
||||
if (root.containsKey("hour")) {
|
||||
call.set_hour(root["hour"]);
|
||||
}
|
||||
if (root.containsKey("minute")) {
|
||||
call.set_minute(root["minute"]);
|
||||
}
|
||||
if (root.containsKey("second")) {
|
||||
call.set_second(root["second"]);
|
||||
}
|
||||
call.perform();
|
||||
});
|
||||
this->time_->add_on_state_callback(
|
||||
[this]() { this->publish_state(this->time_->hour, this->time_->minute, this->time_->second); });
|
||||
}
|
||||
|
||||
void MQTTTimeComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "MQTT Time '%s':", this->time_->get_name().c_str());
|
||||
LOG_MQTT_COMPONENT(true, true)
|
||||
}
|
||||
|
||||
std::string MQTTTimeComponent::component_type() const { return "time"; }
|
||||
const EntityBase *MQTTTimeComponent::get_entity() const { return this->time_; }
|
||||
|
||||
void MQTTTimeComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) {
|
||||
// Nothing extra to add here
|
||||
}
|
||||
bool MQTTTimeComponent::send_initial_state() {
|
||||
if (this->time_->has_state()) {
|
||||
return this->publish_state(this->time_->hour, this->time_->minute, this->time_->second);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
bool MQTTTimeComponent::publish_state(uint8_t hour, uint8_t minute, uint8_t second) {
|
||||
return this->publish_json(this->get_state_topic_(), [hour, minute, second](JsonObject root) {
|
||||
root["hour"] = hour;
|
||||
root["minute"] = minute;
|
||||
root["second"] = second;
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace mqtt
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_DATETIME_TIME
|
||||
#endif // USE_MQTT
|
||||
45
esphome/components/mqtt/mqtt_time.h
Normal file
45
esphome/components/mqtt/mqtt_time.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#ifdef USE_MQTT
|
||||
#ifdef USE_DATETIME_TIME
|
||||
|
||||
#include "esphome/components/datetime/time_entity.h"
|
||||
#include "mqtt_component.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mqtt {
|
||||
|
||||
class MQTTTimeComponent : public mqtt::MQTTComponent {
|
||||
public:
|
||||
/** Construct this MQTTTimeComponent instance with the provided friendly_name and time
|
||||
*
|
||||
* @param time The time entity.
|
||||
*/
|
||||
explicit MQTTTimeComponent(datetime::TimeEntity *time);
|
||||
|
||||
// ========== INTERNAL METHODS ==========
|
||||
// (In most use cases you won't need these)
|
||||
/// Override setup.
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
|
||||
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override;
|
||||
|
||||
bool send_initial_state() override;
|
||||
|
||||
bool publish_state(uint8_t hour, uint8_t minute, uint8_t second);
|
||||
|
||||
protected:
|
||||
std::string component_type() const override;
|
||||
const EntityBase *get_entity() const override;
|
||||
|
||||
datetime::TimeEntity *time_;
|
||||
};
|
||||
|
||||
} // namespace mqtt
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_DATETIME_DATE
|
||||
#endif // USE_MQTT
|
||||
Reference in New Issue
Block a user