Allow removing workday holidays by name (#52700)

pull/53580/head
Matthew Gottlieb 2021-07-27 14:28:04 -04:00 committed by GitHub
parent 54a3c1a217
commit 37c841956f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 1 deletions

View File

@ -103,7 +103,20 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
# Remove holidays # Remove holidays
try: try:
for date in remove_holidays: for date in remove_holidays:
obj_holidays.pop(date) try:
# is this formatted as a date?
if dt.parse_date(date):
# remove holiday by date
removed = obj_holidays.pop(date)
_LOGGER.debug("Removed %s", date)
else:
# remove holiday by name
_LOGGER.debug("Treating '%s' as named holiday", date)
removed = obj_holidays.pop_named(date)
for holiday in removed:
_LOGGER.debug("Removed %s by name '%s'", holiday, date)
except KeyError as 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")

View File

@ -85,6 +85,16 @@ class TestWorkdaySetup:
} }
} }
self.config_remove_named_holidays = {
"binary_sensor": {
"platform": "workday",
"country": "US",
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"excludes": ["sat", "sun", "holiday"],
"remove_holidays": ["Not a Holiday", "Christmas", "Thanksgiving"],
}
}
self.config_tomorrow = { self.config_tomorrow = {
"binary_sensor": {"platform": "workday", "country": "DE", "days_offset": 1} "binary_sensor": {"platform": "workday", "country": "DE", "days_offset": 1}
} }
@ -320,3 +330,17 @@ class TestWorkdaySetup:
entity = self.hass.states.get("binary_sensor.workday_sensor") entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "on" assert entity.state == "on"
# Freeze time to test Fri, but remove holiday by name - Christmas
@patch(FUNCTION_PATH, return_value=date(2020, 12, 25))
def test_config_remove_named_holidays_xmas(self, mock_date):
"""Test if removed by name holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(
self.hass, "binary_sensor", self.config_remove_named_holidays
)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "on"