Skip to content

Commit

Permalink
Make translations keys check hassfest more strict (home-assistant#85221)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Jan 15, 2023
1 parent 209c473 commit 3cb5621
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions script/hassfest/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
REMOVED = 2

RE_REFERENCE = r"\[\%key:(.+)\%\]"
RE_TRANSLATION_KEY = re.compile(r"^(?!.+[_-]{2})(?![_-])[a-z0-9-_]+(?<![_-])$")

# Only allow translation of integration names if they contain non-brand names
ALLOW_NAME_TRANSLATION = {
Expand Down Expand Up @@ -86,9 +87,7 @@ def find_references(
find_references(value, f"{prefix}::{key}", found)
continue

match = re.match(RE_REFERENCE, value)

if match:
if match := re.match(RE_REFERENCE, value):
found.append({"source": f"{prefix}::{key}", "ref": match.groups()[0]})


Expand All @@ -106,10 +105,13 @@ def removed_title_validator(
return value


def lowercase_validator(value: str) -> str:
"""Validate value is lowercase."""
if value.lower() != value:
raise vol.Invalid("Needs to be lowercase")
def translation_key_validator(value: str) -> str:
"""Validate value is valid translation key."""
if RE_TRANSLATION_KEY.match(value) is None:
raise vol.Invalid(
f"Invalid translation key '{value}', need to be [a-z0-9-_]+ and"
" cannot start or end with a hyphen or underscore."
)

return value

Expand Down Expand Up @@ -221,27 +223,32 @@ def gen_strings_schema(config: Config, integration: Integration) -> vol.Schema:
vol.Optional("trigger_subtype"): {str: cv.string_with_no_html},
},
vol.Optional("state"): cv.schema_with_slug_keys(
cv.schema_with_slug_keys(str, slug_validator=lowercase_validator),
cv.schema_with_slug_keys(
cv.string_with_no_html, slug_validator=translation_key_validator
),
slug_validator=vol.Any("_", cv.slug),
),
vol.Optional("state_attributes"): cv.schema_with_slug_keys(
cv.schema_with_slug_keys(
{
vol.Optional("name"): str,
vol.Optional("state"): cv.schema_with_slug_keys(
str, slug_validator=lowercase_validator
cv.string_with_no_html,
slug_validator=translation_key_validator,
),
},
slug_validator=lowercase_validator,
slug_validator=translation_key_validator,
),
slug_validator=vol.Any("_", cv.slug),
),
vol.Optional("system_health"): {
vol.Optional("info"): {str: cv.string_with_no_html}
vol.Optional("info"): cv.schema_with_slug_keys(
cv.string_with_no_html, slug_validator=translation_key_validator
),
},
vol.Optional("config_panel"): cv.schema_with_slug_keys(
cv.schema_with_slug_keys(
cv.string_with_no_html, slug_validator=lowercase_validator
cv.string_with_no_html, slug_validator=translation_key_validator
),
slug_validator=vol.Any("_", cv.slug),
),
Expand Down Expand Up @@ -273,10 +280,16 @@ def gen_strings_schema(config: Config, integration: Integration) -> vol.Schema:
vol.Optional("state_attributes"): {
str: {
vol.Optional("name"): cv.string_with_no_html,
vol.Optional("state"): {str: cv.string_with_no_html},
vol.Optional("state"): cv.schema_with_slug_keys(
cv.string_with_no_html,
slug_validator=translation_key_validator,
),
}
},
vol.Optional("state"): {str: cv.string_with_no_html},
vol.Optional("state"): cv.schema_with_slug_keys(
cv.string_with_no_html,
slug_validator=translation_key_validator,
),
}
}
},
Expand Down Expand Up @@ -352,7 +365,7 @@ def device_class_validator(value: str) -> str:
return vol.Schema(
{
vol.Optional("state"): cv.schema_with_slug_keys(
cv.schema_with_slug_keys(str, slug_validator=lowercase_validator),
cv.schema_with_slug_keys(str, slug_validator=translation_key_validator),
slug_validator=device_class_validator,
)
}
Expand Down

0 comments on commit 3cb5621

Please sign in to comment.