[uptime] Skip the timestamp sensor source unless a timestamp sensor is configured (#18746)

This commit is contained in:
J. Nick Koston
2026-08-25 17:24:20 +12:00
committed by GitHub
parent 036e5cda7e
commit 791590c8a3
8 changed files with 74 additions and 5 deletions
+2 -1
View File
@@ -61,8 +61,9 @@ async def to_code(config: ConfigType) -> None:
if time_id_config := config.get(CONF_TIME_ID):
time_id = await cg.get_variable(time_id_config)
cg.add(var.set_time(time_id))
cg.add_define("USE_UPTIME_TIMESTAMP")
FILTER_SOURCE_FILES = filter_source_files_from_defines(
{"uptime_timestamp_sensor.cpp": "USE_TIME"}
{"uptime_timestamp_sensor.cpp": "USE_UPTIME_TIMESTAMP"}
)
@@ -1,6 +1,6 @@
#include "uptime_timestamp_sensor.h"
#ifdef USE_TIME
#ifdef USE_UPTIME_TIMESTAMP
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
@@ -34,4 +34,4 @@ void UptimeTimestampSensor::dump_config() {
} // namespace esphome::uptime
#endif // USE_TIME
#endif // USE_UPTIME_TIMESTAMP
@@ -2,7 +2,7 @@
#include "esphome/core/defines.h"
#ifdef USE_TIME
#ifdef USE_UPTIME_TIMESTAMP
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/time/real_time_clock.h"
@@ -25,4 +25,4 @@ class UptimeTimestampSensor final : public sensor::Sensor, public Component {
} // namespace esphome::uptime
#endif // USE_TIME
#endif // USE_UPTIME_TIMESTAMP
+1
View File
@@ -192,6 +192,7 @@
#define USE_UART_DEBUGGER
#define USE_UART_WAKE_LOOP_ON_RX
#define USE_UPDATE
#define USE_UPTIME_TIMESTAMP
#define USE_VALVE
#define USE_WATER_HEATER
#define USE_WATER_HEATER_VISUAL_OVERRIDES
@@ -0,0 +1,20 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
wifi:
ssid: "test_ssid"
password: "test_password"
time:
- platform: sntp
id: sntp_time
sensor:
- platform: uptime
name: Uptime Seconds
type: seconds
@@ -0,0 +1,20 @@
esphome:
name: test
esp32:
board: esp32dev
framework:
type: esp-idf
wifi:
ssid: "test_ssid"
password: "test_password"
time:
- platform: sntp
id: sntp_time
sensor:
- platform: uptime
name: Uptime Timestamp
type: timestamp
@@ -0,0 +1,27 @@
"""The timestamp uptime sensor source is only compiled when that type is used,
so the define must follow the configured sensor type rather than time: alone."""
from collections.abc import Callable
from pathlib import Path
import pytest
from esphome.core import CORE
@pytest.mark.parametrize(
("fixture", "emits"),
[
("seconds.yaml", False),
("timestamp.yaml", True),
],
)
def test_timestamp_define_follows_sensor_type(
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_UPTIME_TIMESTAMP" in defines) is emits