Fix schema translation checks for nested config-flow sections (#133392)

This commit is contained in:
epenet 2024-12-17 12:02:53 +01:00 committed by GitHub
parent ce0117b2b8
commit 991864b38c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -19,6 +19,7 @@ from aiohasupervisor.models import (
StoreInfo, StoreInfo,
) )
import pytest import pytest
import voluptuous as vol
from homeassistant.components import repairs from homeassistant.components import repairs
from homeassistant.config_entries import ( from homeassistant.config_entries import (
@ -34,6 +35,7 @@ from homeassistant.data_entry_flow import (
FlowHandler, FlowHandler,
FlowManager, FlowManager,
FlowResultType, FlowResultType,
section,
) )
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import issue_registry as ir from homeassistant.helpers import issue_registry as ir
@ -644,6 +646,61 @@ def _get_integration_quality_scale_rule(integration: str, rule: str) -> str:
return status if isinstance(status, str) else status["status"] return status if isinstance(status, str) else status["status"]
async def _check_step_or_section_translations(
hass: HomeAssistant,
translation_errors: dict[str, str],
category: str,
integration: str,
translation_prefix: str,
description_placeholders: dict[str, str],
data_schema: vol.Schema | None,
) -> None:
# neither title nor description are required
# - title defaults to integration name
# - description is optional
for header in ("title", "description"):
await _validate_translation(
hass,
translation_errors,
category,
integration,
f"{translation_prefix}.{header}",
description_placeholders,
translation_required=False,
)
if not data_schema:
return
for data_key, data_value in data_schema.schema.items():
if isinstance(data_value, section):
# check the nested section
await _check_step_or_section_translations(
hass,
translation_errors,
category,
integration,
f"{translation_prefix}.sections.{data_key}",
description_placeholders,
data_value.schema,
)
return
iqs_config_flow = _get_integration_quality_scale_rule(
integration, "config-flow"
)
# data and data_description are compulsory
for header in ("data", "data_description"):
await _validate_translation(
hass,
translation_errors,
category,
integration,
f"{translation_prefix}.{header}.{data_key}",
description_placeholders,
translation_required=(iqs_config_flow == "done"),
)
async def _check_config_flow_result_translations( async def _check_config_flow_result_translations(
manager: FlowManager, manager: FlowManager,
flow: FlowHandler, flow: FlowHandler,
@ -675,35 +732,16 @@ async def _check_config_flow_result_translations(
setattr(flow, "__flow_seen_before", hasattr(flow, "__flow_seen_before")) setattr(flow, "__flow_seen_before", hasattr(flow, "__flow_seen_before"))
if result["type"] is FlowResultType.FORM: if result["type"] is FlowResultType.FORM:
iqs_config_flow = _get_integration_quality_scale_rule(
integration, "config-flow"
)
if step_id := result.get("step_id"): if step_id := result.get("step_id"):
# neither title nor description are required await _check_step_or_section_translations(
# - title defaults to integration name flow.hass,
# - description is optional translation_errors,
for header in ("title", "description"): category,
await _validate_translation( integration,
flow.hass, f"{key_prefix}step.{step_id}",
translation_errors, result["description_placeholders"],
category, result["data_schema"],
integration, )
f"{key_prefix}step.{step_id}.{header}",
result["description_placeholders"],
translation_required=False,
)
if iqs_config_flow == "done" and (data_schema := result["data_schema"]):
# data and data_description are compulsory
for data_key in data_schema.schema:
for header in ("data", "data_description"):
await _validate_translation(
flow.hass,
translation_errors,
category,
integration,
f"{key_prefix}step.{step_id}.{header}.{data_key}",
result["description_placeholders"],
)
if errors := result.get("errors"): if errors := result.get("errors"):
for error in errors.values(): for error in errors.values():