[time] Skip automation.cpp when no on_time or on_time_sync automation is configured (#18750)

This commit is contained in:
J. Nick Koston
2026-08-25 19:50:43 +12:00
committed by GitHub
parent 3f615f87cf
commit bc8d0840eb
8 changed files with 106 additions and 4 deletions
+13 -4
View File
@@ -426,7 +426,12 @@ async def setup_time_core_(time_var, config):
raise EsphomeError(f"Invalid timezone: {timezone}") from e
_emit_parsed_timezone_fields(parsed)
for conf in config.get(CONF_ON_TIME, []):
on_time = config.get(CONF_ON_TIME, [])
on_time_sync = config.get(CONF_ON_TIME_SYNC, [])
if on_time or on_time_sync:
cg.add_define("USE_TIME_TRIGGERS")
for conf in on_time:
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], time_var)
seconds = conf.get(CONF_SECONDS, list(range(61)))
@@ -445,7 +450,7 @@ async def setup_time_core_(time_var, config):
await cg.register_component(trigger, conf)
await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_TIME_SYNC, []):
for conf in on_time_sync:
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], time_var)
await cg.register_component(trigger, conf)
@@ -479,7 +484,11 @@ async def time_has_time_to_code(config, condition_id, template_arg, args):
# posix_tz.cpp is fully #ifdef'd on USE_TIME_TIMEZONE, set only when a
# timezone is configured or detected.
# timezone is configured or detected; automation.cpp holds the on_time and
# on_time_sync triggers and is #ifdef'd on USE_TIME_TRIGGERS.
FILTER_SOURCE_FILES = filter_source_files_from_defines(
{"posix_tz.cpp": "USE_TIME_TIMEZONE"}
{
"posix_tz.cpp": "USE_TIME_TIMEZONE",
"automation.cpp": "USE_TIME_TRIGGERS",
}
)
+3
View File
@@ -1,4 +1,5 @@
#include "automation.h"
#ifdef USE_TIME_TRIGGERS
#include "esphome/core/log.h"
@@ -98,3 +99,5 @@ SyncTrigger::SyncTrigger(RealTimeClock *rtc) : rtc_(rtc) {
}
} // namespace esphome::time
#endif // USE_TIME_TRIGGERS
+5
View File
@@ -1,5 +1,8 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_TIME_TRIGGERS
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/time.h"
@@ -49,3 +52,5 @@ class SyncTrigger final : public Trigger<>, public Component {
RealTimeClock *rtc_;
};
} // namespace esphome::time
#endif // USE_TIME_TRIGGERS
+1
View File
@@ -188,6 +188,7 @@
#define USE_TEXT_SENSOR
#define USE_TEXT_SENSOR_FILTER
#define USE_TIME
#define USE_TIME_TRIGGERS
#define USE_TOUCHSCREEN
#define USE_UART_DEBUGGER
#define USE_UART_WAKE_LOOP_ON_RX
@@ -0,0 +1,15 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
wifi:
ssid: "test_ssid"
password: "test_password"
time:
- platform: sntp
id: sntp_time
@@ -0,0 +1,21 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
wifi:
ssid: "test_ssid"
password: "test_password"
logger:
time:
- platform: sntp
id: sntp_time
on_time:
- seconds: 0
then:
- logger.log: tick
@@ -0,0 +1,20 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
wifi:
ssid: "test_ssid"
password: "test_password"
logger:
time:
- platform: sntp
id: sntp_time
on_time_sync:
then:
- logger.log: synced
@@ -0,0 +1,28 @@
"""automation.cpp (CronTrigger and SyncTrigger) is only compiled when an
on_time or on_time_sync automation exists, so the define must follow them."""
from collections.abc import Callable
from pathlib import Path
import pytest
from esphome.core import CORE
@pytest.mark.parametrize(
("fixture", "emits"),
[
("no_triggers.yaml", False),
("on_time.yaml", True),
("on_time_sync.yaml", True),
],
)
def test_triggers_define_follows_automations(
fixture: str,
emits: bool,
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
generate_main(component_config_path(fixture))
defines = {define.name for define in CORE.defines}
assert ("USE_TIME_TRIGGERS" in defines) is emits