[version] Skip the hide hash and hide timestamp setters when they match the defaults (#20067)

This commit is contained in:
J. Nick Koston
2026-10-05 12:13:01 +13:00
committed by GitHub
parent e708c85100
commit e09cab94d4
5 changed files with 40 additions and 4 deletions
+5 -2
View File
@@ -35,5 +35,8 @@ CONFIG_SCHEMA = (
async def to_code(config: ConfigType) -> None:
var = await text_sensor.new_text_sensor(config)
await cg.register_component(var, config)
cg.add(var.set_hide_hash(config[CONF_HIDE_HASH]))
cg.add(var.set_hide_timestamp(config[CONF_HIDE_TIMESTAMP]))
# Both flags default to false in C++, so only true needs a setter.
if config[CONF_HIDE_HASH]:
cg.add(var.set_hide_hash(True))
if config[CONF_HIDE_TIMESTAMP]:
cg.add(var.set_hide_timestamp(True))
@@ -15,8 +15,8 @@ class VersionTextSensor final : public text_sensor::TextSensor, public Component
void dump_config() override;
protected:
bool hide_hash_{false};
bool hide_timestamp_{false};
bool hide_hash_{false}; // Must match the setter check in text_sensor.py
bool hide_timestamp_{false}; // Must match the setter check in text_sensor.py
};
} // namespace esphome::version
@@ -0,0 +1,16 @@
---
esphome:
name: test
esp8266:
board: d1_mini
text_sensor:
- platform: version
id: default_version
name: Default Version
- platform: version
id: hidden_version
name: Hidden Version
hide_hash: true
hide_timestamp: true
@@ -0,0 +1,17 @@
"""Tests for the version text sensor codegen."""
from collections.abc import Callable
from pathlib import Path
def test_default_hide_flags_are_not_emitted(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
"""Both flags are false in C++, so only true reaches the setters."""
main_cpp = generate_main(component_config_path("version.yaml"))
assert "default_version->set_hide_hash(" not in main_cpp
assert "default_version->set_hide_timestamp(" not in main_cpp
assert "hidden_version->set_hide_hash(true);" in main_cpp
assert "hidden_version->set_hide_timestamp(true);" in main_cpp