[time] Build on_time_sync with build_callback_automation (#20441)

This commit is contained in:
J. Nick Koston
2026-10-09 15:39:06 -10:00
committed by GitHub
parent 95dc9a749b
commit f1a23a02c2
5 changed files with 19 additions and 30 deletions
+8 -15
View File
@@ -46,7 +46,6 @@ DOMAIN = "time"
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)
# C++ types for pre-parsed timezone struct generation
DSTRuleType_cpp = time_ns.enum("DSTRuleType", is_class=True)
@@ -357,11 +356,7 @@ TIME_SCHEMA = cv.Schema(
},
validate_cron_keys,
),
cv.Optional(CONF_ON_TIME_SYNC): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SyncTrigger),
}
),
cv.Optional(CONF_ON_TIME_SYNC): automation.validate_automation({}),
}
).extend(
# ``visibility=ADVANCED`` flags the inherited ``update_interval``
@@ -424,8 +419,7 @@ async def setup_time_core_(time_var, config):
_emit_parsed_timezone_fields(parsed)
on_time = config.get(CONF_ON_TIME, [])
on_time_sync = config.get(CONF_ON_TIME_SYNC, [])
if on_time or on_time_sync:
if on_time:
cg.add_define("USE_TIME_TRIGGERS")
for conf in on_time:
@@ -447,11 +441,10 @@ async def setup_time_core_(time_var, config):
await cg.register_component(trigger, conf)
await automation.build_automation(trigger, [], conf)
for conf in on_time_sync:
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], time_var)
await cg.register_component(trigger, conf)
await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_TIME_SYNC, []):
await automation.build_callback_automation(
time_var, "add_on_time_sync_callback", [], conf
)
async def register_time(time_var, config):
@@ -478,8 +471,8 @@ automation.register_apply_condition(
# posix_tz.cpp is fully #ifdef'd on USE_TIME_TIMEZONE, set only when a
# timezone is configured or detected; automation.cpp holds the on_time and
# on_time_sync triggers and is #ifdef'd on USE_TIME_TRIGGERS.
# timezone is configured or detected; automation.cpp holds the on_time
# trigger and is #ifdef'd on USE_TIME_TRIGGERS.
FILTER_SOURCE_FILES = filter_source_files_from_defines(
{
"posix_tz.cpp": "USE_TIME_TIMEZONE",
-4
View File
@@ -94,10 +94,6 @@ void CronTrigger::add_days_of_week(const std::vector<uint8_t> &days_of_week) {
}
float CronTrigger::get_setup_priority() const { return setup_priority::HARDWARE; }
SyncTrigger::SyncTrigger(RealTimeClock *rtc) : rtc_(rtc) {
rtc->add_on_time_sync_callback([this]() { this->trigger(); });
}
} // namespace esphome::time
#endif // USE_TIME_TRIGGERS
-8
View File
@@ -43,14 +43,6 @@ class CronTrigger final : public Trigger<>, public Component {
RealTimeClock *rtc_;
optional<ESPTime> last_check_;
};
class SyncTrigger final : public Trigger<>, public Component {
public:
explicit SyncTrigger(RealTimeClock *rtc);
protected:
RealTimeClock *rtc_;
};
} // namespace esphome::time
#endif // USE_TIME_TRIGGERS
+3 -3
View File
@@ -1,5 +1,5 @@
"""automation.cpp (CronTrigger and SyncTrigger) is only compiled when an
on_time or on_time_sync automation exists, so the define must follow them."""
"""automation.cpp (CronTrigger) is only compiled when an on_time automation
exists, so the define must follow it; on_time_sync needs no class."""
from collections.abc import Callable
from pathlib import Path
@@ -14,7 +14,7 @@ from esphome.core import CORE
[
("no_triggers.yaml", False),
("on_time.yaml", True),
("on_time_sync.yaml", True),
("on_time_sync.yaml", False),
],
)
def test_triggers_define_follows_automations(
@@ -0,0 +1,8 @@
wifi:
ssid: MySSID
password: password1
time:
- platform: sntp
on_time_sync:
- logger.log: Time synchronized