Home Assistant Unofficial Reference 2024.12.1
calendar.py
Go to the documentation of this file.
1 """Calendar platform for Mealie."""
2 
3 from __future__ import annotations
4 
5 from datetime import datetime
6 
7 from aiomealie import Mealplan, MealplanEntryType
8 
9 from homeassistant.components.calendar import CalendarEntity, CalendarEvent
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .coordinator import MealieConfigEntry, MealieMealplanCoordinator
14 from .entity import MealieEntity
15 
16 
18  hass: HomeAssistant,
19  entry: MealieConfigEntry,
20  async_add_entities: AddEntitiesCallback,
21 ) -> None:
22  """Set up the calendar platform for entity."""
23  coordinator = entry.runtime_data.mealplan_coordinator
24 
26  MealieMealplanCalendarEntity(coordinator, entry_type)
27  for entry_type in MealplanEntryType
28  )
29 
30 
31 def _get_event_from_mealplan(mealplan: Mealplan) -> CalendarEvent:
32  """Create a CalendarEvent from a Mealplan."""
33  description: str | None = mealplan.description
34  name = mealplan.title or "No recipe"
35  if mealplan.recipe:
36  name = mealplan.recipe.name
37  description = mealplan.recipe.description
38  return CalendarEvent(
39  start=mealplan.mealplan_date,
40  end=mealplan.mealplan_date,
41  summary=name,
42  description=description,
43  )
44 
45 
47  """A calendar entity."""
48 
49  def __init__(
50  self, coordinator: MealieMealplanCoordinator, entry_type: MealplanEntryType
51  ) -> None:
52  """Create the Calendar entity."""
53  super().__init__(coordinator, entry_type.name.lower())
54  self._entry_type_entry_type = entry_type
55  self._attr_translation_key_attr_translation_key = entry_type.name.lower()
56 
57  @property
58  def event(self) -> CalendarEvent | None:
59  """Return the next upcoming event."""
60  mealplans = self.coordinator.data[self._entry_type_entry_type]
61  if not mealplans:
62  return None
63  sorted_mealplans = sorted(mealplans, key=lambda x: x.mealplan_date)
64  return _get_event_from_mealplan(sorted_mealplans[0])
65 
66  async def async_get_events(
67  self, hass: HomeAssistant, start_date: datetime, end_date: datetime
68  ) -> list[CalendarEvent]:
69  """Get all events in a specific time frame."""
70  mealplans = (
71  await self.coordinator.client.get_mealplans(
72  start_date.date(), end_date.date()
73  )
74  ).items
75  return [
76  _get_event_from_mealplan(mealplan)
77  for mealplan in mealplans
78  if mealplan.entry_type is self._entry_type_entry_type
79  ]
None __init__(self, MealieMealplanCoordinator coordinator, MealplanEntryType entry_type)
Definition: calendar.py:51
list[CalendarEvent] async_get_events(self, HomeAssistant hass, datetime start_date, datetime end_date)
Definition: calendar.py:68
None async_setup_entry(HomeAssistant hass, MealieConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: calendar.py:21
CalendarEvent _get_event_from_mealplan(Mealplan mealplan)
Definition: calendar.py:31