From 054b133a9862335f176cd33538e35f26c5f3bae2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 22 Aug 2026 23:49:01 -0500 Subject: [PATCH] Fold the spellings into the main table, cover True defaults and case folding, correct the 1/0 note --- esphome/helpers.py | 6 +++--- tests/unit_tests/test_helpers.py | 27 ++++++++------------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/esphome/helpers.py b/esphome/helpers.py index 91413550aa..8916b9fe74 100644 --- a/esphome/helpers.py +++ b/esphome/helpers.py @@ -397,9 +397,9 @@ def sort_ip_addresses(address_list: list[str]) -> list[str]: def get_bool_env(var, default=False): """Read a boolean environment knob. - Accepts the same spellings as ``cv.boolean`` does in YAML, so the - environment speaks the config language's boolean dialect; anything - unrecognized falls through to ``bool(value)``. + Accepts the ``cv.boolean`` spellings plus ``1``/``0``, so environment + knobs speak the config language's boolean dialect; anything else falls + through to ``bool(value)``. """ value = os.getenv(var, default) if isinstance(value, str): diff --git a/tests/unit_tests/test_helpers.py b/tests/unit_tests/test_helpers.py index a7b911351d..d6e8cdc62d 100644 --- a/tests/unit_tests/test_helpers.py +++ b/tests/unit_tests/test_helpers.py @@ -171,6 +171,14 @@ def test_is_ip_address__valid(value): ("FOO", "fAlSe", True, False), ("FOO", "Yes", False, True), ("FOO", "123", False, True), + # cv.boolean's spellings; the True-default falsy rows are the + # ESPHOME_LOG_STATES=off shape the old bool(str) fallthrough broke + ("FOO", "on", False, True), + ("FOO", "enable", False, True), + ("FOO", "no", True, False), + ("FOO", "off", True, False), + ("FOO", "OFF", True, False), + ("FOO", "Disable", True, False), ), ) def test_get_bool_env(monkeypatch, var, value, default, expected): @@ -1108,22 +1116,3 @@ def test_progressbar_enabled_on_pipe_with_dashboard(monkeypatch) -> None: def test_format_duration(seconds: float, expected: str) -> None: """Test that durations are rendered as short human-readable strings.""" assert helpers.format_duration(seconds) == expected - - -@pytest.mark.parametrize( - ("value", "expected"), - [ - ("yes", True), - ("on", True), - ("enable", True), - ("no", False), - ("off", False), - ("disable", False), - ], -) -def test_get_bool_env_common_spellings( - monkeypatch: pytest.MonkeyPatch, value: str, expected: bool -) -> None: - """The cv.boolean spellings parse instead of reading as truthy.""" - monkeypatch.setenv("SOME_KNOB", value) - assert helpers.get_bool_env("SOME_KNOB") is expected