Skip to content

Commit

Permalink
Improve holidays config form and naming (#133663)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bors-ltd and gjohansson-ST authored Jan 8, 2025
1 parent 02e30ed commit d2a188a
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion homeassistant/components/holiday/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from homeassistant.helpers.selector import (
CountrySelector,
CountrySelectorConfig,
SelectOptionDict,
SelectSelector,
SelectSelectorConfig,
SelectSelectorMode,
Expand All @@ -30,6 +31,30 @@
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.
Expand All @@ -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,
Expand Down

0 comments on commit d2a188a

Please sign in to comment.