Skip to content

Commit

Permalink
Workday raise issues only to next year (#126997)
Browse files Browse the repository at this point in the history
* Workday - raise issues only for current and next year

* variable
  • Loading branch information
gjohansson-ST authored and frenck committed Sep 30, 2024
1 parent a68d7c9 commit fc97eb8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 22 deletions.
45 changes: 24 additions & 21 deletions homeassistant/components/workday/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _get_obj_holidays(
obj_holidays: HolidayBase = country_holidays(
country,
subdiv=province,
years=year,
years=[year, year + 1],
language=language,
categories=set_categories,
)
Expand Down Expand Up @@ -129,6 +129,7 @@ async def async_setup_entry(
)
calc_add_holidays: list[str] = validate_dates(add_holidays)
calc_remove_holidays: list[str] = validate_dates(remove_holidays)
next_year = dt_util.now().year + 1

# Add custom holidays
try:
Expand All @@ -152,26 +153,28 @@ async def async_setup_entry(
LOGGER.debug("Removed %s by name '%s'", holiday, remove_holiday)
except KeyError as unmatched:
LOGGER.warning("No holiday found matching %s", unmatched)
if dt_util.parse_date(remove_holiday):
async_create_issue(
hass,
DOMAIN,
f"bad_date_holiday-{entry.entry_id}-{slugify(remove_holiday)}",
is_fixable=True,
is_persistent=False,
severity=IssueSeverity.WARNING,
translation_key="bad_date_holiday",
translation_placeholders={
CONF_COUNTRY: country if country else "-",
"title": entry.title,
CONF_REMOVE_HOLIDAYS: remove_holiday,
},
data={
"entry_id": entry.entry_id,
"country": country,
"named_holiday": remove_holiday,
},
)
if _date := dt_util.parse_date(remove_holiday):
if _date.year <= next_year:
# Only check and raise issues for current and next year
async_create_issue(
hass,
DOMAIN,
f"bad_date_holiday-{entry.entry_id}-{slugify(remove_holiday)}",
is_fixable=True,
is_persistent=False,
severity=IssueSeverity.WARNING,
translation_key="bad_date_holiday",
translation_placeholders={
CONF_COUNTRY: country if country else "-",
"title": entry.title,
CONF_REMOVE_HOLIDAYS: remove_holiday,
},
data={
"entry_id": entry.entry_id,
"country": country,
"named_holiday": remove_holiday,
},
)
else:
async_create_issue(
hass,
Expand Down
25 changes: 25 additions & 0 deletions tests/components/workday/snapshots/test_binary_sensor.ambr
Original file line number Diff line number Diff line change
@@ -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',
}),
})
# ---
41 changes: 40 additions & 1 deletion tests/components/workday/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@

from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion

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.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from homeassistant.util.dt import UTC
Expand Down Expand Up @@ -422,3 +430,34 @@ async def test_optional_category(
state = hass.states.get("binary_sensor.workday_sensor")
assert state is not None
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

0 comments on commit fc97eb8

Please sign in to comment.