mirror of
https://github.com/home-assistant/core.git
synced 2025-07-11 15:27:08 +00:00
Fix incorrect categories handling in holiday (#146470)
This commit is contained in:
parent
5cc9cc3c99
commit
481bf2694b
@ -25,17 +25,12 @@ def _get_obj_holidays_and_language(
|
|||||||
selected_categories: list[str] | None,
|
selected_categories: list[str] | None,
|
||||||
) -> tuple[HolidayBase, str]:
|
) -> tuple[HolidayBase, str]:
|
||||||
"""Get the object for the requested country and year."""
|
"""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(
|
obj_holidays = country_holidays(
|
||||||
country,
|
country,
|
||||||
subdiv=province,
|
subdiv=province,
|
||||||
years={dt_util.now().year, dt_util.now().year + 1},
|
years={dt_util.now().year, dt_util.now().year + 1},
|
||||||
language=language,
|
language=language,
|
||||||
categories=categories,
|
categories=selected_categories,
|
||||||
)
|
)
|
||||||
if language == "en":
|
if language == "en":
|
||||||
for lang in obj_holidays.supported_languages:
|
for lang in obj_holidays.supported_languages:
|
||||||
@ -45,7 +40,7 @@ def _get_obj_holidays_and_language(
|
|||||||
subdiv=province,
|
subdiv=province,
|
||||||
years={dt_util.now().year, dt_util.now().year + 1},
|
years={dt_util.now().year, dt_util.now().year + 1},
|
||||||
language=lang,
|
language=lang,
|
||||||
categories=categories,
|
categories=selected_categories,
|
||||||
)
|
)
|
||||||
language = lang
|
language = lang
|
||||||
break
|
break
|
||||||
@ -59,7 +54,7 @@ def _get_obj_holidays_and_language(
|
|||||||
subdiv=province,
|
subdiv=province,
|
||||||
years={dt_util.now().year, dt_util.now().year + 1},
|
years={dt_util.now().year, dt_util.now().year + 1},
|
||||||
language=default_language,
|
language=default_language,
|
||||||
categories=categories,
|
categories=selected_categories,
|
||||||
)
|
)
|
||||||
language = default_language
|
language = default_language
|
||||||
|
|
||||||
@ -77,6 +72,11 @@ async def async_setup_entry(
|
|||||||
categories: list[str] | None = config_entry.options.get(CONF_CATEGORIES)
|
categories: list[str] | None = config_entry.options.get(CONF_CATEGORIES)
|
||||||
language = hass.config.language
|
language = hass.config.language
|
||||||
|
|
||||||
|
if categories is None:
|
||||||
|
categories = [PUBLIC]
|
||||||
|
else:
|
||||||
|
categories = [PUBLIC, *categories]
|
||||||
|
|
||||||
obj_holidays, language = await hass.async_add_executor_job(
|
obj_holidays, language = await hass.async_add_executor_job(
|
||||||
_get_obj_holidays_and_language, country, province, language, categories
|
_get_obj_holidays_and_language, country, province, language, categories
|
||||||
)
|
)
|
||||||
|
@ -3,13 +3,18 @@
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from freezegun.api import FrozenDateTimeFactory
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
|
from holidays import CATHOLIC
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.calendar import (
|
from homeassistant.components.calendar import (
|
||||||
DOMAIN as CALENDAR_DOMAIN,
|
DOMAIN as CALENDAR_DOMAIN,
|
||||||
SERVICE_GET_EVENTS,
|
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.const import CONF_COUNTRY
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
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",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user