Workday raise issues only to next year (#126997)

* Workday - raise issues only for current and next year

* variable
This commit is contained in:
G Johansson 2024-09-30 13:08:58 +02:00 committed by GitHub
parent a44bf164e5
commit 92a6f231a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 89 additions and 22 deletions

View File

@ -90,7 +90,7 @@ def _get_obj_holidays(
obj_holidays: HolidayBase = country_holidays( obj_holidays: HolidayBase = country_holidays(
country, country,
subdiv=province, subdiv=province,
years=year, years=[year, year + 1],
language=language, language=language,
categories=set_categories, categories=set_categories,
) )
@ -129,6 +129,7 @@ async def async_setup_entry(
) )
calc_add_holidays: list[str] = validate_dates(add_holidays) calc_add_holidays: list[str] = validate_dates(add_holidays)
calc_remove_holidays: list[str] = validate_dates(remove_holidays) calc_remove_holidays: list[str] = validate_dates(remove_holidays)
next_year = dt_util.now().year + 1
# Add custom holidays # Add custom holidays
try: try:
@ -152,26 +153,28 @@ async def async_setup_entry(
LOGGER.debug("Removed %s by name '%s'", holiday, remove_holiday) LOGGER.debug("Removed %s by name '%s'", holiday, remove_holiday)
except KeyError as unmatched: except KeyError as unmatched:
LOGGER.warning("No holiday found matching %s", unmatched) LOGGER.warning("No holiday found matching %s", unmatched)
if dt_util.parse_date(remove_holiday): if _date := dt_util.parse_date(remove_holiday):
async_create_issue( if _date.year <= next_year:
hass, # Only check and raise issues for current and next year
DOMAIN, async_create_issue(
f"bad_date_holiday-{entry.entry_id}-{slugify(remove_holiday)}", hass,
is_fixable=True, DOMAIN,
is_persistent=False, f"bad_date_holiday-{entry.entry_id}-{slugify(remove_holiday)}",
severity=IssueSeverity.WARNING, is_fixable=True,
translation_key="bad_date_holiday", is_persistent=False,
translation_placeholders={ severity=IssueSeverity.WARNING,
CONF_COUNTRY: country if country else "-", translation_key="bad_date_holiday",
"title": entry.title, translation_placeholders={
CONF_REMOVE_HOLIDAYS: remove_holiday, CONF_COUNTRY: country if country else "-",
}, "title": entry.title,
data={ CONF_REMOVE_HOLIDAYS: remove_holiday,
"entry_id": entry.entry_id, },
"country": country, data={
"named_holiday": remove_holiday, "entry_id": entry.entry_id,
}, "country": country,
) "named_holiday": remove_holiday,
},
)
else: else:
async_create_issue( async_create_issue(
hass, hass,

View File

@ -0,0 +1,25 @@
# serializer version: 1
# name: test_only_repairs_for_current_next_year
dict({
tuple(
'workday',
'bad_date_holiday-1-2024_08_15',
): IssueRegistryItemSnapshot({
'created': <ANY>,
'dismissed_version': None,
'domain': 'workday',
'is_persistent': False,
'issue_id': 'bad_date_holiday-1-2024_08_15',
}),
tuple(
'workday',
'bad_date_holiday-1-2025_08_15',
): IssueRegistryItemSnapshot({
'created': <ANY>,
'dismissed_version': None,
'domain': 'workday',
'is_persistent': False,
'issue_id': 'bad_date_holiday-1-2025_08_15',
}),
})
# ---

View File

@ -5,10 +5,18 @@ from typing import Any
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.workday.binary_sensor import SERVICE_CHECK_DATE from homeassistant.components.workday.binary_sensor import SERVICE_CHECK_DATE
from homeassistant.components.workday.const import DOMAIN from homeassistant.components.workday.const import (
DEFAULT_EXCLUDES,
DEFAULT_NAME,
DEFAULT_OFFSET,
DEFAULT_WORKDAYS,
DOMAIN,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from homeassistant.util.dt import UTC from homeassistant.util.dt import UTC
@ -422,3 +430,34 @@ async def test_optional_category(
state = hass.states.get("binary_sensor.workday_sensor") state = hass.states.get("binary_sensor.workday_sensor")
assert state is not None assert state is not None
assert state.state == end_state assert state.state == end_state
async def test_only_repairs_for_current_next_year(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
issue_registry: ir.IssueRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test only repairs are raised for current and next year."""
freezer.move_to(datetime(2024, 8, 15, 12, tzinfo=UTC))
remove_dates = [
# None of these dates are holidays
"2024-08-15", # Creates issue
"2025-08-15", # Creates issue
"2026-08-15", # No issue
]
config = {
"name": DEFAULT_NAME,
"country": "DE",
"province": "BW",
"excludes": DEFAULT_EXCLUDES,
"days_offset": DEFAULT_OFFSET,
"workdays": DEFAULT_WORKDAYS,
"add_holidays": [],
"remove_holidays": remove_dates,
"language": "de",
}
await init_integration(hass, config)
assert len(issue_registry.issues) == 2
assert issue_registry.issues == snapshot