Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNM: Ensure config_flow translations are available #127787

Draft
wants to merge 17 commits into
base: dev
Choose a base branch
from
108 changes: 108 additions & 0 deletions tests/components/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,21 @@
from aiohasupervisor.models import StoreInfo
import pytest

from homeassistant.config_entries import (
DISCOVERY_SOURCES,
ConfigEntriesFlowManager,
FlowResult,
OptionsFlowManager,
)
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import (
FlowHandler,
FlowManager,
FlowResultType,
section,
)
from homeassistant.helpers.translation import async_get_translations

if TYPE_CHECKING:
from homeassistant.components.hassio import AddonManager
Expand Down Expand Up @@ -438,3 +451,98 @@ def supervisor_client() -> Generator[AsyncMock]:
),
):
yield supervisor_client


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

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)
def check_config_translations() -> Generator[None]:
"""Ensure config_flow translations are available."""
_original = FlowManager._async_handle_step

async def _async_handle_step(
self: FlowManager, flow: FlowHandler, *args
) -> FlowResult:
result = await _original(self, flow, *args)
if isinstance(self, ConfigEntriesFlowManager):
category = "config"
component = flow.handler
elif isinstance(self, OptionsFlowManager):
category = "options"
component = flow.hass.config_entries.async_get_entry(flow.handler).domain
else:
return result

if result["type"] is FlowResultType.FORM:
if data_schema := result.get("data_schema"):
for key, value in data_schema.schema.items():
if isinstance(value, section):
for sub_key in value.schema.schema:
await _ensure_translation_exists(
flow.hass,
category,
component,
f"step.{result['step_id']}.sections.{key}.data.{sub_key}",
)
else:
await _ensure_translation_exists(
flow.hass,
category,
component,
f"step.{result['step_id']}.data.{key}",
)
if errors := result.get("errors"):
for error in errors.values():
await _ensure_translation_exists(
flow.hass,
category,
component,
f"error.{error}",
)

if (
result["type"] is FlowResultType.ABORT
and flow.source not in DISCOVERY_SOURCES
):
await _ensure_translation_exists(
flow.hass,
category,
component,
f"abort.{result["reason"]}",
)

return result

with patch(
"homeassistant.data_entry_flow.FlowManager._async_handle_step",
_async_handle_step,
):
yield
Loading