diff --git a/homeassistant/components/workday/binary_sensor.py b/homeassistant/components/workday/binary_sensor.py index 533bc77e27c..fc726d56f04 100644 --- a/homeassistant/components/workday/binary_sensor.py +++ b/homeassistant/components/workday/binary_sensor.py @@ -103,7 +103,20 @@ def setup_platform(hass, config, add_entities, discovery_info=None): # Remove holidays try: 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: _LOGGER.debug("No holidays to remove or invalid holidays") diff --git a/tests/components/workday/test_binary_sensor.py b/tests/components/workday/test_binary_sensor.py index 3ec17c3e6d3..f8ab8794c0d 100644 --- a/tests/components/workday/test_binary_sensor.py +++ b/tests/components/workday/test_binary_sensor.py @@ -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 = { "binary_sensor": {"platform": "workday", "country": "DE", "days_offset": 1} } @@ -320,3 +330,17 @@ class TestWorkdaySetup: entity = self.hass.states.get("binary_sensor.workday_sensor") 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"