Skip to content

Commit

Permalink
Allow translating select selector options (home-assistant#85531)
Browse files Browse the repository at this point in the history
Co-authored-by: Franck Nijhof <[email protected]>
  • Loading branch information
jbouwh and frenck authored Jan 17, 2023
1 parent 096ef5d commit 2539265
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/mqtt/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
SelectSelectorConfig(
options=CA_VERIFICATION_MODES,
mode=SelectSelectorMode.DROPDOWN,
translation_key=SET_CA_CERT,
)
)

Expand Down
9 changes: 9 additions & 0 deletions homeassistant/components/mqtt/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,14 @@
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_inclusion": "[%key:component::mqtt::config::error::invalid_inclusion%]"
}
},
"selector": {
"set_ca_cert": {
"options": {
"off": "Off",
"auto": "Auto",
"custom": "Custom"
}
}
}
}
9 changes: 9 additions & 0 deletions homeassistant/components/mqtt/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,14 @@
"title": "MQTT options"
}
}
},
"selector": {
"set_ca_cert": {
"options": {
"off": "Off",
"auto": "Auto",
"custom": "Custom"
}
}
}
}
2 changes: 2 additions & 0 deletions homeassistant/helpers/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ class SelectSelectorConfig(TypedDict, total=False):
multiple: bool
custom_value: bool
mode: SelectSelectorMode
translation_key: str


@SELECTORS.register("select")
Expand All @@ -749,6 +750,7 @@ class SelectSelector(Selector[SelectSelectorConfig]):
vol.Optional("mode"): vol.All(
vol.Coerce(SelectSelectorMode), lambda val: val.value
),
vol.Optional("translation_key"): cv.string,
}
)

Expand Down
8 changes: 8 additions & 0 deletions script/hassfest/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ def gen_strings_schema(config: Config, integration: Integration) -> vol.Schema:
flow_title=UNDEFINED,
require_step_title=False,
),
vol.Optional("selector"): cv.schema_with_slug_keys(
{
"options": cv.schema_with_slug_keys(
cv.string_with_no_html, slug_validator=translation_key_validator
)
},
slug_validator=vol.Any("_", cv.slug),
),
vol.Optional("device_automation"): {
vol.Optional("action_type"): {str: cv.string_with_no_html},
vol.Optional("condition_type"): {str: cv.string_with_no_html},
Expand Down
19 changes: 19 additions & 0 deletions tests/helpers/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,25 @@ def test_text_selector_schema(schema, valid_selections, invalid_selections):
("red", "green"),
("cat", 0, None, ["red"]),
),
(
{
"options": ["red", "green", "blue"],
"translation_key": "color",
},
("red", "green", "blue"),
("cat", 0, None, ["red"]),
),
(
{
"options": [
{"value": "red", "label": "Ruby Red"},
{"value": "green", "label": "Emerald Green"},
],
"translation_key": "color",
},
("red", "green"),
("cat", 0, None, ["red"]),
),
(
{"options": ["red", "green", "blue"], "multiple": True},
(["red"], ["green", "blue"], []),
Expand Down

0 comments on commit 2539265

Please sign in to comment.