diff --git a/esphome/build_helpers/ccache.py b/esphome/build_helpers/ccache.py index dfceaabf1c..82d62615ee 100644 --- a/esphome/build_helpers/ccache.py +++ b/esphome/build_helpers/ccache.py @@ -8,12 +8,13 @@ import os from pathlib import Path from esphome.framework_helpers import strip_win_long_path_prefix, tool_version_runs +from esphome.helpers import FALSY_BOOL_STRINGS, TRUTHY_BOOL_STRINGS _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"}) +# 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"} def _ccache_runs(ccache: str) -> bool: diff --git a/esphome/config_validation.py b/esphome/config_validation.py index 98001d5d5b..09962e8c95 100644 --- a/esphome/config_validation.py +++ b/esphome/config_validation.py @@ -93,7 +93,13 @@ from esphome.core import ( ) from esphome.enum import StrEnum from esphome.expression import SUBSTITUTION_VARIABLE_PROG as VARIABLE_PROG -from esphome.helpers import add_class_to_obj, docs_url, list_starts_with +from esphome.helpers import ( + FALSY_BOOL_STRINGS, + TRUTHY_BOOL_STRINGS, + add_class_to_obj, + docs_url, + list_starts_with, +) from esphome.schema_extractors import ( SCHEMA_EXTRACT, schema_extractor, @@ -581,9 +587,9 @@ def boolean(value): return value if isinstance(value, str): value = value.lower() - if value in ("true", "yes", "on", "enable"): + if value in TRUTHY_BOOL_STRINGS: return True - if value in ("false", "no", "off", "disable"): + if value in FALSY_BOOL_STRINGS: return False raise Invalid( f"Expected boolean value, but cannot convert {value} to a boolean. Please use 'true' or 'false'" diff --git a/esphome/helpers.py b/esphome/helpers.py index 9b2a461ccd..fe3c383f59 100644 --- a/esphome/helpers.py +++ b/esphome/helpers.py @@ -31,6 +31,11 @@ SockAddr = IPv4SockAddr | IPv6SockAddr _LOGGER = logging.getLogger(__name__) +# cv.boolean's closed spelling tables; shared so env-knob parsers cannot +# drift from what configs accept +TRUTHY_BOOL_STRINGS = frozenset({"true", "yes", "on", "enable"}) +FALSY_BOOL_STRINGS = frozenset({"false", "no", "off", "disable"}) + IS_MACOS = platform.system() == "Darwin" IS_WINDOWS = platform.system() == "Windows" IS_LINUX = platform.system() == "Linux"