mirror of
https://github.com/esphome/esphome.git
synced 2026-09-11 15:27:33 +00:00
Fold the spellings into the main table, cover True defaults and case folding, correct the 1/0 note
This commit is contained in:
+3
-3
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user