Skip to content

Commit

Permalink
Add custom holidays to workday sensor (#21718)
Browse files Browse the repository at this point in the history
* Add custom holidays to workday sensor

* correcting copy/paste errors

* resolve block comment and add default

* resolve line too long

* fixed handling of no custom holidays

* hound fixes

* rerun Travis

* @fabaff requested changes

* spaces v tabs

* Fix validation
  • Loading branch information
epleypa authored and fabaff committed Mar 10, 2019
1 parent bab53a1 commit c888e65
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions homeassistant/components/binary_sensor/workday.py
Expand Up @@ -42,6 +42,7 @@
CONF_WORKDAYS = 'workdays'
CONF_EXCLUDES = 'excludes'
CONF_OFFSET = 'days_offset'
CONF_ADD_HOLIDAYS = 'add_holidays'

# By default, Monday - Friday are workdays
DEFAULT_WORKDAYS = ['mon', 'tue', 'wed', 'thu', 'fri']
Expand All @@ -59,6 +60,7 @@
vol.Optional(CONF_PROVINCE): cv.string,
vol.Optional(CONF_WORKDAYS, default=DEFAULT_WORKDAYS):
vol.All(cv.ensure_list, [vol.In(ALLOWED_DAYS)]),
vol.Optional(CONF_ADD_HOLIDAYS): vol.All(cv.ensure_list, [cv.string]),
})


Expand All @@ -72,6 +74,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
workdays = config.get(CONF_WORKDAYS)
excludes = config.get(CONF_EXCLUDES)
days_offset = config.get(CONF_OFFSET)
add_holidays = config.get(CONF_ADD_HOLIDAYS)

year = (get_date(datetime.today()) + timedelta(days=days_offset)).year
obj_holidays = getattr(holidays, country)(years=year)
Expand All @@ -92,6 +95,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
province, country)
return

# Add custom holidays
try:
obj_holidays.append(add_holidays)
except TypeError:
_LOGGER.debug("No custom holidays 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)
Expand Down

0 comments on commit c888e65

Please sign in to comment.