From 481bf2694b3761aee5810e3a247bb726874bdca4 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Tue, 10 Jun 2025 19:28:48 +0200 Subject: [PATCH] Fix incorrect categories handling in holiday (#146470) --- homeassistant/components/holiday/calendar.py | 16 ++-- tests/components/holiday/test_calendar.py | 80 +++++++++++++++++++- 2 files changed, 87 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/holiday/calendar.py b/homeassistant/components/holiday/calendar.py index 1c01319129b..c5b67b7d555 100644 --- a/homeassistant/components/holiday/calendar.py +++ b/homeassistant/components/holiday/calendar.py @@ -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 ) diff --git a/tests/components/holiday/test_calendar.py b/tests/components/holiday/test_calendar.py index 6733d38442b..463f8645647 100644 --- a/tests/components/holiday/test_calendar.py +++ b/tests/components/holiday/test_calendar.py @@ -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", + } + ] + } + }