Add custom holidays to workday sensor (#21718)
* 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 validationpull/21913/head
parent
bab53a1c94
commit
c888e65f11
|
@ -42,6 +42,7 @@ CONF_PROVINCE = 'province'
|
||||||
CONF_WORKDAYS = 'workdays'
|
CONF_WORKDAYS = 'workdays'
|
||||||
CONF_EXCLUDES = 'excludes'
|
CONF_EXCLUDES = 'excludes'
|
||||||
CONF_OFFSET = 'days_offset'
|
CONF_OFFSET = 'days_offset'
|
||||||
|
CONF_ADD_HOLIDAYS = 'add_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']
|
||||||
|
@ -59,6 +60,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Optional(CONF_PROVINCE): cv.string,
|
vol.Optional(CONF_PROVINCE): cv.string,
|
||||||
vol.Optional(CONF_WORKDAYS, default=DEFAULT_WORKDAYS):
|
vol.Optional(CONF_WORKDAYS, default=DEFAULT_WORKDAYS):
|
||||||
vol.All(cv.ensure_list, [vol.In(ALLOWED_DAYS)]),
|
vol.All(cv.ensure_list, [vol.In(ALLOWED_DAYS)]),
|
||||||
|
vol.Optional(CONF_ADD_HOLIDAYS): vol.All(cv.ensure_list, [cv.string]),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@ -72,6 +74,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
workdays = config.get(CONF_WORKDAYS)
|
workdays = config.get(CONF_WORKDAYS)
|
||||||
excludes = config.get(CONF_EXCLUDES)
|
excludes = config.get(CONF_EXCLUDES)
|
||||||
days_offset = config.get(CONF_OFFSET)
|
days_offset = config.get(CONF_OFFSET)
|
||||||
|
add_holidays = config.get(CONF_ADD_HOLIDAYS)
|
||||||
|
|
||||||
year = (get_date(datetime.today()) + timedelta(days=days_offset)).year
|
year = (get_date(datetime.today()) + timedelta(days=days_offset)).year
|
||||||
obj_holidays = getattr(holidays, country)(years=year)
|
obj_holidays = getattr(holidays, country)(years=year)
|
||||||
|
@ -92,6 +95,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
province, country)
|
province, country)
|
||||||
return
|
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:")
|
_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)
|
||||||
|
|
Loading…
Reference in New Issue