Skip to content

Commit

Permalink
Be nicer about data translations
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet committed Oct 10, 2024
1 parent 1e35d30 commit 522f6a0
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions tests/components/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,31 +445,34 @@ def supervisor_client() -> Generator[AsyncMock]:
yield supervisor_client


_IGNORE_TRANSLATION_VIOLATIONS = {
# These data input have a step description and it would be redundant to add
# a separate description for the data input
"component.airvisual.config.step.user.data.type",
"component.ambient_network.config.step.user.data.location",
"component.axis.options.step.configure_stream.data.video_source",
"component.tesla_fleet.config.step.pick_implementation.data.implementation",
}


async def _ensure_translation_exists(
hass: HomeAssistant, category: str, component: str, key: str
) -> None:
"""Raise if translation doesn't exist."""
full_key = f"component.{component}.{category}.{key}"
if full_key in _IGNORE_TRANSLATION_VIOLATIONS:
translations = await async_get_translations(hass, "en", category, [component])
if f"component.{component}.{category}.{key}" in translations:
return

translations = await async_get_translations(hass, "en", category, [component])
if full_key not in translations:
raise ValueError(
f"Translation not found for {component}: `{category}.{key}`. "
f"Please add to homeassistant/components/{component}/strings.json "
"or add to _IGNORE_TRANSLATION_VIOLATIONS."
key_parts = key.split(".")
# Ignore step data translations if title or description exists
if (
len(key_parts) >= 3
and key_parts[0] == "step"
and key_parts[2] == "data"
and (
f"component.{component}.{category}.{key_parts[0]}.{key_parts[1]}.description"
in translations
or f"component.{component}.{category}.{key_parts[0]}.{key_parts[1]}.title"
in translations
)
):
return

raise ValueError(
f"Translation not found for {component}: `{category}.{key}`. "
f"Please add to homeassistant/components/{component}/strings.json "
"or add to _IGNORE_TRANSLATION_VIOLATIONS."
)


@pytest.fixture(autouse=True)
Expand Down

0 comments on commit 522f6a0

Please sign in to comment.