calendar.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """Calendar platform support for Waste Collection Schedule."""
  2. import logging
  3. from datetime import datetime, timedelta
  4. from homeassistant.components.calendar import CalendarEntity, CalendarEvent
  5. from homeassistant.core import HomeAssistant
  6. from custom_components.waste_collection_schedule.waste_collection_schedule.scraper import Scraper
  7. _LOGGER = logging.getLogger(__name__)
  8. async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
  9. """Set up calendar platform."""
  10. # We only want this platform to be set up via discovery.
  11. if discovery_info is None:
  12. return
  13. entities = []
  14. api = discovery_info["api"]
  15. for scraper in api.scrapers:
  16. dedicated_calendar_types = scraper.get_dedicated_calendar_types()
  17. global_calendar_types = scraper.get_global_calendar_types()
  18. if dedicated_calendar_types is not None:
  19. for type in dedicated_calendar_types:
  20. unique_id = calc_unique_calendar_id(scraper, type)
  21. entities.append(
  22. WasteCollectionCalendar(
  23. api,
  24. scraper,
  25. scraper.get_calendar_title_for_type(type),
  26. [scraper.get_collection_type(type)],
  27. unique_id,
  28. )
  29. )
  30. if global_calendar_types is not None or dedicated_calendar_types is None:
  31. unique_id = calc_unique_calendar_id(scraper)
  32. entities.append(
  33. WasteCollectionCalendar(
  34. api,
  35. scraper,
  36. scraper.calendar_title,
  37. [
  38. scraper.get_collection_type(type)
  39. for type in global_calendar_types
  40. ]
  41. if global_calendar_types is not None
  42. else None,
  43. unique_id,
  44. )
  45. )
  46. async_add_entities(entities)
  47. class WasteCollectionCalendar(CalendarEntity):
  48. """Calendar entity class."""
  49. def __init__(self, api, scraper, name, types, unique_id: str):
  50. self._api = api
  51. self._scraper = scraper
  52. self._name = name
  53. self._types = types
  54. self._unique_id = unique_id
  55. self._attr_unique_id = unique_id
  56. @property
  57. def name(self):
  58. """Return entity name."""
  59. return self._name
  60. @property
  61. def event(self):
  62. """Return next collection event."""
  63. collections = self._scraper.get_upcoming(
  64. count=1, include_today=True, types=self._types
  65. )
  66. if len(collections) == 0:
  67. return None
  68. else:
  69. return self._convert(collections[0])
  70. async def async_get_events(
  71. self, hass: HomeAssistant, start_date: datetime, end_date: datetime
  72. ):
  73. """Return all events within specified time span."""
  74. events = []
  75. for collection in self._scraper.get_upcoming(
  76. include_today=True, types=self._types
  77. ):
  78. event = self._convert(collection)
  79. if start_date <= event.start_datetime_local <= end_date:
  80. events.append(event)
  81. return events
  82. def _convert(self, collection) -> CalendarEvent:
  83. """Convert an collection into a Home Assistant calendar event."""
  84. return CalendarEvent(
  85. summary=collection.type,
  86. start=collection.date,
  87. end=collection.date + timedelta(days=1),
  88. )
  89. def calc_unique_calendar_id(scraper: Scraper, type: str = None):
  90. return scraper.unique_id + ("_" + type if type is not None else "") + "_calendar"