Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix language flavors in holiday #107392

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions homeassistant/components/holiday/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ async def async_setup_entry(
)
language = lang
break
if (
obj_holidays.supported_languages
and language not in obj_holidays.supported_languages
and (default_language := obj_holidays.default_language)
):
obj_holidays = country_holidays(
country,
subdiv=province,
years={dt_util.now().year, dt_util.now().year + 1},
language=default_language,
)
language = default_language

async_add_entities(
[
Expand Down
85 changes: 85 additions & 0 deletions tests/components/holiday/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,88 @@ async def test_no_next_event(
assert state is not None
assert state.state == "off"
assert state.attributes == {"friendly_name": "Germany"}


async def test_language_not_exist(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test when language doesn't exist it will fallback to country default language."""

hass.config.language = "nb" # Norweigan language "Norks bokmål"
hass.config.country = "NO"

freezer.move_to(datetime(2023, 1, 1, 12, tzinfo=dt_util.UTC))

config_entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_COUNTRY: "NO"},
title="Norge",
)
config_entry.add_to_hass(hass)

await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

state = hass.states.get("calendar.norge")
assert state is not None
assert state.state == "on"
assert state.attributes == {
"friendly_name": "Norge",
"all_day": True,
"description": "",
"end_time": "2023-01-02 00:00:00",
"location": "Norge",
"message": "Første nyttårsdag",
"start_time": "2023-01-01 00:00:00",
}

response = await hass.services.async_call(
CALENDAR_DOMAIN,
SERVICE_GET_EVENTS,
{
"entity_id": "calendar.norge",
"end_date_time": dt_util.now(),
},
blocking=True,
return_response=True,
)
assert response == {
"calendar.norge": {
"events": [
{
"start": "2023-01-01",
"end": "2023-01-02",
"summary": "Første nyttårsdag",
"location": "Norge",
}
]
}
}

# Test with English as exist as optional language for Norway
hass.config.language = "en"
hass.config.country = "NO"
await hass.config_entries.async_reload(config_entry.entry_id)
await hass.async_block_till_done()
response = await hass.services.async_call(
CALENDAR_DOMAIN,
SERVICE_GET_EVENTS,
{
"entity_id": "calendar.norge",
"end_date_time": dt_util.now(),
},
blocking=True,
return_response=True,
)
assert response == {
"calendar.norge": {
"events": [
{
"start": "2023-01-01",
"end": "2023-01-02",
"summary": "New Year's Day",
"location": "Norge",
}
]
}
}