mirror of
https://github.com/home-assistant/core.git
synced 2025-09-04 04:11:37 +02:00
Move holiday object to runtime data in workday (#149122)
This commit is contained in:
@@ -2,103 +2,72 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from functools import partial
|
from datetime import timedelta
|
||||||
|
from typing import cast
|
||||||
|
|
||||||
from holidays import HolidayBase, country_holidays
|
from holidays import DateLike, HolidayBase
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_COUNTRY, CONF_LANGUAGE
|
from homeassistant.const import CONF_COUNTRY, CONF_LANGUAGE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryError
|
from homeassistant.util import dt as dt_util
|
||||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
|
||||||
from homeassistant.setup import SetupPhases, async_pause_setup
|
|
||||||
|
|
||||||
from .const import CONF_PROVINCE, DOMAIN, PLATFORMS
|
from .const import (
|
||||||
|
CONF_ADD_HOLIDAYS,
|
||||||
|
CONF_CATEGORY,
|
||||||
|
CONF_OFFSET,
|
||||||
|
CONF_PROVINCE,
|
||||||
|
CONF_REMOVE_HOLIDAYS,
|
||||||
|
LOGGER,
|
||||||
|
PLATFORMS,
|
||||||
|
)
|
||||||
|
from .util import (
|
||||||
|
add_remove_custom_holidays,
|
||||||
|
async_validate_country_and_province,
|
||||||
|
get_holidays_object,
|
||||||
|
validate_dates,
|
||||||
|
)
|
||||||
|
|
||||||
|
type WorkdayConfigEntry = ConfigEntry[HolidayBase]
|
||||||
|
|
||||||
|
|
||||||
async def _async_validate_country_and_province(
|
async def async_setup_entry(hass: HomeAssistant, entry: WorkdayConfigEntry) -> bool:
|
||||||
hass: HomeAssistant, entry: ConfigEntry, country: str | None, province: str | None
|
|
||||||
) -> None:
|
|
||||||
"""Validate country and province."""
|
|
||||||
|
|
||||||
if not country:
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):
|
|
||||||
# import executor job is used here because multiple integrations use
|
|
||||||
# the holidays library and it is not thread safe to import it in parallel
|
|
||||||
# https://github.com/python/cpython/issues/83065
|
|
||||||
await hass.async_add_import_executor_job(country_holidays, country)
|
|
||||||
except NotImplementedError as ex:
|
|
||||||
async_create_issue(
|
|
||||||
hass,
|
|
||||||
DOMAIN,
|
|
||||||
"bad_country",
|
|
||||||
is_fixable=True,
|
|
||||||
is_persistent=False,
|
|
||||||
severity=IssueSeverity.ERROR,
|
|
||||||
translation_key="bad_country",
|
|
||||||
translation_placeholders={"title": entry.title},
|
|
||||||
data={"entry_id": entry.entry_id, "country": None},
|
|
||||||
)
|
|
||||||
raise ConfigEntryError(f"Selected country {country} is not valid") from ex
|
|
||||||
|
|
||||||
if not province:
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):
|
|
||||||
# import executor job is used here because multiple integrations use
|
|
||||||
# the holidays library and it is not thread safe to import it in parallel
|
|
||||||
# https://github.com/python/cpython/issues/83065
|
|
||||||
await hass.async_add_import_executor_job(
|
|
||||||
partial(country_holidays, country, subdiv=province)
|
|
||||||
)
|
|
||||||
except NotImplementedError as ex:
|
|
||||||
async_create_issue(
|
|
||||||
hass,
|
|
||||||
DOMAIN,
|
|
||||||
"bad_province",
|
|
||||||
is_fixable=True,
|
|
||||||
is_persistent=False,
|
|
||||||
severity=IssueSeverity.ERROR,
|
|
||||||
translation_key="bad_province",
|
|
||||||
translation_placeholders={
|
|
||||||
CONF_COUNTRY: country,
|
|
||||||
"title": entry.title,
|
|
||||||
},
|
|
||||||
data={"entry_id": entry.entry_id, "country": country},
|
|
||||||
)
|
|
||||||
raise ConfigEntryError(
|
|
||||||
f"Selected province {province} for country {country} is not valid"
|
|
||||||
) from ex
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
||||||
"""Set up Workday from a config entry."""
|
"""Set up Workday from a config entry."""
|
||||||
|
|
||||||
|
calc_add_holidays = cast(
|
||||||
|
list[DateLike], validate_dates(entry.options[CONF_ADD_HOLIDAYS])
|
||||||
|
)
|
||||||
|
calc_remove_holidays: list[str] = validate_dates(
|
||||||
|
entry.options[CONF_REMOVE_HOLIDAYS]
|
||||||
|
)
|
||||||
|
categories: list[str] | None = entry.options.get(CONF_CATEGORY)
|
||||||
country: str | None = entry.options.get(CONF_COUNTRY)
|
country: str | None = entry.options.get(CONF_COUNTRY)
|
||||||
|
days_offset: int = int(entry.options[CONF_OFFSET])
|
||||||
|
language: str | None = entry.options.get(CONF_LANGUAGE)
|
||||||
province: str | None = entry.options.get(CONF_PROVINCE)
|
province: str | None = entry.options.get(CONF_PROVINCE)
|
||||||
|
year: int = (dt_util.now() + timedelta(days=days_offset)).year
|
||||||
|
|
||||||
await _async_validate_country_and_province(hass, entry, country, province)
|
await async_validate_country_and_province(hass, entry, country, province)
|
||||||
|
|
||||||
if country and CONF_LANGUAGE not in entry.options:
|
entry.runtime_data = await hass.async_add_executor_job(
|
||||||
with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):
|
get_holidays_object, country, province, year, language, categories
|
||||||
# import executor job is used here because multiple integrations use
|
)
|
||||||
# the holidays library and it is not thread safe to import it in parallel
|
|
||||||
# https://github.com/python/cpython/issues/83065
|
add_remove_custom_holidays(
|
||||||
cls: HolidayBase = await hass.async_add_import_executor_job(
|
hass, entry, country, calc_add_holidays, calc_remove_holidays
|
||||||
partial(country_holidays, country, subdiv=province)
|
)
|
||||||
)
|
|
||||||
default_language = cls.default_language
|
LOGGER.debug("Found the following holidays for your configuration:")
|
||||||
new_options = entry.options.copy()
|
for holiday_date, name in sorted(entry.runtime_data.items()):
|
||||||
new_options[CONF_LANGUAGE] = default_language
|
# Make explicit str variable to avoid "Incompatible types in assignment"
|
||||||
hass.config_entries.async_update_entry(entry, options=new_options)
|
_holiday_string = holiday_date.strftime("%Y-%m-%d")
|
||||||
|
LOGGER.debug("%s %s", _holiday_string, name)
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: WorkdayConfigEntry) -> bool:
|
||||||
"""Unload Workday config entry."""
|
"""Unload Workday config entry."""
|
||||||
|
|
||||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
@@ -5,17 +5,11 @@ from __future__ import annotations
|
|||||||
from datetime import date, datetime, timedelta
|
from datetime import date, datetime, timedelta
|
||||||
from typing import Final
|
from typing import Final
|
||||||
|
|
||||||
from holidays import (
|
from holidays import HolidayBase, __version__ as python_holidays_version
|
||||||
PUBLIC,
|
|
||||||
HolidayBase,
|
|
||||||
__version__ as python_holidays_version,
|
|
||||||
country_holidays,
|
|
||||||
)
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.const import CONF_NAME
|
||||||
from homeassistant.const import CONF_COUNTRY, CONF_LANGUAGE, CONF_NAME
|
|
||||||
from homeassistant.core import (
|
from homeassistant.core import (
|
||||||
CALLBACK_TYPE,
|
CALLBACK_TYPE,
|
||||||
HomeAssistant,
|
HomeAssistant,
|
||||||
@@ -30,221 +24,26 @@ from homeassistant.helpers.entity_platform import (
|
|||||||
async_get_current_platform,
|
async_get_current_platform,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.event import async_track_point_in_utc_time
|
from homeassistant.helpers.event import async_track_point_in_utc_time
|
||||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
from homeassistant.util import dt as dt_util
|
||||||
from homeassistant.util import dt as dt_util, slugify
|
|
||||||
|
|
||||||
from .const import (
|
from . import WorkdayConfigEntry
|
||||||
ALLOWED_DAYS,
|
from .const import ALLOWED_DAYS, CONF_EXCLUDES, CONF_OFFSET, CONF_WORKDAYS, DOMAIN
|
||||||
CONF_ADD_HOLIDAYS,
|
|
||||||
CONF_CATEGORY,
|
|
||||||
CONF_EXCLUDES,
|
|
||||||
CONF_OFFSET,
|
|
||||||
CONF_PROVINCE,
|
|
||||||
CONF_REMOVE_HOLIDAYS,
|
|
||||||
CONF_WORKDAYS,
|
|
||||||
DOMAIN,
|
|
||||||
LOGGER,
|
|
||||||
)
|
|
||||||
|
|
||||||
SERVICE_CHECK_DATE: Final = "check_date"
|
SERVICE_CHECK_DATE: Final = "check_date"
|
||||||
CHECK_DATE: Final = "check_date"
|
CHECK_DATE: Final = "check_date"
|
||||||
|
|
||||||
|
|
||||||
def validate_dates(holiday_list: list[str]) -> list[str]:
|
|
||||||
"""Validate and adds to list of dates to add or remove."""
|
|
||||||
calc_holidays: list[str] = []
|
|
||||||
for add_date in holiday_list:
|
|
||||||
if add_date.find(",") > 0:
|
|
||||||
dates = add_date.split(",", maxsplit=1)
|
|
||||||
d1 = dt_util.parse_date(dates[0])
|
|
||||||
d2 = dt_util.parse_date(dates[1])
|
|
||||||
if d1 is None or d2 is None:
|
|
||||||
LOGGER.error("Incorrect dates in date range: %s", add_date)
|
|
||||||
continue
|
|
||||||
_range: timedelta = d2 - d1
|
|
||||||
for i in range(_range.days + 1):
|
|
||||||
day: date = d1 + timedelta(days=i)
|
|
||||||
calc_holidays.append(day.strftime("%Y-%m-%d"))
|
|
||||||
continue
|
|
||||||
calc_holidays.append(add_date)
|
|
||||||
return calc_holidays
|
|
||||||
|
|
||||||
|
|
||||||
def _get_obj_holidays(
|
|
||||||
country: str | None,
|
|
||||||
province: str | None,
|
|
||||||
year: int,
|
|
||||||
language: str | None,
|
|
||||||
categories: list[str] | None,
|
|
||||||
) -> HolidayBase:
|
|
||||||
"""Get the object for the requested country and year."""
|
|
||||||
if not country:
|
|
||||||
return HolidayBase()
|
|
||||||
|
|
||||||
set_categories = None
|
|
||||||
if categories:
|
|
||||||
category_list = [PUBLIC]
|
|
||||||
category_list.extend(categories)
|
|
||||||
set_categories = tuple(category_list)
|
|
||||||
|
|
||||||
obj_holidays: HolidayBase = country_holidays(
|
|
||||||
country,
|
|
||||||
subdiv=province,
|
|
||||||
years=[year, year + 1],
|
|
||||||
language=language,
|
|
||||||
categories=set_categories,
|
|
||||||
)
|
|
||||||
|
|
||||||
supported_languages = obj_holidays.supported_languages
|
|
||||||
default_language = obj_holidays.default_language
|
|
||||||
|
|
||||||
if default_language and not language:
|
|
||||||
# If no language is set, use the default language
|
|
||||||
LOGGER.debug("Changing language from None to %s", default_language)
|
|
||||||
return country_holidays( # Return default if no language
|
|
||||||
country,
|
|
||||||
subdiv=province,
|
|
||||||
years=year,
|
|
||||||
language=default_language,
|
|
||||||
categories=set_categories,
|
|
||||||
)
|
|
||||||
|
|
||||||
if (
|
|
||||||
default_language
|
|
||||||
and language
|
|
||||||
and language not in supported_languages
|
|
||||||
and language.startswith("en")
|
|
||||||
):
|
|
||||||
# If language does not match supported languages, use the first English variant
|
|
||||||
if default_language.startswith("en"):
|
|
||||||
LOGGER.debug("Changing language from %s to %s", language, default_language)
|
|
||||||
return country_holidays( # Return default English if default language
|
|
||||||
country,
|
|
||||||
subdiv=province,
|
|
||||||
years=year,
|
|
||||||
language=default_language,
|
|
||||||
categories=set_categories,
|
|
||||||
)
|
|
||||||
for lang in supported_languages:
|
|
||||||
if lang.startswith("en"):
|
|
||||||
LOGGER.debug("Changing language from %s to %s", language, lang)
|
|
||||||
return country_holidays(
|
|
||||||
country,
|
|
||||||
subdiv=province,
|
|
||||||
years=year,
|
|
||||||
language=lang,
|
|
||||||
categories=set_categories,
|
|
||||||
)
|
|
||||||
|
|
||||||
if default_language and language and language not in supported_languages:
|
|
||||||
# If language does not match supported languages, use the default language
|
|
||||||
LOGGER.debug("Changing language from %s to %s", language, default_language)
|
|
||||||
return country_holidays( # Return default English if default language
|
|
||||||
country,
|
|
||||||
subdiv=province,
|
|
||||||
years=year,
|
|
||||||
language=default_language,
|
|
||||||
categories=set_categories,
|
|
||||||
)
|
|
||||||
|
|
||||||
return obj_holidays
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: WorkdayConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Workday sensor."""
|
"""Set up the Workday sensor."""
|
||||||
add_holidays: list[str] = entry.options[CONF_ADD_HOLIDAYS]
|
|
||||||
remove_holidays: list[str] = entry.options[CONF_REMOVE_HOLIDAYS]
|
|
||||||
country: str | None = entry.options.get(CONF_COUNTRY)
|
|
||||||
days_offset: int = int(entry.options[CONF_OFFSET])
|
days_offset: int = int(entry.options[CONF_OFFSET])
|
||||||
excludes: list[str] = entry.options[CONF_EXCLUDES]
|
excludes: list[str] = entry.options[CONF_EXCLUDES]
|
||||||
province: str | None = entry.options.get(CONF_PROVINCE)
|
|
||||||
sensor_name: str = entry.options[CONF_NAME]
|
sensor_name: str = entry.options[CONF_NAME]
|
||||||
workdays: list[str] = entry.options[CONF_WORKDAYS]
|
workdays: list[str] = entry.options[CONF_WORKDAYS]
|
||||||
language: str | None = entry.options.get(CONF_LANGUAGE)
|
obj_holidays = entry.runtime_data
|
||||||
categories: list[str] | None = entry.options.get(CONF_CATEGORY)
|
|
||||||
|
|
||||||
year: int = (dt_util.now() + timedelta(days=days_offset)).year
|
|
||||||
obj_holidays: HolidayBase = await hass.async_add_executor_job(
|
|
||||||
_get_obj_holidays, country, province, year, language, categories
|
|
||||||
)
|
|
||||||
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:
|
|
||||||
obj_holidays.append(calc_add_holidays) # type: ignore[arg-type]
|
|
||||||
except ValueError as error:
|
|
||||||
LOGGER.error("Could not add custom holidays: %s", error)
|
|
||||||
|
|
||||||
# Remove holidays
|
|
||||||
for remove_holiday in calc_remove_holidays:
|
|
||||||
try:
|
|
||||||
# is this formatted as a date?
|
|
||||||
if dt_util.parse_date(remove_holiday):
|
|
||||||
# remove holiday by date
|
|
||||||
removed = obj_holidays.pop(remove_holiday)
|
|
||||||
LOGGER.debug("Removed %s", remove_holiday)
|
|
||||||
else:
|
|
||||||
# remove holiday by name
|
|
||||||
LOGGER.debug("Treating '%s' as named holiday", remove_holiday)
|
|
||||||
removed = obj_holidays.pop_named(remove_holiday)
|
|
||||||
for holiday in removed:
|
|
||||||
LOGGER.debug("Removed %s by name '%s'", holiday, remove_holiday)
|
|
||||||
except KeyError as unmatched:
|
|
||||||
LOGGER.warning("No holiday found matching %s", unmatched)
|
|
||||||
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,
|
|
||||||
DOMAIN,
|
|
||||||
f"bad_named_holiday-{entry.entry_id}-{slugify(remove_holiday)}",
|
|
||||||
is_fixable=True,
|
|
||||||
is_persistent=False,
|
|
||||||
severity=IssueSeverity.WARNING,
|
|
||||||
translation_key="bad_named_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,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
LOGGER.debug("Found the following holidays for your configuration:")
|
|
||||||
for holiday_date, name in sorted(obj_holidays.items()):
|
|
||||||
# Make explicit str variable to avoid "Incompatible types in assignment"
|
|
||||||
_holiday_string = holiday_date.strftime("%Y-%m-%d")
|
|
||||||
LOGGER.debug("%s %s", _holiday_string, name)
|
|
||||||
|
|
||||||
platform = async_get_current_platform()
|
platform = async_get_current_platform()
|
||||||
platform.async_register_entity_service(
|
platform.async_register_entity_service(
|
||||||
|
254
homeassistant/components/workday/util.py
Normal file
254
homeassistant/components/workday/util.py
Normal file
@@ -0,0 +1,254 @@
|
|||||||
|
"""Helpers functions for the Workday component."""
|
||||||
|
|
||||||
|
from datetime import date, timedelta
|
||||||
|
from functools import partial
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from holidays import PUBLIC, DateLike, HolidayBase, country_holidays
|
||||||
|
|
||||||
|
from homeassistant.const import CONF_COUNTRY
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ConfigEntryError
|
||||||
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||||
|
from homeassistant.setup import SetupPhases, async_pause_setup
|
||||||
|
from homeassistant.util import dt as dt_util, slugify
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from . import WorkdayConfigEntry
|
||||||
|
from .const import CONF_REMOVE_HOLIDAYS, DOMAIN, LOGGER
|
||||||
|
|
||||||
|
|
||||||
|
async def async_validate_country_and_province(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entry: "WorkdayConfigEntry",
|
||||||
|
country: str | None,
|
||||||
|
province: str | None,
|
||||||
|
) -> None:
|
||||||
|
"""Validate country and province."""
|
||||||
|
|
||||||
|
if not country:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):
|
||||||
|
# import executor job is used here because multiple integrations use
|
||||||
|
# the holidays library and it is not thread safe to import it in parallel
|
||||||
|
# https://github.com/python/cpython/issues/83065
|
||||||
|
await hass.async_add_import_executor_job(country_holidays, country)
|
||||||
|
except NotImplementedError as ex:
|
||||||
|
async_create_issue(
|
||||||
|
hass,
|
||||||
|
DOMAIN,
|
||||||
|
"bad_country",
|
||||||
|
is_fixable=True,
|
||||||
|
is_persistent=False,
|
||||||
|
severity=IssueSeverity.ERROR,
|
||||||
|
translation_key="bad_country",
|
||||||
|
translation_placeholders={"title": entry.title},
|
||||||
|
data={"entry_id": entry.entry_id, "country": None},
|
||||||
|
)
|
||||||
|
raise ConfigEntryError(f"Selected country {country} is not valid") from ex
|
||||||
|
|
||||||
|
if not province:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):
|
||||||
|
# import executor job is used here because multiple integrations use
|
||||||
|
# the holidays library and it is not thread safe to import it in parallel
|
||||||
|
# https://github.com/python/cpython/issues/83065
|
||||||
|
await hass.async_add_import_executor_job(
|
||||||
|
partial(country_holidays, country, subdiv=province)
|
||||||
|
)
|
||||||
|
except NotImplementedError as ex:
|
||||||
|
async_create_issue(
|
||||||
|
hass,
|
||||||
|
DOMAIN,
|
||||||
|
"bad_province",
|
||||||
|
is_fixable=True,
|
||||||
|
is_persistent=False,
|
||||||
|
severity=IssueSeverity.ERROR,
|
||||||
|
translation_key="bad_province",
|
||||||
|
translation_placeholders={
|
||||||
|
CONF_COUNTRY: country,
|
||||||
|
"title": entry.title,
|
||||||
|
},
|
||||||
|
data={"entry_id": entry.entry_id, "country": country},
|
||||||
|
)
|
||||||
|
raise ConfigEntryError(
|
||||||
|
f"Selected province {province} for country {country} is not valid"
|
||||||
|
) from ex
|
||||||
|
|
||||||
|
|
||||||
|
def validate_dates(holiday_list: list[str]) -> list[str]:
|
||||||
|
"""Validate and add to list of dates to add or remove."""
|
||||||
|
calc_holidays: list[str] = []
|
||||||
|
for add_date in holiday_list:
|
||||||
|
if add_date.find(",") > 0:
|
||||||
|
dates = add_date.split(",", maxsplit=1)
|
||||||
|
d1 = dt_util.parse_date(dates[0])
|
||||||
|
d2 = dt_util.parse_date(dates[1])
|
||||||
|
if d1 is None or d2 is None:
|
||||||
|
LOGGER.error("Incorrect dates in date range: %s", add_date)
|
||||||
|
continue
|
||||||
|
_range: timedelta = d2 - d1
|
||||||
|
for i in range(_range.days + 1):
|
||||||
|
day: date = d1 + timedelta(days=i)
|
||||||
|
calc_holidays.append(day.strftime("%Y-%m-%d"))
|
||||||
|
continue
|
||||||
|
calc_holidays.append(add_date)
|
||||||
|
return calc_holidays
|
||||||
|
|
||||||
|
|
||||||
|
def get_holidays_object(
|
||||||
|
country: str | None,
|
||||||
|
province: str | None,
|
||||||
|
year: int,
|
||||||
|
language: str | None,
|
||||||
|
categories: list[str] | None,
|
||||||
|
) -> HolidayBase:
|
||||||
|
"""Get the object for the requested country and year."""
|
||||||
|
if not country:
|
||||||
|
return HolidayBase()
|
||||||
|
|
||||||
|
set_categories = None
|
||||||
|
if categories:
|
||||||
|
category_list = [PUBLIC]
|
||||||
|
category_list.extend(categories)
|
||||||
|
set_categories = tuple(category_list)
|
||||||
|
|
||||||
|
obj_holidays: HolidayBase = country_holidays(
|
||||||
|
country,
|
||||||
|
subdiv=province,
|
||||||
|
years=[year, year + 1],
|
||||||
|
language=language,
|
||||||
|
categories=set_categories,
|
||||||
|
)
|
||||||
|
|
||||||
|
supported_languages = obj_holidays.supported_languages
|
||||||
|
default_language = obj_holidays.default_language
|
||||||
|
|
||||||
|
if default_language and not language:
|
||||||
|
# If no language is set, use the default language
|
||||||
|
LOGGER.debug("Changing language from None to %s", default_language)
|
||||||
|
return country_holidays( # Return default if no language
|
||||||
|
country,
|
||||||
|
subdiv=province,
|
||||||
|
years=year,
|
||||||
|
language=default_language,
|
||||||
|
categories=set_categories,
|
||||||
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
default_language
|
||||||
|
and language
|
||||||
|
and language not in supported_languages
|
||||||
|
and language.startswith("en")
|
||||||
|
):
|
||||||
|
# If language does not match supported languages, use the first English variant
|
||||||
|
if default_language.startswith("en"):
|
||||||
|
LOGGER.debug("Changing language from %s to %s", language, default_language)
|
||||||
|
return country_holidays( # Return default English if default language
|
||||||
|
country,
|
||||||
|
subdiv=province,
|
||||||
|
years=year,
|
||||||
|
language=default_language,
|
||||||
|
categories=set_categories,
|
||||||
|
)
|
||||||
|
for lang in supported_languages:
|
||||||
|
if lang.startswith("en"):
|
||||||
|
LOGGER.debug("Changing language from %s to %s", language, lang)
|
||||||
|
return country_holidays(
|
||||||
|
country,
|
||||||
|
subdiv=province,
|
||||||
|
years=year,
|
||||||
|
language=lang,
|
||||||
|
categories=set_categories,
|
||||||
|
)
|
||||||
|
|
||||||
|
if default_language and language and language not in supported_languages:
|
||||||
|
# If language does not match supported languages, use the default language
|
||||||
|
LOGGER.debug("Changing language from %s to %s", language, default_language)
|
||||||
|
return country_holidays( # Return default English if default language
|
||||||
|
country,
|
||||||
|
subdiv=province,
|
||||||
|
years=year,
|
||||||
|
language=default_language,
|
||||||
|
categories=set_categories,
|
||||||
|
)
|
||||||
|
|
||||||
|
return obj_holidays
|
||||||
|
|
||||||
|
|
||||||
|
def add_remove_custom_holidays(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entry: "WorkdayConfigEntry",
|
||||||
|
country: str | None,
|
||||||
|
calc_add_holidays: list[DateLike],
|
||||||
|
calc_remove_holidays: list[str],
|
||||||
|
) -> None:
|
||||||
|
"""Add or remove custom holidays."""
|
||||||
|
next_year = dt_util.now().year + 1
|
||||||
|
|
||||||
|
# Add custom holidays
|
||||||
|
try:
|
||||||
|
entry.runtime_data.append(calc_add_holidays)
|
||||||
|
except ValueError as error:
|
||||||
|
LOGGER.error("Could not add custom holidays: %s", error)
|
||||||
|
|
||||||
|
# Remove custom holidays
|
||||||
|
for remove_holiday in calc_remove_holidays:
|
||||||
|
try:
|
||||||
|
# is this formatted as a date?
|
||||||
|
if dt_util.parse_date(remove_holiday):
|
||||||
|
# remove holiday by date
|
||||||
|
removed = entry.runtime_data.pop(remove_holiday)
|
||||||
|
LOGGER.debug("Removed %s", remove_holiday)
|
||||||
|
else:
|
||||||
|
# remove holiday by name
|
||||||
|
LOGGER.debug("Treating '%s' as named holiday", remove_holiday)
|
||||||
|
removed = entry.runtime_data.pop_named(remove_holiday)
|
||||||
|
for holiday in removed:
|
||||||
|
LOGGER.debug("Removed %s by name '%s'", holiday, remove_holiday)
|
||||||
|
except KeyError as unmatched:
|
||||||
|
LOGGER.warning("No holiday found matching %s", unmatched)
|
||||||
|
if _date := dt_util.parse_date(remove_holiday):
|
||||||
|
if _date.year <= next_year:
|
||||||
|
# Only check and raise issues for max 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,
|
||||||
|
DOMAIN,
|
||||||
|
f"bad_named_holiday-{entry.entry_id}-{slugify(remove_holiday)}",
|
||||||
|
is_fixable=True,
|
||||||
|
is_persistent=False,
|
||||||
|
severity=IssueSeverity.WARNING,
|
||||||
|
translation_key="bad_named_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,
|
||||||
|
},
|
||||||
|
)
|
Reference in New Issue
Block a user