Repository: mampfes/hacs_waste_collection_schedule

Issue:
Calendar used as a trigger for automations never fire.
So I created an automation that uses the generated calendar to notify me when I have to put the bins out using the new calendar triggers.

However, the automation never runs because the trigger never fires.

I debugged this a bit and found tha following issue:

HA asks the integration [here](https://github.com/home-assistant/core/blob/dev/homeassistant/components/calendar/trigger.py#L98) for all applicable events. However, the returned list is not quite correct. The timestamps are datetimes and the integration checks only the date component [here](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/custom_components/waste_collection_schedule/calendar.py#L53).

In my case, my local timezone is Europe/Berlin, which is currently UTC+2. HA gives UTC timestamps to the integration, so they are shifted by two hours "into the past" (not really, but you know what I mean). This means that the date check is wrong, as it misses the events for the day.

I changed the following and it worked in my testing but maybe you have a better idea on how to fix that:

```python
    async def async_get_events(self, hass, start_datetime, end_datetime):
        """Return all events within specified time span."""
        collections = []
        for a in self._scraper.get_upcoming(include_today=True):
            event = self._convert(a)
            if event.start_datetime_local >= start_datetime and event.end_datetime_local <= end_datetime:
                collections.append(event)
        return collections

    def _convert(self, collection):
        """Convert an collection into a Home Assistant calendar event."""
        return CalendarEvent(
            summary=collection.type,
            start=collection.date,
            end=collection.date,
        )
```

Essentially, I convert to a HA calender event first and then let HA convert the start/end times of the event to local time to compare them against the given start/end times which are still in UTC. But both are now proper datetime objects with timezone information so comparing them works fine.


Discussion:
Datetime handling is always a nightmare :-) I need some time to check this.

But upfront: You also removed the ` + timedelta(days=1)` from the end-date in `_convert`. Intentionally?
Kinda. I removed it during testing because I thought the end being one day later might trip up HA. I did not test putting it back.

HA considers an [event an all day event](https://github.com/home-assistant/core/blob/dev/homeassistant/components/calendar/__init__.py#L99) if `start` is _not_ `datetime`, so whatever date `end` is, should not matter for this. I looked at my Thunderbird calenders and it seems all all day events always just have the same day (and time!) for start and end.
Oh wait, I just remembered that it is intentional!

I call `_convert` once and then let HA convert it to local time so that I can then compare the start and end `datetime`s.

You could do this and then keep the one day offset:

```python
if event.start_datetime_local >= start_datetime and event.start_datetime_local <= end_datetime:
#                                                         ^ start_datetime_local instead of end_datetime_local
```

Resolving patch:
diff --git a/custom_components/waste_collection_schedule/calendar.py b/custom_components/waste_collection_schedule/calendar.py
--- a/custom_components/waste_collection_schedule/calendar.py
+++ b/custom_components/waste_collection_schedule/calendar.py
@@ -1,9 +1,11 @@
 """Calendar platform support for Waste Collection Schedule."""
 
 import logging
-from datetime import timedelta
+from datetime import timedelta, timezone, datetime
 
 from homeassistant.components.calendar import CalendarEntity, CalendarEvent
+from homeassistant.core import HomeAssistant
+from homeassistant.util.dt import DEFAULT_TIME_ZONE
 
 from custom_components.waste_collection_schedule.waste_collection_schedule.scraper import (
     Scraper,
@@ -88,15 +90,23 @@ def event(self):
         else:
             return self._convert(collections[0])
 
-    async def async_get_events(self, hass, start_date, end_date):
+    async def async_get_events(
+        self, hass: HomeAssistant, start_date: datetime, end_date: datetime
+    ):
         """Return all events within specified time span."""
-        collections = []
-        for a in self._scraper.get_upcoming(include_today=True, types=self._types):
-            if a.date >= start_date.date() and a.date <= end_date.date():
-                collections.append(self._convert(a))
-        return collections
+        events = []
 
-    def _convert(self, collection):
+        for collection in self._scraper.get_upcoming(
+            include_today=True, types=self._types
+        ):
+            event = self._convert(collection)
+
+            if start_date <= event.start_datetime_local <= end_date:
+                events.append(event)
+
+        return events
+
+    def _convert(self, collection) -> CalendarEvent:
         """Convert an collection into a Home Assistant calendar event."""
         return CalendarEvent(
             summary=collection.type,


Test patch: