Fix holiday HA language not supported (#106554)
This commit is contained in:
parent
1d0fafcf2d
commit
d7a697faf4
2 changed files with 32 additions and 3 deletions
|
@ -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}"
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue