From f1a23a02c24203c306c3a7c9f970abe71ee5e1cf Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 9 Oct 2026 15:39:06 -1000 Subject: [PATCH] [time] Build on_time_sync with build_callback_automation (#20441) --- esphome/components/time/__init__.py | 23 +++++++------------ esphome/components/time/automation.cpp | 4 ---- esphome/components/time/automation.h | 8 ------- tests/component_tests/time/test_triggers.py | 6 ++--- .../time/test-sync.esp8266-ard.yaml | 8 +++++++ 5 files changed, 19 insertions(+), 30 deletions(-) create mode 100644 tests/components/time/test-sync.esp8266-ard.yaml diff --git a/esphome/components/time/__init__.py b/esphome/components/time/__init__.py index cedb9b3c9b..a017109e7d 100644 --- a/esphome/components/time/__init__.py +++ b/esphome/components/time/__init__.py @@ -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", diff --git a/esphome/components/time/automation.cpp b/esphome/components/time/automation.cpp index 26956703e7..1c3d462566 100644 --- a/esphome/components/time/automation.cpp +++ b/esphome/components/time/automation.cpp @@ -94,10 +94,6 @@ void CronTrigger::add_days_of_week(const std::vector &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 diff --git a/esphome/components/time/automation.h b/esphome/components/time/automation.h index 5f980690a4..5e70000ac3 100644 --- a/esphome/components/time/automation.h +++ b/esphome/components/time/automation.h @@ -43,14 +43,6 @@ class CronTrigger final : public Trigger<>, public Component { RealTimeClock *rtc_; optional last_check_; }; - -class SyncTrigger final : public Trigger<>, public Component { - public: - explicit SyncTrigger(RealTimeClock *rtc); - - protected: - RealTimeClock *rtc_; -}; } // namespace esphome::time #endif // USE_TIME_TRIGGERS diff --git a/tests/component_tests/time/test_triggers.py b/tests/component_tests/time/test_triggers.py index 34fd5a3157..d6c3eed584 100644 --- a/tests/component_tests/time/test_triggers.py +++ b/tests/component_tests/time/test_triggers.py @@ -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( diff --git a/tests/components/time/test-sync.esp8266-ard.yaml b/tests/components/time/test-sync.esp8266-ard.yaml new file mode 100644 index 0000000000..6240fcd73e --- /dev/null +++ b/tests/components/time/test-sync.esp8266-ard.yaml @@ -0,0 +1,8 @@ +wifi: + ssid: MySSID + password: password1 + +time: + - platform: sntp + on_time_sync: + - logger.log: Time synchronized