[time] Use register_apply_condition for time.has_time (#19560)

This commit is contained in:
J. Nick Koston
2026-09-24 08:54:03 -04:00
committed by GitHub
parent 0ceefd5f91
commit 1671bb6dd3
3 changed files with 11 additions and 17 deletions
+2 -8
View File
@@ -6,7 +6,6 @@ import logging
import tzlocal
from esphome import automation
from esphome.automation import Condition
import esphome.codegen as cg
from esphome.components.zephyr import zephyr_add_prj_conf
from esphome.config_helpers import filter_source_files_from_defines
@@ -18,7 +17,6 @@ from esphome.const import (
CONF_DAYS_OF_WEEK,
CONF_HOUR,
CONF_HOURS,
CONF_ID,
CONF_MINUTE,
CONF_MINUTES,
CONF_MONTHS,
@@ -49,7 +47,6 @@ time_ns = cg.esphome_ns.namespace("time")
RealTimeClock = time_ns.class_("RealTimeClock", cg.PollingComponent)
CronTrigger = time_ns.class_("CronTrigger", automation.Trigger.template(), cg.Component)
SyncTrigger = time_ns.class_("SyncTrigger", automation.Trigger.template(), cg.Component)
TimeHasTimeCondition = time_ns.class_("TimeHasTimeCondition", Condition)
# C++ types for pre-parsed timezone struct generation
DSTRuleType_cpp = time_ns.enum("DSTRuleType", is_class=True)
@@ -469,18 +466,15 @@ async def to_code(config):
cg.add_global(time_ns.using)
@automation.register_condition(
automation.register_apply_condition(
"time.has_time",
TimeHasTimeCondition,
cv.Schema(
{
cv.GenerateID(): cv.use_id(RealTimeClock),
}
),
"now().is_valid()",
)
async def time_has_time_to_code(config, condition_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(condition_id, template_arg, paren)
# posix_tz.cpp is fully #ifdef'd on USE_TIME_TIMEZONE, set only when a
@@ -44,13 +44,4 @@ class RealTimeClock : public PollingComponent {
LazyCallbackManager<void()> time_sync_callback_;
};
template<typename... Ts> class TimeHasTimeCondition final : public Condition<Ts...> {
public:
TimeHasTimeCondition(RealTimeClock *parent) : parent_(parent) {}
bool check(const Ts &...x) override { return this->parent_->now().is_valid(); }
protected:
RealTimeClock *parent_;
};
} // namespace esphome::time
+9
View File
@@ -12,3 +12,12 @@ time:
- logger.log: "CronTrigger fired (every 10 seconds)"
- platform: sntp
id: sntp_time
esphome:
on_boot:
- if:
condition:
time.has_time:
id: sntp_time
then:
- logger.log: "sntp_time has valid time"