CountrySelector (#100963)

* CountrySelector

* rename

* remove multiple for now

* Add no_sort

* Update homeassistant/helpers/selector.py

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
G Johansson 2023-10-12 16:44:30 +02:00 committed by GitHub
parent 5c52a15df7
commit dc29190564
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import voluptuous as vol
from homeassistant.const import CONF_MODE, CONF_UNIT_OF_MEASUREMENT
from homeassistant.core import split_entity_id, valid_entity_id
from homeassistant.generated.countries import COUNTRIES
from homeassistant.util import decorator
from homeassistant.util.yaml import dumper
@ -564,6 +565,40 @@ class ConversationAgentSelector(Selector[ConversationAgentSelectorConfig]):
return agent
class CountrySelectorConfig(TypedDict, total=False):
"""Class to represent a country selector config."""
countries: list[str]
no_sort: bool
@SELECTORS.register("country")
class CountrySelector(Selector[CountrySelectorConfig]):
"""Selector for a single-choice country select."""
selector_type = "country"
CONFIG_SCHEMA = vol.Schema(
{
vol.Optional("countries"): [str],
vol.Optional("no_sort", default=False): cv.boolean,
}
)
def __init__(self, config: CountrySelectorConfig | None = None) -> None:
"""Instantiate a selector."""
super().__init__(config)
def __call__(self, data: Any) -> Any:
"""Validate the passed selection."""
country: str = vol.Schema(str)(data)
if "countries" in self.config and (
country not in self.config["countries"] or country not in COUNTRIES
):
raise vol.Invalid(f"Value {country} is not a valid option")
return country
class DateSelectorConfig(TypedDict):
"""Class to represent a date selector config."""

View File

@ -479,6 +479,26 @@ def test_config_entry_selector_schema(
_test_selector("config_entry", schema, valid_selections, invalid_selections)
@pytest.mark.parametrize(
("schema", "valid_selections", "invalid_selections"),
(
(
{},
("NL", "DE"),
(None, True, 1),
),
(
{"countries": ["NL", "DE"]},
("NL", "DE"),
(None, True, 1, "sv", "en"),
),
),
)
def test_country_selector_schema(schema, valid_selections, invalid_selections) -> None:
"""Test country selector."""
_test_selector("country", schema, valid_selections, invalid_selections)
@pytest.mark.parametrize(
("schema", "valid_selections", "invalid_selections"),
(({}, ("00:00:00",), ("blah", None)),),