mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
parent
dd4a3089ec
commit
b1370cbd42
@ -2,7 +2,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import date, timedelta
|
from datetime import date, timedelta
|
||||||
import logging
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import holidays
|
import holidays
|
||||||
@ -13,31 +12,28 @@ from homeassistant.components.binary_sensor import (
|
|||||||
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_NAME, WEEKDAYS
|
from homeassistant.const import CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
from homeassistant.util import dt
|
from homeassistant.util import dt
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
from .const import (
|
||||||
|
ALLOWED_DAYS,
|
||||||
ALLOWED_DAYS = WEEKDAYS + ["holiday"]
|
CONF_ADD_HOLIDAYS,
|
||||||
|
CONF_COUNTRY,
|
||||||
CONF_COUNTRY = "country"
|
CONF_EXCLUDES,
|
||||||
CONF_PROVINCE = "province"
|
CONF_OFFSET,
|
||||||
CONF_WORKDAYS = "workdays"
|
CONF_PROVINCE,
|
||||||
CONF_EXCLUDES = "excludes"
|
CONF_REMOVE_HOLIDAYS,
|
||||||
CONF_OFFSET = "days_offset"
|
CONF_WORKDAYS,
|
||||||
CONF_ADD_HOLIDAYS = "add_holidays"
|
DEFAULT_EXCLUDES,
|
||||||
CONF_REMOVE_HOLIDAYS = "remove_holidays"
|
DEFAULT_NAME,
|
||||||
|
DEFAULT_OFFSET,
|
||||||
# By default, Monday - Friday are workdays
|
DEFAULT_WORKDAYS,
|
||||||
DEFAULT_WORKDAYS = ["mon", "tue", "wed", "thu", "fri"]
|
LOGGER,
|
||||||
# By default, public holidays, Saturdays and Sundays are excluded from workdays
|
)
|
||||||
DEFAULT_EXCLUDES = ["sat", "sun", "holiday"]
|
|
||||||
DEFAULT_NAME = "Workday Sensor"
|
|
||||||
DEFAULT_OFFSET = 0
|
|
||||||
|
|
||||||
|
|
||||||
def valid_country(value: Any) -> str:
|
def valid_country(value: Any) -> str:
|
||||||
@ -106,14 +102,14 @@ def setup_platform(
|
|||||||
):
|
):
|
||||||
obj_holidays = getattr(holidays, country)(subdiv=province, years=year)
|
obj_holidays = getattr(holidays, country)(subdiv=province, years=year)
|
||||||
else:
|
else:
|
||||||
_LOGGER.error("There is no subdivision %s in country %s", province, country)
|
LOGGER.error("There is no subdivision %s in country %s", province, country)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Add custom holidays
|
# Add custom holidays
|
||||||
try:
|
try:
|
||||||
obj_holidays.append(add_holidays)
|
obj_holidays.append(add_holidays)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
_LOGGER.debug("No custom holidays or invalid holidays")
|
LOGGER.debug("No custom holidays or invalid holidays")
|
||||||
|
|
||||||
# Remove holidays
|
# Remove holidays
|
||||||
try:
|
try:
|
||||||
@ -123,25 +119,23 @@ def setup_platform(
|
|||||||
if dt.parse_date(remove_holiday):
|
if dt.parse_date(remove_holiday):
|
||||||
# remove holiday by date
|
# remove holiday by date
|
||||||
removed = obj_holidays.pop(remove_holiday)
|
removed = obj_holidays.pop(remove_holiday)
|
||||||
_LOGGER.debug("Removed %s", remove_holiday)
|
LOGGER.debug("Removed %s", remove_holiday)
|
||||||
else:
|
else:
|
||||||
# remove holiday by name
|
# remove holiday by name
|
||||||
_LOGGER.debug("Treating '%s' as named holiday", remove_holiday)
|
LOGGER.debug("Treating '%s' as named holiday", remove_holiday)
|
||||||
removed = obj_holidays.pop_named(remove_holiday)
|
removed = obj_holidays.pop_named(remove_holiday)
|
||||||
for holiday in removed:
|
for holiday in removed:
|
||||||
_LOGGER.debug(
|
LOGGER.debug("Removed %s by name '%s'", holiday, remove_holiday)
|
||||||
"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)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
_LOGGER.debug("No holidays to remove or invalid holidays")
|
LOGGER.debug("No holidays to remove or invalid holidays")
|
||||||
|
|
||||||
_LOGGER.debug("Found the following holidays for your configuration:")
|
LOGGER.debug("Found the following holidays for your configuration:")
|
||||||
for holiday_date, name in sorted(obj_holidays.items()):
|
for holiday_date, name in sorted(obj_holidays.items()):
|
||||||
# Make explicit str variable to avoid "Incompatible types in assignment"
|
# Make explicit str variable to avoid "Incompatible types in assignment"
|
||||||
_holiday_string = holiday_date.strftime("%Y-%m-%d")
|
_holiday_string = holiday_date.strftime("%Y-%m-%d")
|
||||||
_LOGGER.debug("%s %s", _holiday_string, name)
|
LOGGER.debug("%s %s", _holiday_string, name)
|
||||||
|
|
||||||
add_entities(
|
add_entities(
|
||||||
[IsWorkdaySensor(obj_holidays, workdays, excludes, days_offset, sensor_name)],
|
[IsWorkdaySensor(obj_holidays, workdays, excludes, days_offset, sensor_name)],
|
||||||
|
25
homeassistant/components/workday/const.py
Normal file
25
homeassistant/components/workday/const.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
"""Add constants for Workday integration."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from homeassistant.const import WEEKDAYS
|
||||||
|
|
||||||
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
ALLOWED_DAYS = WEEKDAYS + ["holiday"]
|
||||||
|
|
||||||
|
CONF_COUNTRY = "country"
|
||||||
|
CONF_PROVINCE = "province"
|
||||||
|
CONF_WORKDAYS = "workdays"
|
||||||
|
CONF_EXCLUDES = "excludes"
|
||||||
|
CONF_OFFSET = "days_offset"
|
||||||
|
CONF_ADD_HOLIDAYS = "add_holidays"
|
||||||
|
CONF_REMOVE_HOLIDAYS = "remove_holidays"
|
||||||
|
|
||||||
|
# By default, Monday - Friday are workdays
|
||||||
|
DEFAULT_WORKDAYS = ["mon", "tue", "wed", "thu", "fri"]
|
||||||
|
# By default, public holidays, Saturdays and Sundays are excluded from workdays
|
||||||
|
DEFAULT_EXCLUDES = ["sat", "sun", "holiday"]
|
||||||
|
DEFAULT_NAME = "Workday Sensor"
|
||||||
|
DEFAULT_OFFSET = 0
|
Loading…
x
Reference in New Issue
Block a user