Skip to content

Commit

Permalink
use icalevents instead of icalendar + recurring_ical_events
Browse files Browse the repository at this point in the history
The icalevents python package is much more lightweight than icalendar +
recurring_ical_events.
recurring_ical_events also problems in some environments, see github issue:
#164
  • Loading branch information
mampfes committed Feb 12, 2022
1 parent be37378 commit 1f65571
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
4 changes: 2 additions & 2 deletions custom_components/waste_collection_schedule/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"domain": "waste_collection_schedule",
"name": "waste_collection_schedule",
"documentation": "https://github.com/mampfes/hacs_waste_collection_schedule#readme",
"requirements": ["icalendar", "recurring_ical_events", "bs4"],
"requirements": ["icalevents", "bs4"],
"dependencies": [],
"codeowners": ["@mampfes"],
"iot_class": "cloud_polling",
"version": "1.14.0"
"version": "1.16.0-pre2"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import logging
import re

import icalendar
import recurring_ical_events
from icalevents import icalevents

_LOGGER = logging.getLogger(__name__)

Expand All @@ -17,14 +16,6 @@ def __init__(self, offset=None, regex=None, split_at=None):
self._split_at = split_at

def convert(self, ics_data):
# parse ics file
try:
calendar = icalendar.Calendar.from_ical(ics_data)
except Exception as err:
_LOGGER.error(f"Parsing ics data failed:{str(err)}")
_LOGGER.debug(ics_data)
return []

# calculate start- and end-date for recurring events
start_date = datetime.datetime.now().replace(
hour=0, minute=0, second=0, microsecond=0
Expand All @@ -33,32 +24,34 @@ def convert(self, ics_data):
start_date -= datetime.timedelta(days=self._offset)
end_date = start_date.replace(year=start_date.year + 1)

events = recurring_ical_events.of(calendar).between(start_date, end_date)
# parse ics data
events = icalevents.events(
start=start_date, end=end_date, string_content=ics_data.encode()
)

entries = []
for e in events:
if e.name == "VEVENT":
# calculate date
dtstart = None
if type(e.get("dtstart").dt) == datetime.date:
dtstart = e.get("dtstart").dt
elif type(e.get("dtstart").dt) == datetime.datetime:
dtstart = e.get("dtstart").dt.date()
if self._offset is not None:
dtstart += datetime.timedelta(days=self._offset)

# calculate waste type
summary = str(e.get("summary"))
if self._regex is not None:
match = self._regex.match(summary)
if match:
summary = match.group(1)

if self._split_at is not None:
summary = re.split(self._split_at, summary)
for t in summary:
entries.append((dtstart, t.strip().title()))
else:
entries.append((dtstart, summary))
# calculate date
dtstart = None
if type(e.start) == datetime.date:
dtstart = e.start
elif type(e.start) == datetime.datetime:
dtstart = e.start.date()
if self._offset is not None:
dtstart += datetime.timedelta(days=self._offset)

# calculate waste type
summary = str(e.summary)
if self._regex is not None:
match = self._regex.match(summary)
if match:
summary = match.group(1)

if self._split_at is not None:
summary = re.split(self._split_at, summary)
for t in summary:
entries.append((dtstart, t.strip().title()))
else:
entries.append((dtstart, summary))

return entries

0 comments on commit 1f65571

Please sign in to comment.