1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-31 07:03:55 +00:00
This commit is contained in:
J. Nick Koston
2025-10-26 15:02:36 -07:00
parent cf37ffada0
commit 61796c672b
4 changed files with 54 additions and 58 deletions

View File

@@ -9,7 +9,8 @@ namespace template_ {
static const char *const TAG = "template.date";
void TemplateDate::setup() {
// Template instantiations
template<typename F> void TemplateDateBase<F>::setup() {
if (this->f_.has_value())
return;
@@ -36,21 +37,7 @@ void TemplateDate::setup() {
this->publish_state();
}
void TemplateDate::update() {
if (!this->f_.has_value())
return;
auto val = (*this->f_)();
if (!val.has_value())
return;
this->year_ = val->year;
this->month_ = val->month;
this->day_ = val->day_of_month;
this->publish_state();
}
void TemplateDate::control(const datetime::DateCall &call) {
template<typename F> void TemplateDateBase<F>::control(const datetime::DateCall &call) {
bool has_year = call.get_year().has_value();
bool has_month = call.get_month().has_value();
bool has_day = call.get_day().has_value();
@@ -99,12 +86,15 @@ void TemplateDate::control(const datetime::DateCall &call) {
}
}
void TemplateDate::dump_config() {
template<typename F> void TemplateDateBase<F>::dump_config() {
LOG_DATETIME_DATE("", "Template Date", this);
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
LOG_UPDATE_INTERVAL(this);
}
template class TemplateDateBase<std::function<optional<ESPTime>()>>;
template class TemplateDateBase<optional<ESPTime> (*)()>;
} // namespace template_
} // namespace esphome

View File

@@ -9,7 +9,8 @@ namespace template_ {
static const char *const TAG = "template.datetime";
void TemplateDateTime::setup() {
// Template instantiations
template<typename F> void TemplateDateTimeBase<F>::setup() {
if (this->f_.has_value())
return;
@@ -39,24 +40,7 @@ void TemplateDateTime::setup() {
this->publish_state();
}
void TemplateDateTime::update() {
if (!this->f_.has_value())
return;
auto val = (*this->f_)();
if (!val.has_value())
return;
this->year_ = val->year;
this->month_ = val->month;
this->day_ = val->day_of_month;
this->hour_ = val->hour;
this->minute_ = val->minute;
this->second_ = val->second;
this->publish_state();
}
void TemplateDateTime::control(const datetime::DateTimeCall &call) {
template<typename F> void TemplateDateTimeBase<F>::control(const datetime::DateTimeCall &call) {
bool has_year = call.get_year().has_value();
bool has_month = call.get_month().has_value();
bool has_day = call.get_day().has_value();
@@ -138,12 +122,15 @@ void TemplateDateTime::control(const datetime::DateTimeCall &call) {
}
}
void TemplateDateTime::dump_config() {
template<typename F> void TemplateDateTimeBase<F>::dump_config() {
LOG_DATETIME_DATETIME("", "Template DateTime", this);
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
LOG_UPDATE_INTERVAL(this);
}
template class TemplateDateTimeBase<std::function<optional<ESPTime>()>>;
template class TemplateDateTimeBase<optional<ESPTime> (*)()>;
} // namespace template_
} // namespace esphome

View File

@@ -13,12 +13,26 @@
namespace esphome {
namespace template_ {
class TemplateDateTime : public datetime::DateTimeEntity, public PollingComponent {
template<typename F> class TemplateDateTimeBase : public datetime::DateTimeEntity, public PollingComponent {
public:
void set_template(std::function<optional<ESPTime>()> &&f) { this->f_ = f; }
void update() override {
if (!this->f_.has_value())
return;
auto val = (*this->f_)();
if (!val.has_value())
return;
this->year_ = val->year;
this->month_ = val->month;
this->day_ = val->day_of_month;
this->hour_ = val->hour;
this->minute_ = val->minute;
this->second_ = val->second;
this->publish_state();
}
void setup() override;
void update() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::HARDWARE; }
@@ -35,11 +49,26 @@ class TemplateDateTime : public datetime::DateTimeEntity, public PollingComponen
ESPTime initial_value_{};
bool restore_value_{false};
Trigger<ESPTime> *set_trigger_ = new Trigger<ESPTime>();
optional<std::function<optional<ESPTime>()>> f_;
optional<F> f_;
ESPPreferenceObject pref_;
};
class TemplateDateTime : public TemplateDateTimeBase<std::function<optional<ESPTime>()>> {
public:
void set_template(std::function<optional<ESPTime>()> &&f) { this->f_ = f; }
};
/** Optimized template datetime for stateless lambdas (no capture).
*
* Uses function pointers instead of std::function to reduce memory overhead.
* Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function) per lambda.
*/
class StatelessTemplateDateTime : public TemplateDateTimeBase<optional<ESPTime> (*)()> {
public:
explicit StatelessTemplateDateTime(optional<ESPTime> (*f)()) { this->f_ = f; }
};
} // namespace template_
} // namespace esphome

View File

@@ -9,7 +9,8 @@ namespace template_ {
static const char *const TAG = "template.time";
void TemplateTime::setup() {
// Template instantiations
template<typename F> void TemplateTimeBase<F>::setup() {
if (this->f_.has_value())
return;
@@ -36,21 +37,7 @@ void TemplateTime::setup() {
this->publish_state();
}
void TemplateTime::update() {
if (!this->f_.has_value())
return;
auto val = (*this->f_)();
if (!val.has_value())
return;
this->hour_ = val->hour;
this->minute_ = val->minute;
this->second_ = val->second;
this->publish_state();
}
void TemplateTime::control(const datetime::TimeCall &call) {
template<typename F> void TemplateTimeBase<F>::control(const datetime::TimeCall &call) {
bool has_hour = call.get_hour().has_value();
bool has_minute = call.get_minute().has_value();
bool has_second = call.get_second().has_value();
@@ -99,12 +86,15 @@ void TemplateTime::control(const datetime::TimeCall &call) {
}
}
void TemplateTime::dump_config() {
template<typename F> void TemplateTimeBase<F>::dump_config() {
LOG_DATETIME_TIME("", "Template Time", this);
ESP_LOGCONFIG(TAG, " Optimistic: %s", YESNO(this->optimistic_));
LOG_UPDATE_INTERVAL(this);
}
template class TemplateTimeBase<std::function<optional<ESPTime>()>>;
template class TemplateTimeBase<optional<ESPTime> (*)()>;
} // namespace template_
} // namespace esphome