Add function to remove holidays from workday sensor (#41988)

This commit is contained in:
Peter Epley 2020-11-10 04:26:55 -05:00 committed by GitHub
parent 1366354725
commit b099726854
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View file

@ -20,6 +20,7 @@ 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"]
@ -60,6 +61,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
cv.ensure_list, [vol.In(ALLOWED_DAYS)]
),
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):
"""Set up the Workday sensor."""
add_holidays = config.get(CONF_ADD_HOLIDAYS)
remove_holidays = config.get(CONF_REMOVE_HOLIDAYS)
country = config[CONF_COUNTRY]
days_offset = config[CONF_OFFSET]
excludes = config[CONF_EXCLUDES]
@ -96,6 +99,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
except TypeError:
_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:")
for date, name in sorted(obj_holidays.items()):
_LOGGER.debug("%s %s", date, name)

View file

@ -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 = {
"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(7) == "holiday"
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"