Name the env boolean spelling tables like device-builder does

parse_enable_env's inline tuples become TRUTHY_ENV_STRINGS and
FALSY_ENV_STRINGS frozensets mirroring cv.boolean's spellings (enable
and disable included) plus the 1/0 env convention, matching
device-builder's TRUTHY_BOOL_STRINGS pattern.
This commit is contained in:
J. Nick Koston
2026-08-22 12:47:05 -05:00
parent 57cbadbe9f
commit e52703617e
2 changed files with 26 additions and 2 deletions
+6 -2
View File
@@ -11,6 +11,10 @@ from esphome.framework_helpers import strip_win_long_path_prefix, tool_version_r
_LOGGER = logging.getLogger(__name__)
# esphome cv.boolean's spelling tables plus the 1/0 env convention
TRUTHY_ENV_STRINGS = frozenset({"1", "true", "yes", "on", "enable"})
FALSY_ENV_STRINGS = frozenset({"0", "false", "no", "off", "disable"})
def _ccache_runs(ccache: str) -> bool:
"""Return True when the ``ccache`` found on PATH actually runs."""
@@ -31,9 +35,9 @@ def parse_enable_env(name: str) -> bool | None:
if raw is None:
return None
lowered = raw.strip().lower()
if lowered in ("1", "true", "yes", "on"):
if lowered in TRUTHY_ENV_STRINGS:
return True
if lowered in ("0", "false", "no", "off"):
if lowered in FALSY_ENV_STRINGS:
return False
_LOGGER.warning("Ignoring unrecognized %s=%r; use 1 or 0", name, raw)
return None
@@ -97,3 +97,23 @@ def test_resolve_unrecognized_value_warns_and_probes(
assert ccache.resolve_ccache_path() is None
mock_probe.assert_called_once()
assert "unrecognized ESPHOME_CCACHE_ENABLE" in caplog.text
@pytest.mark.parametrize(
("raw", "expected"),
[
("1", True),
("enable", True),
("ON", True),
("0", False),
("disable", False),
("Off", False),
("maybe", None),
],
)
def test_parse_enable_env_spelling_tables(
monkeypatch: pytest.MonkeyPatch, raw: str, expected: bool | None
) -> None:
"""cv.boolean's spelling tables plus the 1/0 env convention."""
monkeypatch.setenv("ESPHOME_CCACHE_ENABLE", raw)
assert ccache.parse_enable_env("ESPHOME_CCACHE_ENABLE") is expected