diff --git a/esphome/helpers.py b/esphome/helpers.py index 9bb57b6468..7f0f340454 100644 --- a/esphome/helpers.py +++ b/esphome/helpers.py @@ -31,6 +31,13 @@ SockAddr = IPv4SockAddr | IPv6SockAddr _LOGGER = logging.getLogger(__name__) +# cv.boolean's closed spelling tables, shared with the env-knob parsing below +TRUTHY_BOOL_STRINGS = frozenset({"true", "yes", "on", "enable"}) +FALSY_BOOL_STRINGS = frozenset({"false", "no", "off", "disable"}) +# cv.boolean's spelling tables plus the 1/0 env convention +TRUTHY_ENV_STRINGS = TRUTHY_BOOL_STRINGS | {"1"} +FALSY_ENV_STRINGS = FALSY_BOOL_STRINGS | {"0"} + IS_MACOS = platform.system() == "Darwin" IS_WINDOWS = platform.system() == "Windows" IS_LINUX = platform.system() == "Linux" @@ -400,9 +407,9 @@ def get_bool_env(var, default=False): value = os.getenv(var, default) if isinstance(value, str): value = value.lower() - if value in ("1", "true", "yes", "on", "enable"): + if value in TRUTHY_ENV_STRINGS: return True - if value in ("0", "false", "no", "off", "disable"): + if value in FALSY_ENV_STRINGS: return False return bool(value)