8000 Fix language selections in workday (#145813) · home-assistant/core@d8e3e88 · GitHub
[go: up one dir, main page]

10000 Skip to content

Commit d8e3e88

Browse files
Fix language selections in workday (#145813)
1 parent d1d1bca commit d8e3e88

File tree

3 files changed

+89
-18
lines changed

3 files changed

+89
-18
lines changed

homeassistant/components/workday/binary_sensor.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,59 @@ def _get_obj_holidays(
9494
language=language,
9595
categories=set_categories,
9696
)
97+
98+
supported_languages = obj_holidays.supported_languages
99+
default_language = obj_holidays.default_language
100+
101+
if default_language and not language:
102+
# If no language is set, use the default language
103+
LOGGER.debug("Changing language from None to %s", default_language)
104+
return country_holidays( # Return default if no language
105+
country,
106+
subdiv=province,
107+
years=year,
108+
language=default_language,
109+
categories=set_categories,
110+
)
111+
97112
if (
98-
(supported_languages := obj_holidays.supported_languages)
113+
default_language
99114
and language
115+
and language not in supported_languages
100116
and language.startswith("en")
101117
):
118+
# If language does not match supported languages, use the first English variant
119+
if default_language.startswith("en"):
120+
LOGGER.debug("Changing language from %s to %s", language, default_language)
121+
return country_holidays( # Return default English if default language
122+
country,
123+
subdiv=province,
124+
years=year,
125+
language=default_language,
126+
categories=set_categories,
127+
)
102128
for lang in supported_languages:
103129
if lang.startswith("en"):
104-
obj_holidays = country_holidays(
130+
LOGGER.debug("Changing language from %s to %s", language, lang)
131+
return country_holidays(
105132
country,
106133
subdiv=province,
107134
years=year,
108135
language=lang,
109136
categories=set_categories,
110137
)
111-
LOGGER.debug("Changing language from %s to %s", language, lang)
138+
139+
if default_language and language and language not in supported_languages:
140+
# If language does not match supported languages, use the default language
141+
LOGGER.debug("Changing language from %s to %s", language, default_language)
142+
return country_holidays( # Return default English if default language
143+
country,
144+
subdiv=province,
145+
years=year,
146+
language=default_language,
147+
categories=set_categories,
148+
)
149+
112150
return obj_holidays
113151

114152

homeassistant/components/workday/config_flow.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ def add_province_and_language_to_schema(
6767

6868
_country = country_holidays(country=country)
6969
if country_default_language := (_country.default_language):
70-
selectable_languages = _country.supported_languages
71-
new_selectable_languages = list(selectable_languages)
70+
new_selectable_languages = list(_country.supported_languages)
7271
language_schema = {
7372
vol.Optional(
7473
CONF_LANGUAGE, default=country_default_language
@@ -154,19 +153,7 @@ def validate_custom_dates(user_input: dict[str, Any]) -> None:
154153
years=year,
155154
language=language,
156155
)
157-
if (
158-
(supported_languages := obj_holidays.supported_languages)
159-
and language
160-
and language.startswith("en")
161-
):
162-
for lang in supported_languages:
163-
if lang.startswith("en"):
164-
obj_holidays = country_holidays(
165-
country,
166-
subdiv=province,
167-
years=year,
168-
language=lang,
169-
)
156+
170157
else:
171158
obj_holidays = HolidayBase(years=year)
172159

tests/components/workday/test_binary_sensor.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,49 @@ async def test_only_repairs_for_current_next_year(
461461

462462
assert len(issue_registry.issues) == 2
463463
assert issue_registry.issues == snapshot
464+
465+
466+
async def test_missing_language(
467+
hass: HomeAssistant,
468+
caplog: pytest.LogCaptureFixture,
469+
) -> None:
470+
"""Test when language exist but is empty."""
471+
config = {
472+
"add_holidays": [],
473+
"country": "AU",
474+
"days_offset": 0,
475+
"excludes": ["sat", "sun", "holiday"],
476+
"language": None,
477+
"name": "Workday Sensor",
478+
"platform": "workday",
479+
"province": "QLD",
480+
"remove_holidays": [
481+
"Labour Day",
482+
],
483+
"workdays": ["mon", "tue", "wed", "thu", "fri"],
484+
}
485+
await init_integration(hass, config)
486+
assert "Changing language from None to en_AU" in caplog.text
487+
488+
489+
async def test_incorrect_english_variant(
490+
hass: HomeAssistant,
491+
caplog: pytest.LogCaptureFixture,
492+
) -> None:
493+
"""Test when language exist but is empty."""
494+
config = {
495+
"add_holidays": [],
496+
"country": "AU",
497+
"days_offset": 0,
498+
"excludes": ["sat", "sun", "holiday"],
499+
"language": "en_UK", # Incorrect variant
500+
"name": "Workday Sensor",
501+
"platform": "workday",
502+
"province": "QLD",
503+
"remove_holidays": [
504+
"Labour Day",
505+
],
506+
"workdays": ["mon", "tue", "wed", "thu", "fri"],
507+
}
508+
await init_integration(hass, config)
509+
assert "Changing language from en_UK to en_AU" in caplog.text

0 commit comments

Comments
 (0)
0