Skip to content

Commit

Permalink
Merge pull request #33 from tykeal/link_to_keymaster
Browse files Browse the repository at this point in the history
Fix: Correct fetch limit
  • Loading branch information
tykeal authored Jan 14, 2022
2 parents 4c7925a + 89e5ad6 commit 409bf16
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
22 changes: 19 additions & 3 deletions custom_components/rental_control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,18 @@ def update_config(self, config):
self.days = config.get(CONF_DAYS)
self.verify_ssl = config.get(CONF_VERIFY_SSL)

# updated the calendar in case the fetch days has changed
self.calendar = self._refresh_event_dict()

def _ical_parser(self, calendar, from_date, to_date):
"""Return a sorted list of events from a icalendar object."""

events = []

_LOGGER.debug(
"In _ical_parser:: from_date: %s; to_date: %s", from_date, to_date
)

for event in calendar.walk("VEVENT"):
# RRULEs should not exist in AirBnB bookings, so log and error and
# skip
Expand All @@ -213,13 +220,14 @@ def _ical_parser(self, calendar, from_date, to_date):
# Just ignore events that ended a long time ago
if "DTEND" in event and event[
"DTEND"
].dt.date() < from_date.date() - timedelta(days=30):
].dt < from_date.date() - timedelta(days=30):
continue
except Exception: # pylint: disable=broad-except
pass

try:
# Ignore dates that are too far in the future
if "DSTART" in event and event["DTSTART"].dt <= to_date.date():
if "DTSTART" in event and event["DTSTART"].dt > to_date.date():
continue
except Exception: # pylint: disable=broad-except
pass
Expand All @@ -238,7 +246,7 @@ def _ical_parser(self, calendar, from_date, to_date):
if "DTEND" not in event:
dtend = dtstart
else:
_LOGGER.debug("DTEND in event")
_LOGGER.debug("DTEND in event: %s", event["DTEND"].dt)
dtend = datetime.combine(
event["DTEND"].dt, self.checkout, dt.DEFAULT_TIME_ZONE
)
Expand Down Expand Up @@ -288,3 +296,11 @@ def _ical_event_dict(self, start, end, from_date, event):
}
_LOGGER.debug("Event to add: %s", str(event_dict))
return event_dict

def _refresh_event_dict(self):
"""Ensure that all events in the calendar are start before max days."""

cal = self.calendar
days = dt.start_of_local_day() + timedelta(days=self.days)

return [x for x in cal if x["start"].date() <= days.date()]
18 changes: 17 additions & 1 deletion custom_components/rental_control/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, hass, rental_control_events, sensor_name, event_number):
hass=self._hass,
)
self._event_attributes = {
"summary": None,
"summary": "No reservation",
"description": None,
"location": None,
"start": None,
Expand Down Expand Up @@ -140,3 +140,19 @@ async def async_update(self):
self._state = f"{name} - {start.strftime('%-d %B %Y')}"
if not val.get("all_day"):
self._state += f" {start.strftime('%H:%M')}"
else:
# No reservations
_LOGGER.debug(
"No events available for sensor %s, removing from calendar %s",
str(self._event_number),
self.name,
)
self._event_attributes = {
"summary": "No reservation",
"description": None,
"location": None,
"start": None,
"end": None,
"eta": None,
}
self._state = "No reservation"

0 comments on commit 409bf16

Please sign in to comment.