mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 06:47:09 +00:00
Add function to remove holidays from workday sensor (#41988)
This commit is contained in:
parent
1366354725
commit
b099726854
@ -20,6 +20,7 @@ CONF_WORKDAYS = "workdays"
|
|||||||
CONF_EXCLUDES = "excludes"
|
CONF_EXCLUDES = "excludes"
|
||||||
CONF_OFFSET = "days_offset"
|
CONF_OFFSET = "days_offset"
|
||||||
CONF_ADD_HOLIDAYS = "add_holidays"
|
CONF_ADD_HOLIDAYS = "add_holidays"
|
||||||
|
CONF_REMOVE_HOLIDAYS = "remove_holidays"
|
||||||
|
|
||||||
# By default, Monday - Friday are workdays
|
# By default, Monday - Friday are workdays
|
||||||
DEFAULT_WORKDAYS = ["mon", "tue", "wed", "thu", "fri"]
|
DEFAULT_WORKDAYS = ["mon", "tue", "wed", "thu", "fri"]
|
||||||
@ -60,6 +61,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
cv.ensure_list, [vol.In(ALLOWED_DAYS)]
|
cv.ensure_list, [vol.In(ALLOWED_DAYS)]
|
||||||
),
|
),
|
||||||
vol.Optional(CONF_ADD_HOLIDAYS): vol.All(cv.ensure_list, [cv.string]),
|
vol.Optional(CONF_ADD_HOLIDAYS): vol.All(cv.ensure_list, [cv.string]),
|
||||||
|
vol.Optional(CONF_REMOVE_HOLIDAYS): vol.All(cv.ensure_list, [cv.string]),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -67,6 +69,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the Workday sensor."""
|
"""Set up the Workday sensor."""
|
||||||
add_holidays = config.get(CONF_ADD_HOLIDAYS)
|
add_holidays = config.get(CONF_ADD_HOLIDAYS)
|
||||||
|
remove_holidays = config.get(CONF_REMOVE_HOLIDAYS)
|
||||||
country = config[CONF_COUNTRY]
|
country = config[CONF_COUNTRY]
|
||||||
days_offset = config[CONF_OFFSET]
|
days_offset = config[CONF_OFFSET]
|
||||||
excludes = config[CONF_EXCLUDES]
|
excludes = config[CONF_EXCLUDES]
|
||||||
@ -96,6 +99,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
_LOGGER.debug("No custom holidays or invalid holidays")
|
_LOGGER.debug("No custom holidays or invalid holidays")
|
||||||
|
|
||||||
|
# Remove holidays
|
||||||
|
try:
|
||||||
|
for date in remove_holidays:
|
||||||
|
obj_holidays.pop(date)
|
||||||
|
except TypeError:
|
||||||
|
_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 date, name in sorted(obj_holidays.items()):
|
for date, name in sorted(obj_holidays.items()):
|
||||||
_LOGGER.debug("%s %s", date, name)
|
_LOGGER.debug("%s %s", date, name)
|
||||||
|
@ -75,6 +75,16 @@ class TestWorkdaySetup:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.config_remove_holidays = {
|
||||||
|
"binary_sensor": {
|
||||||
|
"platform": "workday",
|
||||||
|
"country": "US",
|
||||||
|
"workdays": ["mon", "tue", "wed", "thu", "fri"],
|
||||||
|
"excludes": ["sat", "sun", "holiday"],
|
||||||
|
"remove_holidays": ["2020-12-25", "2020-11-26"],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
self.config_tomorrow = {
|
self.config_tomorrow = {
|
||||||
"binary_sensor": {"platform": "workday", "country": "DE", "days_offset": 1}
|
"binary_sensor": {"platform": "workday", "country": "DE", "days_offset": 1}
|
||||||
}
|
}
|
||||||
@ -298,3 +308,15 @@ class TestWorkdaySetup:
|
|||||||
assert binary_sensor.day_to_string(1) == "tue"
|
assert binary_sensor.day_to_string(1) == "tue"
|
||||||
assert binary_sensor.day_to_string(7) == "holiday"
|
assert binary_sensor.day_to_string(7) == "holiday"
|
||||||
assert binary_sensor.day_to_string(8) is None
|
assert binary_sensor.day_to_string(8) is None
|
||||||
|
|
||||||
|
# Freeze time to test Fri, but remove holiday - December 25, 2020
|
||||||
|
@patch(FUNCTION_PATH, return_value=date(2020, 12, 25))
|
||||||
|
def test_config_remove_holidays_xmas(self, mock_date):
|
||||||
|
"""Test if removed holidays are reported correctly."""
|
||||||
|
with assert_setup_component(1, "binary_sensor"):
|
||||||
|
setup_component(self.hass, "binary_sensor", self.config_remove_holidays)
|
||||||
|
|
||||||
|
self.hass.start()
|
||||||
|
|
||||||
|
entity = self.hass.states.get("binary_sensor.workday_sensor")
|
||||||
|
assert entity.state == "on"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user