Fix incorrect categories handling in holiday (#146470)

This commit is contained in:
G Johansson 2025-06-10 19:28:48 +02:00 committed by GitHub
parent 5cc9cc3c99
commit 481bf2694b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 87 additions and 9 deletions

View File

@ -25,17 +25,12 @@ def _get_obj_holidays_and_language(
selected_categories: list[str] | None,
) -> tuple[HolidayBase, str]:
"""Get the object for the requested country and year."""
if selected_categories is None:
categories = [PUBLIC]
else:
categories = [PUBLIC, *selected_categories]
obj_holidays = country_holidays(
country,
subdiv=province,
years={dt_util.now().year, dt_util.now().year + 1},
language=language,
categories=categories,
categories=selected_categories,
)
if language == "en":
for lang in obj_holidays.supported_languages:
@ -45,7 +40,7 @@ def _get_obj_holidays_and_language(
subdiv=province,
years={dt_util.now().year, dt_util.now().year + 1},
language=lang,
categories=categories,
categories=selected_categories,
)
language = lang
break
@ -59,7 +54,7 @@ def _get_obj_holidays_and_language(
subdiv=province,
years={dt_util.now().year, dt_util.now().year + 1},
language=default_language,
categories=categories,
categories=selected_categories,
)
language = default_language
@ -77,6 +72,11 @@ async def async_setup_entry(
categories: list[str] | None = config_entry.options.get(CONF_CATEGORIES)
language = hass.config.language
if categories is None:
categories = [PUBLIC]
else:
categories = [PUBLIC, *categories]
obj_holidays, language = await hass.async_add_executor_job(
_get_obj_holidays_and_language, country, province, language, categories
)

View File

@ -3,13 +3,18 @@
from datetime import datetime, timedelta
from freezegun.api import FrozenDateTimeFactory
from holidays import CATHOLIC
import pytest
from homeassistant.components.calendar import (
DOMAIN as CALENDAR_DOMAIN,
SERVICE_GET_EVENTS,
)
from homeassistant.components.holiday.const import CONF_PROVINCE, DOMAIN
from homeassistant.components.holiday.const import (
CONF_CATEGORIES,
CONF_PROVINCE,
DOMAIN,
)
from homeassistant.const import CONF_COUNTRY
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
@ -353,3 +358,76 @@ async def test_language_not_exist(
]
}
}
async def test_categories(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test if there is no next event."""
await hass.config.async_set_time_zone("Europe/Berlin")
zone = await dt_util.async_get_time_zone("Europe/Berlin")
freezer.move_to(datetime(2025, 8, 14, 12, tzinfo=zone))
config_entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_COUNTRY: "DE",
CONF_PROVINCE: "BY",
},
options={
CONF_CATEGORIES: [CATHOLIC],
},
title="Germany",
)
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
response = await hass.services.async_call(
CALENDAR_DOMAIN,
SERVICE_GET_EVENTS,
{
"entity_id": "calendar.germany",
"end_date_time": dt_util.now() + timedelta(days=2),
},
blocking=True,
return_response=True,
)
assert response == {
"calendar.germany": {
"events": [
{
"start": "2025-08-15",
"end": "2025-08-16",
"summary": "Assumption Day",
"location": "Germany",
}
]
}
}
freezer.move_to(datetime(2025, 12, 23, 12, tzinfo=zone))
response = await hass.services.async_call(
CALENDAR_DOMAIN,
SERVICE_GET_EVENTS,
{
"entity_id": "calendar.germany",
"end_date_time": dt_util.now() + timedelta(days=2),
},
blocking=True,
return_response=True,
)
assert response == {
"calendar.germany": {
"events": [
{
"start": "2025-12-25",
"end": "2025-12-26",
"summary": "Christmas Day",
"location": "Germany",
}
]
}
}