From fbb126256bf242c44612135322f79db86999e004 Mon Sep 17 00:00:00 2001 From: Andrew Jackson Date: Sun, 2 Jun 2024 14:31:32 +0100 Subject: [PATCH] Calendar --- custom_components/mealie/calendar.py | 71 +++++++++++++++++++++---- custom_components/mealie/coordinator.py | 33 ++++++++++++ 2 files changed, 94 insertions(+), 10 deletions(-) diff --git a/custom_components/mealie/calendar.py b/custom_components/mealie/calendar.py index b1ecb67..f1676de 100644 --- a/custom_components/mealie/calendar.py +++ b/custom_components/mealie/calendar.py @@ -19,7 +19,7 @@ CalendarEvent, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_ID, CONF_NAME, CONF_TOKEN, EVENT_HOMEASSISTANT_STOP +from homeassistant.const import CONF_ID, CONF_NAME, CONF_TOKEN, EVENT_HOMEASSISTANT_STOP, STATE_OFF, STATE_ON from homeassistant.core import Event, HomeAssistant, ServiceCall, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -38,7 +38,6 @@ SCAN_INTERVAL = timedelta(minutes=1) - async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: @@ -74,12 +73,68 @@ def _handle_coordinator_update(self) -> None: self.todays_meal_plan = self.coordinator.meal_plan super()._handle_coordinator_update() + @property + def breakfast_start(self) -> datetime: + """Return breakfast start today.""" + return dt_util.now().replace(hour=7, minute=0, second=0) + + @property + def breakfast_end(self) -> datetime: + """Return breakfast end today.""" + return dt_util.now().replace(hour=11, minute=0, second=0) + + @property + def lunch_start(self) -> datetime: + """Return lunch start today.""" + return dt_util.now().replace(hour=11, minute=30, second=0) + + @property + def lunch_end(self) -> datetime: + """Return lunch end today.""" + return dt_util.now().replace(hour=14, minute=30, second=0) + + @property + def dinner_start(self) -> datetime: + """Return dinner start today.""" + return dt_util.now().replace(hour=16, minute=0, second=0) + + @property + def dinner_end(self) -> datetime: + """Return dinner end today.""" + return dt_util.now().replace(hour=21, minute=0, second=0) + + @property def event(self) -> CalendarEvent | None: """Return the next upcoming event.""" - # return self.todays_meal_plan + + if self.breakfast_start <= dt_util.utcnow() <= self.breakfast_end: + if self.coordinator.today_breakfast(): + return CalendarEvent(start=self.breakfast_start, end=self.breakfast_end, summary=self.coordinator.today_breakfast()) + + if self.lunch_start <= dt_util.utcnow() <= self.lunch_end: + if self.coordinator.today_lunch(): + return CalendarEvent(start=self.lunch_start, end=self.lunch_end, summary=self.coordinator.today_lunch()) + + if self.dinner_start <= dt_util.utcnow() <= self.dinner_end: + if self.coordinator.today_dinner(): + return CalendarEvent(start=self.dinner_start, end=self.dinner_end, summary=self.coordinator.today_dinner()) + return None + @property + def state(self) -> str: + """Return the state of the calendar event.""" + if (event := self.event) is None: + return STATE_OFF + + now = dt_util.now() + + if event.start_datetime_local <= now < event.end_datetime_local: + return STATE_ON + + return STATE_OFF + @property def name(self) -> str: """Return the name of the entity.""" @@ -111,19 +166,15 @@ async def async_get_events( if plan["entryType"] == "breakfast": start_time = "7:00:00" end_time = "11:00:00" - summary_event_type = "Breakfast" elif plan["entryType"] == "lunch": start_time = "11:30:00" - end_time = "14:00:00" - summary_event_type = "Lunch" + end_time = "14:30:00" elif plan["entryType"] == "dinner": start_time = "16:00:00" end_time = "21:00:00" - summary_event_type = "Dinner" else: start_time = "16:00:00" end_time = "21:00:00" - summary_event_type = "Side" mealie_start_dt = f"{plan["date"]} {start_time}" mealie_end_dt = f"{plan["date"]} {end_time}" @@ -146,9 +197,9 @@ async def async_get_events( # end = end.replace(tzinfo=dt_util.get_time_zone(self.hass.config.time_zone)) if plan["recipeId"]: - summary = f"{plan["recipe"]["name"]} ({summary_event_type})" + summary = plan["recipe"]["name"] else: - summary = f"{plan["title"]} ({summary_event_type})" + summary = plan["title"] event = CalendarEvent(start=start, end=end, summary=summary, uid=plan["id"]) diff --git a/custom_components/mealie/coordinator.py b/custom_components/mealie/coordinator.py index df3f760..c18df4a 100644 --- a/custom_components/mealie/coordinator.py +++ b/custom_components/mealie/coordinator.py @@ -42,6 +42,39 @@ def __init__( update_interval=timedelta(seconds=10), ) + def today_breakfast(self) -> str | None: + """Return today's breakfast.""" + if self.meal_plan: + for plan in self.meal_plan: + if plan.get("entryType") == "breakfast": + if plan["recipeId"]: + return plan["recipe"]["name"] + else: + return plan["title"] + return None + + def today_lunch(self) -> str | None: + """Return today's lunch.""" + if self.meal_plan: + for plan in self.meal_plan: + if plan.get("entryType") == "lunch": + if plan["recipeId"]: + return plan["recipe"]["name"] + else: + return plan["title"] + return None + + def today_dinner(self) -> str | None: + """Return today's dinner.""" + if self.meal_plan: + for plan in self.meal_plan: + if plan.get("entryType") == "dinner": + if plan["recipeId"]: + return plan["recipe"]["name"] + else: + return plan["title"] + return None + async def async_get_shopping_lists(self) -> dict: """Return shopping lists fetched at most once.""" if self._shopping_lists is None: