Allow translating select selector options (#85531)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
Jan Bouwhuis 2023-01-17 16:22:19 +01:00 committed by GitHub
parent 096ef5da47
commit 25392655e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 0 deletions

View File

@ -137,6 +137,7 @@ BROKER_VERIFICATION_SELECTOR = SelectSelector(
SelectSelectorConfig(
options=CA_VERIFICATION_MODES,
mode=SelectSelectorMode.DROPDOWN,
translation_key=SET_CA_CERT,
)
)

View File

@ -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"
}
}
}
}

View File

@ -136,5 +136,14 @@
"title": "MQTT options"
}
}
},
"selector": {
"set_ca_cert": {
"options": {
"off": "Off",
"auto": "Auto",
"custom": "Custom"
}
}
}
}

View File

@ -733,6 +733,7 @@ class SelectSelectorConfig(TypedDict, total=False):
multiple: bool
custom_value: bool
mode: SelectSelectorMode
translation_key: str
@SELECTORS.register("select")
@ -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,
}
)

View File

@ -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},

View File

@ -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"], []),