Skip to content

Commit

Permalink
Merge pull request #240 from marcelvriend/feature/performance-get-onl…
Browse files Browse the repository at this point in the history
…y-necessary-data

Only get necessary data from Grocy
  • Loading branch information
isabellaalstrom authored Aug 1, 2022
2 parents a414f52 + 5834f60 commit be0a57b
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions custom_components/grocy/grocy_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

import logging
from datetime import datetime
from datetime import datetime, timedelta
from typing import List

from aiohttp import hdrs, web
Expand Down Expand Up @@ -78,18 +78,12 @@ def wrapper():
async def async_update_overdue_chores(self):
"""Update overdue chores data."""

query_filter = [f"next_estimated_execution_time<{datetime.now()}"]

def wrapper():
return self.api.chores(True)
return self.api.chores(get_details=True, query_filters=query_filter)

chores = await self.hass.async_add_executor_job(wrapper)
overdue_chores = []
for chore in chores:
if chore.next_estimated_execution_time:
now = datetime.now()
due = chore.next_estimated_execution_time
if due < now:
overdue_chores.append(chore)
return overdue_chores
return await self.hass.async_add_executor_job(wrapper)

async def async_get_config(self):
"""Get the configuration from Grocy."""
Expand All @@ -101,20 +95,22 @@ def wrapper():

async def async_update_tasks(self):
"""Update tasks data."""

return await self.hass.async_add_executor_job(self.api.tasks)

async def async_update_overdue_tasks(self):
"""Update overdue tasks data."""
tasks = await self.hass.async_add_executor_job(self.api.tasks)

overdue_tasks = []
for task in tasks:
if task.due_date:
current_date = datetime.now().date()
due_date = task.due_date
if due_date < current_date:
overdue_tasks.append(task)
return overdue_tasks
and_query_filter = [
f"due_date<{datetime.now().date()}",
# It's not possible to pass an empty value to Grocy, so use a regex that matches non-empty values to exclude empty str due_date.
r"due_date§.*\S.*",
]

def wrapper():
return self.api.tasks(query_filters=and_query_filter)

return await self.hass.async_add_executor_job(wrapper)

async def async_update_shopping_list(self):
"""Update shopping list data."""
Expand Down Expand Up @@ -159,12 +155,13 @@ def wrapper():
async def async_update_meal_plan(self):
"""Update meal plan data."""

# The >= condition is broken before Grocy 3.3.1. So use > to maintain backward compatibility.
yesterday = datetime.now() - timedelta(1)
query_filter = [f"day>{yesterday.date()}"]

def wrapper():
meal_plan = self.api.meal_plan(True)
today = datetime.today().date()
plan = [
MealPlanItemWrapper(item) for item in meal_plan if item.day >= today
]
meal_plan = self.api.meal_plan(get_details=True, query_filters=query_filter)
plan = [MealPlanItemWrapper(item) for item in meal_plan]
return sorted(plan, key=lambda item: item.meal_plan.day)

return await self.hass.async_add_executor_job(wrapper)
Expand Down

0 comments on commit be0a57b

Please sign in to comment.