From d2a188ad3cea7bf3b02bc0955930c848b8ef9927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Cauwelier?= Date: Wed, 8 Jan 2025 17:19:28 +0100 Subject: [PATCH] Improve holidays config form and naming (#133663) The holidays library is improving over time, let's make use of their data for a more user-friendly experience. Co-authored-by: G Johansson --- .../components/holiday/config_flow.py | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/holiday/config_flow.py b/homeassistant/components/holiday/config_flow.py index 00a71351ca7..6d29e09c0f8 100644 --- a/homeassistant/components/holiday/config_flow.py +++ b/homeassistant/components/holiday/config_flow.py @@ -19,6 +19,7 @@ from homeassistant.core import callback from homeassistant.helpers.selector import ( CountrySelector, CountrySelectorConfig, + SelectOptionDict, SelectSelector, SelectSelectorConfig, SelectSelectorMode, @@ -30,6 +31,30 @@ from .const import CONF_CATEGORIES, CONF_PROVINCE, DOMAIN SUPPORTED_COUNTRIES = list_supported_countries(include_aliases=False) +def get_optional_provinces(country: str) -> list[Any]: + """Return the country provinces (territories). + + Some territories can have extra or different holidays + from another within the same country. + Some territories can have different names (aliases). + """ + province_options: list[Any] = [] + + if provinces := SUPPORTED_COUNTRIES[country]: + country_data = country_holidays(country, years=dt_util.utcnow().year) + if country_data.subdivisions_aliases and ( + subdiv_aliases := country_data.get_subdivision_aliases() + ): + province_options = [ + SelectOptionDict(value=k, label=", ".join(v)) + for k, v in subdiv_aliases.items() + ] + else: + province_options = provinces + + return province_options + + def get_optional_categories(country: str) -> list[str]: """Return the country categories. @@ -45,7 +70,7 @@ def get_optional_categories(country: str) -> list[str]: def get_options_schema(country: str) -> vol.Schema: """Return the options schema.""" schema = {} - if provinces := SUPPORTED_COUNTRIES[country]: + if provinces := get_optional_provinces(country): schema[vol.Optional(CONF_PROVINCE)] = SelectSelector( SelectSelectorConfig( options=provinces,