Skip to content

Commit

Permalink
Allow for translating service examples (home-assistant#97141)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Jul 24, 2023
1 parent 0c4e341 commit b655b9d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions homeassistant/helpers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,10 @@ async def async_get_all_descriptions(
f"component.{domain}.services.{service_name}.fields.{field_name}.description"
):
field_schema["description"] = desc
if example := translations.get(
f"component.{domain}.services.{service_name}.fields.{field_name}.example"
):
field_schema["example"] = example

if "target" in yaml_description:
description["target"] = yaml_description["target"]
Expand Down
1 change: 1 addition & 0 deletions script/hassfest/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ def gen_strings_schema(config: Config, integration: Integration) -> vol.Schema:
{
vol.Required("name"): str,
vol.Required("description"): translation_value_validator,
vol.Optional("example"): translation_value_validator,
},
slug_validator=translation_key_validator,
),
Expand Down
5 changes: 5 additions & 0 deletions tests/helpers/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ async def async_get_translations(
f"{translation_key_prefix}.description": "Translated description",
f"{translation_key_prefix}.fields.level.name": "Field name",
f"{translation_key_prefix}.fields.level.description": "Field description",
f"{translation_key_prefix}.fields.level.example": "Field example",
}

with patch(
Expand All @@ -599,6 +600,10 @@ async def async_get_translations(
]
== "Field description"
)
assert (
descriptions[logger.DOMAIN]["set_default_level"]["fields"]["level"]["example"]
== "Field example"
)

hass.services.async_register(logger.DOMAIN, "new_service", lambda x: None, None)
service.async_set_service_schema(
Expand Down

0 comments on commit b655b9d

Please sign in to comment.