diff --git a/homeassistant/components/calendar/caldav.py b/homeassistant/components/calendar/caldav.py index ba798ce7902..d70e7ff8946 100644 --- a/homeassistant/components/calendar/caldav.py +++ b/homeassistant/components/calendar/caldav.py @@ -166,7 +166,7 @@ class WebDavCalendarData(object): self.event = { "summary": vevent.summary.value, "start": self.get_hass_date(vevent.dtstart.value), - "end": self.get_hass_date(vevent.dtend.value), + "end": self.get_hass_date(self.get_end_date(vevent)), "location": self.get_attr_value(vevent, "location"), "description": self.get_attr_value(vevent, "description") } @@ -194,7 +194,7 @@ class WebDavCalendarData(object): @staticmethod def is_over(vevent): """Return if the event is over.""" - return dt.now() > WebDavCalendarData.to_datetime(vevent.dtend.value) + return dt.now() > WebDavCalendarData.get_end_date(vevent) @staticmethod def get_hass_date(obj): @@ -217,3 +217,17 @@ class WebDavCalendarData(object): if hasattr(obj, attribute): return getattr(obj, attribute).value return None + + @staticmethod + def get_end_date(obj): + """Return the end datetime as determined by dtend or duration.""" + if hasattr(obj, "dtend"): + enddate = obj.dtend.value + + elif hasattr(obj, "duration"): + enddate = obj.dtstart.value + obj.duration.value + + else: + enddate = obj.dtstart.value + timedelta(days=1) + + return WebDavCalendarData.to_datetime(enddate) diff --git a/tests/components/calendar/test_caldav.py b/tests/components/calendar/test_caldav.py index 7234d40c410..e44e5cfc1f0 100644 --- a/tests/components/calendar/test_caldav.py +++ b/tests/components/calendar/test_caldav.py @@ -64,7 +64,49 @@ LOCATION:Hamburg DESCRIPTION:What a beautiful day END:VEVENT END:VCALENDAR +""", + """BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Global Corp.//CalDAV Client//EN +BEGIN:VEVENT +UID:4 +DTSTAMP:20171125T000000Z +DTSTART:20171127 +SUMMARY:This is an event without dtend or duration +LOCATION:Hamburg +DESCRIPTION:What an endless day +END:VEVENT +END:VCALENDAR +""", + """BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Global Corp.//CalDAV Client//EN +BEGIN:VEVENT +UID:5 +DTSTAMP:20171125T000000Z +DTSTART:20171127 +DURATION:PT1H +SUMMARY:This is an event with duration +LOCATION:Hamburg +DESCRIPTION:What a day +END:VEVENT +END:VCALENDAR +""", + """BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Global Corp.//CalDAV Client//EN +BEGIN:VEVENT +UID:6 +DTSTAMP:20171125T000000Z +DTSTART:20171127T100000Z +DURATION:PT1H +SUMMARY:This is an event with duration +LOCATION:Hamburg +DESCRIPTION:What a day +END:VEVENT +END:VCALENDAR """ + ]