Fold the spellings into the main table, cover True defaults and case folding, correct the 1/0 note

This commit is contained in:
J. Nick Koston
2026-08-22 23:49:01 -05:00
parent 9543930085
commit 054b133a98
2 changed files with 11 additions and 22 deletions
+3 -3
View File
@@ -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):
+8 -19
View File
@@ -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