1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-06 05:12:21 +01:00

Allow substitutions to be valid names (#4726)

This commit is contained in:
Joel Goguen
2023-05-17 00:33:08 -04:00
committed by GitHub
parent 77695aa55b
commit b3ed988119
4 changed files with 69 additions and 14 deletions

View File

@@ -53,6 +53,7 @@ from esphome.const import (
KEY_TARGET_PLATFORM,
TYPE_GIT,
TYPE_LOCAL,
VALID_SUBSTITUTIONS_CHARACTERS,
)
from esphome.core import (
CORE,
@@ -79,6 +80,11 @@ from esphome.yaml_util import make_data_base
_LOGGER = logging.getLogger(__name__)
# pylint: disable=consider-using-f-string
VARIABLE_PROG = re.compile(
"\\$([{0}]+|\\{{[{0}]*\\}})".format(VALID_SUBSTITUTIONS_CHARACTERS)
)
# pylint: disable=invalid-name
Schema = _Schema
@@ -265,6 +271,14 @@ def alphanumeric(value):
def valid_name(value):
value = string_strict(value)
if CORE.vscode:
# If the value is a substitution, it can't be validated until the substitution
# is actually made.
sub_match = VARIABLE_PROG.search(value)
if sub_match:
return value
for c in value:
if c not in ALLOWED_NAME_CHARS:
raise Invalid(
@@ -447,6 +461,14 @@ def validate_id_name(value):
raise Invalid(
"Dashes are not supported in IDs, please use underscores instead."
)
if CORE.vscode:
# If the value is a substitution, it can't be validated until the substitution
# is actually made
sub_match = VARIABLE_PROG.match(value)
if sub_match:
return value
valid_chars = f"{ascii_letters + digits}_"
for char in value:
if char not in valid_chars: