Reduce API calls to fetch Habitica tasks (#116897)

This commit is contained in:
Mr. Bubbles 2024-05-06 15:02:54 +02:00 committed by GitHub
parent 7e8fab65ff
commit d81fad1ef1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 18 deletions

View file

@ -47,9 +47,7 @@ class HabiticaDataUpdateCoordinator(DataUpdateCoordinator[HabiticaData]):
try: try:
user_response = await self.api.user.get(userFields=",".join(user_fields)) user_response = await self.api.user.get(userFields=",".join(user_fields))
tasks_response = [] tasks_response = await self.api.tasks.user.get()
for task_type in ("todos", "dailys", "habits", "rewards"):
tasks_response.extend(await self.api.tasks.user.get(type=task_type))
except ClientResponseError as error: except ClientResponseError as error:
raise UpdateFailed(f"Error communicating with API: {error}") from error raise UpdateFailed(f"Error communicating with API: {error}") from error

View file

@ -13,7 +13,6 @@ from homeassistant.components.habitica.const import (
EVENT_API_CALL_SUCCESS, EVENT_API_CALL_SUCCESS,
SERVICE_API_CALL, SERVICE_API_CALL,
) )
from homeassistant.components.habitica.sensor import TASKS_TYPES
from homeassistant.const import ATTR_NAME from homeassistant.const import ATTR_NAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -73,20 +72,21 @@ def common_requests(aioclient_mock):
} }
}, },
) )
for n_tasks, task_type in enumerate(TASKS_TYPES.keys(), start=1):
aioclient_mock.get( aioclient_mock.get(
f"https://habitica.com/api/v3/tasks/user?type={task_type}", "https://habitica.com/api/v3/tasks/user",
json={ json={
"data": [ "data": [
{ {
"text": f"this is a mock {task_type} #{task}", "text": f"this is a mock {task} #{i}",
"id": f"{task}", "id": f"{i}",
"type": TASKS_TYPES[task_type].path[0], "type": task,
} "completed": False,
for task in range(n_tasks) }
] for i, task in enumerate(("habit", "daily", "todo", "reward"), start=1)
}, ]
) },
)
aioclient_mock.post( aioclient_mock.post(
"https://habitica.com/api/v3/tasks/user", "https://habitica.com/api/v3/tasks/user",