From d7a697faf45b48ce806bcb77ddf111eb404adf03 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Thu, 28 Dec 2023 16:10:27 +0100 Subject: [PATCH] Fix holiday HA language not supported (#106554) --- .../components/holiday/config_flow.py | 16 +++++++++++++--- tests/components/holiday/test_config_flow.py | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/holiday/config_flow.py b/homeassistant/components/holiday/config_flow.py index 1ba4a2a0c26..842849a7c57 100644 --- a/homeassistant/components/holiday/config_flow.py +++ b/homeassistant/components/holiday/config_flow.py @@ -3,7 +3,7 @@ from __future__ import annotations from typing import Any -from babel import Locale +from babel import Locale, UnknownLocaleError from holidays import list_supported_countries import voluptuous as vol @@ -46,7 +46,12 @@ class HolidayConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self._async_abort_entries_match({CONF_COUNTRY: user_input[CONF_COUNTRY]}) - locale = Locale(self.hass.config.language) + try: + locale = Locale(self.hass.config.language) + except UnknownLocaleError: + # Default to (US) English if language not recognized by babel + # Mainly an issue with English flavors such as "en-GB" + locale = Locale("en") title = locale.territories[selected_country] return self.async_create_entry(title=title, data=user_input) @@ -81,7 +86,12 @@ class HolidayConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): } ) - locale = Locale(self.hass.config.language) + try: + locale = Locale(self.hass.config.language) + except UnknownLocaleError: + # Default to (US) English if language not recognized by babel + # Mainly an issue with English flavors such as "en-GB" + locale = Locale("en") province_str = f", {province}" if province else "" name = f"{locale.territories[country]}{province_str}" diff --git a/tests/components/holiday/test_config_flow.py b/tests/components/holiday/test_config_flow.py index e99d310762e..c88d66d843b 100644 --- a/tests/components/holiday/test_config_flow.py +++ b/tests/components/holiday/test_config_flow.py @@ -126,3 +126,22 @@ async def test_single_combination_country_province(hass: HomeAssistant) -> None: ) assert result_de_step2["type"] == FlowResultType.ABORT assert result_de_step2["reason"] == "already_configured" + + +async def test_form_babel_unresolved_language(hass: HomeAssistant) -> None: + """Test the config flow if using not babel supported language.""" + hass.config.language = "en-GB" + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_COUNTRY: "SE", + }, + ) + await hass.async_block_till_done() + + assert result2["title"] == "Sweden"