Add CalDAV upcoming appointments period option (#34584)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
nicx 2020-06-03 18:54:51 +02:00 committed by GitHub
parent 2b5bb8dac0
commit 9871efd52f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,6 +32,7 @@ CONF_CALENDARS = "calendars"
CONF_CUSTOM_CALENDARS = "custom_calendars" CONF_CUSTOM_CALENDARS = "custom_calendars"
CONF_CALENDAR = "calendar" CONF_CALENDAR = "calendar"
CONF_SEARCH = "search" CONF_SEARCH = "search"
CONF_DAYS = "days"
OFFSET = "!!" OFFSET = "!!"
@ -55,6 +56,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
], ],
), ),
vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean, vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean,
vol.Optional(CONF_DAYS, default=1): cv.positive_int,
} }
) )
@ -66,6 +68,7 @@ def setup_platform(hass, config, add_entities, disc_info=None):
url = config[CONF_URL] url = config[CONF_URL]
username = config.get(CONF_USERNAME) username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD) password = config.get(CONF_PASSWORD)
days = config[CONF_DAYS]
client = caldav.DAVClient( client = caldav.DAVClient(
url, None, username, password, ssl_verify_cert=config[CONF_VERIFY_SSL] url, None, username, password, ssl_verify_cert=config[CONF_VERIFY_SSL]
@ -92,7 +95,7 @@ def setup_platform(hass, config, add_entities, disc_info=None):
entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id, hass=hass) entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id, hass=hass)
calendar_devices.append( calendar_devices.append(
WebDavCalendarEventDevice( WebDavCalendarEventDevice(
name, calendar, entity_id, True, cust_calendar[CONF_SEARCH] name, calendar, entity_id, days, True, cust_calendar[CONF_SEARCH]
) )
) )
@ -102,7 +105,7 @@ def setup_platform(hass, config, add_entities, disc_info=None):
device_id = calendar.name device_id = calendar.name
entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id, hass=hass) entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id, hass=hass)
calendar_devices.append( calendar_devices.append(
WebDavCalendarEventDevice(name, calendar, entity_id) WebDavCalendarEventDevice(name, calendar, entity_id, days)
) )
add_entities(calendar_devices, True) add_entities(calendar_devices, True)
@ -111,9 +114,9 @@ def setup_platform(hass, config, add_entities, disc_info=None):
class WebDavCalendarEventDevice(CalendarEventDevice): class WebDavCalendarEventDevice(CalendarEventDevice):
"""A device for getting the next Task from a WebDav Calendar.""" """A device for getting the next Task from a WebDav Calendar."""
def __init__(self, name, calendar, entity_id, all_day=False, search=None): def __init__(self, name, calendar, entity_id, days, all_day=False, search=None):
"""Create the WebDav Calendar Event Device.""" """Create the WebDav Calendar Event Device."""
self.data = WebDavCalendarData(calendar, all_day, search) self.data = WebDavCalendarData(calendar, days, all_day, search)
self.entity_id = entity_id self.entity_id = entity_id
self._event = None self._event = None
self._name = name self._name = name
@ -153,9 +156,10 @@ class WebDavCalendarEventDevice(CalendarEventDevice):
class WebDavCalendarData: class WebDavCalendarData:
"""Class to utilize the calendar dav client object to get next event.""" """Class to utilize the calendar dav client object to get next event."""
def __init__(self, calendar, include_all_day, search): def __init__(self, calendar, days, include_all_day, search):
"""Set up how we are going to search the WebDav calendar.""" """Set up how we are going to search the WebDav calendar."""
self.calendar = calendar self.calendar = calendar
self.days = days
self.include_all_day = include_all_day self.include_all_day = include_all_day
self.search = search self.search = search
self.event = None self.event = None
@ -192,7 +196,7 @@ class WebDavCalendarData:
def update(self): def update(self):
"""Get the latest data.""" """Get the latest data."""
start_of_today = dt.start_of_local_day() start_of_today = dt.start_of_local_day()
start_of_tomorrow = dt.start_of_local_day() + timedelta(days=1) start_of_tomorrow = dt.start_of_local_day() + timedelta(days=self.days)
# We have to retrieve the results for the whole day as the server # We have to retrieve the results for the whole day as the server
# won't return events that have already started # won't return events that have already started