prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <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.
  
  </issue>
  <code>
  [start of custom_components/waste_collection_schedule/calendar.py]
  1 """Calendar platform support for Waste Collection Schedule."""
  2 
  3 import logging
  4 from datetime import timedelta
  5 
  6 from homeassistant.components.calendar import CalendarEntity, CalendarEvent
  7 
  8 from custom_components.waste_collection_schedule.waste_collection_schedule.scraper import (
  9     Scraper,
  10 )
  11 
  12 _LOGGER = logging.getLogger(__name__)
  13 
  14 
  15 async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
  16     """Set up calendar platform."""
  17     # We only want this platform to be set up via discovery.
  18     if discovery_info is None:
  19         return
  20 
  21     entities = []
  22 
  23     api = discovery_info["api"]
  24 
  25     for scraper in api.scrapers:
  26         dedicated_calendar_types = scraper.get_dedicated_calendar_types()
  27         global_calendar_types = scraper.get_global_calendar_types()
  28 
  29         if dedicated_calendar_types is not None:
  30             for type in dedicated_calendar_types:
  31                 unique_id = calc_unique_calendar_id(scraper, type)
  32 
  33                 entities.append(
  34                     WasteCollectionCalendar(
  35                         api,
  36                         scraper,
  37                         scraper.get_calendar_title_for_type(type),
  38                         [scraper.get_collection_type(type)],
  39                         unique_id,
  40                     )
  41                 )
  42 
  43         if global_calendar_types is not None or dedicated_calendar_types is None:
  44             unique_id = calc_unique_calendar_id(scraper)
  45             entities.append(
  46                 WasteCollectionCalendar(
  47                     api,
  48                     scraper,
  49                     scraper.calendar_title,
  50                     [
  51                         scraper.get_collection_type(type)
  52                         for type in global_calendar_types
  53                     ]
  54                     if global_calendar_types is not None
  55                     else None,
  56                     unique_id,
  57                 )
  58             )
  59 
  60     async_add_entities(entities)
  61 
  62 
  63 class WasteCollectionCalendar(CalendarEntity):
  64     """Calendar entity class."""
  65 
  66     def __init__(self, api, scraper, name, types, unique_id: str):
  67         self._api = api
  68         self._scraper = scraper
  69         self._name = name
  70         self._types = types
  71         self._unique_id = unique_id
  72         self._attr_unique_id = unique_id
  73 
  74     @property
  75     def name(self):
  76         """Return entity name."""
  77         return self._name
  78 
  79     @property
  80     def event(self):
  81         """Return next collection event."""
  82         collections = self._scraper.get_upcoming(
  83             count=1, include_today=True, types=self._types
  84         )
  85 
  86         if len(collections) == 0:
  87             return None
  88         else:
  89             return self._convert(collections[0])
  90 
  91     async def async_get_events(self, hass, start_date, end_date):
  92         """Return all events within specified time span."""
  93         collections = []
  94         for a in self._scraper.get_upcoming(include_today=True, types=self._types):
  95             if a.date >= start_date.date() and a.date <= end_date.date():
  96                 collections.append(self._convert(a))
  97         return collections
  98 
  99     def _convert(self, collection):
  100         """Convert an collection into a Home Assistant calendar event."""
  101         return CalendarEvent(
  102             summary=collection.type,
  103             start=collection.date,
  104             end=collection.date + timedelta(days=1),
  105         )
  106 
  107 
  108 def calc_unique_calendar_id(scraper: Scraper, type: str | None = None):
  109     return scraper.unique_id + ("_" + type if type is not None else "") + "_calendar"
  110
  [end of custom_components/waste_collection_schedule/calendar.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: 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 @@
           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,
  
verification_info: {"golden_diff": "diff --git a/custom_components/waste_collection_schedule/calendar.py b/custom_components/waste_collection_schedule/calendar.py\n--- a/custom_components/waste_collection_schedule/calendar.py\n+++ b/custom_components/waste_collection_schedule/calendar.py\n@@ -1,9 +1,11 @@\n \"\"\"Calendar platform support for Waste Collection Schedule.\"\"\"\n \n import logging\n-from datetime import timedelta\n+from datetime import timedelta, timezone, datetime\n \n from homeassistant.components.calendar import CalendarEntity, CalendarEvent\n+from homeassistant.core import HomeAssistant\n+from homeassistant.util.dt import DEFAULT_TIME_ZONE\n \n from custom_components.waste_collection_schedule.waste_collection_schedule.scraper import (\n     Scraper,\n@@ -88,15 +90,23 @@\n         else:\n             return self._convert(collections[0])\n \n-    async def async_get_events(self, hass, start_date, end_date):\n+    async def async_get_events(\n+        self, hass: HomeAssistant, start_date: datetime, end_date: datetime\n+    ):\n         \"\"\"Return all events within specified time span.\"\"\"\n-        collections = []\n-        for a in self._scraper.get_upcoming(include_today=True, types=self._types):\n-            if a.date >= start_date.date() and a.date <= end_date.date():\n-                collections.append(self._convert(a))\n-        return collections\n+        events = []\n \n-    def _convert(self, collection):\n+        for collection in self._scraper.get_upcoming(\n+            include_today=True, types=self._types\n+        ):\n+            event = self._convert(collection)\n+\n+            if start_date <= event.start_datetime_local <= end_date:\n+                events.append(event)\n+\n+        return events\n+\n+    def _convert(self, collection) -> CalendarEvent:\n         \"\"\"Convert an collection into a Home Assistant calendar event.\"\"\"\n         return CalendarEvent(\n             summary=collection.type,\n", "issue": "Calendar used as a trigger for automations never fire.\nSo 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.\r\n\r\nHowever, the automation never runs because the trigger never fires.\r\n\r\nI debugged this a bit and found tha following issue:\r\n\r\nHA 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).\r\n\r\nIn 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.\r\n\r\nI changed the following and it worked in my testing but maybe you have a better idea on how to fix that:\r\n\r\n```python\r\n    async def async_get_events(self, hass, start_datetime, end_datetime):\r\n        \"\"\"Return all events within specified time span.\"\"\"\r\n        collections = []\r\n        for a in self._scraper.get_upcoming(include_today=True):\r\n            event = self._convert(a)\r\n            if event.start_datetime_local >= start_datetime and event.end_datetime_local <= end_datetime:\r\n                collections.append(event)\r\n        return collections\r\n\r\n    def _convert(self, collection):\r\n        \"\"\"Convert an collection into a Home Assistant calendar event.\"\"\"\r\n        return CalendarEvent(\r\n            summary=collection.type,\r\n            start=collection.date,\r\n            end=collection.date,\r\n        )\r\n```\r\n\r\nEssentially, 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.\n", "before_files": [{"content": "\"\"\"Calendar platform support for Waste Collection Schedule.\"\"\"\n\nimport logging\nfrom datetime import timedelta\n\nfrom homeassistant.components.calendar import CalendarEntity, CalendarEvent\n\nfrom custom_components.waste_collection_schedule.waste_collection_schedule.scraper import (\n    Scraper,\n)\n\n_LOGGER = logging.getLogger(__name__)\n\n\nasync def async_setup_platform(hass, config, async_add_entities, discovery_info=None):\n    \"\"\"Set up calendar platform.\"\"\"\n    # We only want this platform to be set up via discovery.\n    if discovery_info is None:\n        return\n\n    entities = []\n\n    api = discovery_info[\"api\"]\n\n    for scraper in api.scrapers:\n        dedicated_calendar_types = scraper.get_dedicated_calendar_types()\n        global_calendar_types = scraper.get_global_calendar_types()\n\n        if dedicated_calendar_types is not None:\n            for type in dedicated_calendar_types:\n                unique_id = calc_unique_calendar_id(scraper, type)\n\n                entities.append(\n                    WasteCollectionCalendar(\n                        api,\n                        scraper,\n                        scraper.get_calendar_title_for_type(type),\n                        [scraper.get_collection_type(type)],\n                        unique_id,\n                    )\n                )\n\n        if global_calendar_types is not None or dedicated_calendar_types is None:\n            unique_id = calc_unique_calendar_id(scraper)\n            entities.append(\n                WasteCollectionCalendar(\n                    api,\n                    scraper,\n                    scraper.calendar_title,\n                    [\n                        scraper.get_collection_type(type)\n                        for type in global_calendar_types\n                    ]\n                    if global_calendar_types is not None\n                    else None,\n                    unique_id,\n                )\n            )\n\n    async_add_entities(entities)\n\n\nclass WasteCollectionCalendar(CalendarEntity):\n    \"\"\"Calendar entity class.\"\"\"\n\n    def __init__(self, api, scraper, name, types, unique_id: str):\n        self._api = api\n        self._scraper = scraper\n        self._name = name\n        self._types = types\n        self._unique_id = unique_id\n        self._attr_unique_id = unique_id\n\n    @property\n    def name(self):\n        \"\"\"Return entity name.\"\"\"\n        return self._name\n\n    @property\n    def event(self):\n        \"\"\"Return next collection event.\"\"\"\n        collections = self._scraper.get_upcoming(\n            count=1, include_today=True, types=self._types\n        )\n\n        if len(collections) == 0:\n            return None\n        else:\n            return self._convert(collections[0])\n\n    async def async_get_events(self, hass, start_date, end_date):\n        \"\"\"Return all events within specified time span.\"\"\"\n        collections = []\n        for a in self._scraper.get_upcoming(include_today=True, types=self._types):\n            if a.date >= start_date.date() and a.date <= end_date.date():\n                collections.append(self._convert(a))\n        return collections\n\n    def _convert(self, collection):\n        \"\"\"Convert an collection into a Home Assistant calendar event.\"\"\"\n        return CalendarEvent(\n            summary=collection.type,\n            start=collection.date,\n            end=collection.date + timedelta(days=1),\n        )\n\n\ndef calc_unique_calendar_id(scraper: Scraper, type: str | None = None):\n    return scraper.unique_id + (\"_\" + type if type is not None else \"\") + \"_calendar\"\n", "path": "custom_components/waste_collection_schedule/calendar.py"}]}