Skip to content

Commit

Permalink
Change to language selector in Workday (home-assistant#104472)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST authored Nov 25, 2023
1 parent df37ee4 commit 8376a6b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
12 changes: 12 additions & 0 deletions homeassistant/components/workday/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ async def async_setup_entry(
years=year,
language=language,
)
if (
supported_languages := obj_holidays.supported_languages
) and language == "en":
for lang in supported_languages:
if lang.startswith("en"):
obj_holidays = country_holidays(
country,
subdiv=province,
years=year,
language=lang,
)
LOGGER.debug("Changing language from %s to %s", language, lang)
else:
obj_holidays = HolidayBase()

Expand Down
29 changes: 22 additions & 7 deletions homeassistant/components/workday/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from homeassistant.helpers.selector import (
CountrySelector,
CountrySelectorConfig,
LanguageSelector,
LanguageSelectorConfig,
NumberSelector,
NumberSelectorConfig,
NumberSelectorMode,
Expand Down Expand Up @@ -62,14 +64,14 @@ def add_province_and_language_to_schema(
_country = country_holidays(country=country)
if country_default_language := (_country.default_language):
selectable_languages = _country.supported_languages
new_selectable_languages = []
for lang in selectable_languages:
new_selectable_languages.append(lang[:2])
language_schema = {
vol.Optional(
CONF_LANGUAGE, default=country_default_language
): SelectSelector(
SelectSelectorConfig(
options=list(selectable_languages),
mode=SelectSelectorMode.DROPDOWN,
)
): LanguageSelector(
LanguageSelectorConfig(languages=new_selectable_languages)
)
}

Expand Down Expand Up @@ -109,12 +111,25 @@ def validate_custom_dates(user_input: dict[str, Any]) -> None:

year: int = dt_util.now().year
if country := user_input.get(CONF_COUNTRY):
language = user_input.get(CONF_LANGUAGE)
province = user_input.get(CONF_PROVINCE)
obj_holidays = country_holidays(
country=country,
subdiv=user_input.get(CONF_PROVINCE),
subdiv=province,
years=year,
language=user_input.get(CONF_LANGUAGE),
language=language,
)
if (
supported_languages := obj_holidays.supported_languages
) and language == "en":
for lang in supported_languages:
if lang.startswith("en"):
obj_holidays = country_holidays(
country,
subdiv=province,
years=year,
language=lang,
)
else:
obj_holidays = HolidayBase(years=year)

Expand Down
22 changes: 22 additions & 0 deletions tests/components/workday/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,25 @@ async def init_integration(
"remove_holidays": ["2022-12-04", "2022-12-24,2022-12-26"],
"language": "de",
}
TEST_LANGUAGE_CHANGE = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": ["2022-12-01", "2022-12-05,2022-12-15"],
"remove_holidays": ["2022-12-04", "2022-12-24,2022-12-26"],
"language": "en",
}
TEST_LANGUAGE_NO_CHANGE = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": ["2022-12-01", "2022-12-05,2022-12-15"],
"remove_holidays": ["2022-12-04", "2022-12-24,2022-12-26"],
"language": "de",
}
20 changes: 20 additions & 0 deletions tests/components/workday/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
TEST_CONFIG_WITH_PROVINCE,
TEST_CONFIG_WITH_STATE,
TEST_CONFIG_YESTERDAY,
TEST_LANGUAGE_CHANGE,
TEST_LANGUAGE_NO_CHANGE,
init_integration,
)

Expand Down Expand Up @@ -313,3 +315,21 @@ async def test_check_date_service(
return_response=True,
)
assert response == {"binary_sensor.workday_sensor": {"workday": True}}


async def test_language_difference_english_language(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test handling difference in English language naming."""
await init_integration(hass, TEST_LANGUAGE_CHANGE)
assert "Changing language from en to en_US" in caplog.text


async def test_language_difference_no_change_other_language(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test skipping if no difference in language naming."""
await init_integration(hass, TEST_LANGUAGE_NO_CHANGE)
assert "Changing language from en to en_US" not in caplog.text
2 changes: 1 addition & 1 deletion tests/components/workday/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ async def test_options_form_incorrect_date_ranges(hass: HomeAssistant) -> None:
("language", "holiday"),
[
("de", "Weihnachtstag"),
("en_US", "Christmas"),
("en", "Christmas"),
],
)
async def test_language(
Expand Down

0 comments on commit 8376a6b

Please sign in to comment.