Reduce API calls to fetch Habitica tasks (#116897)
This commit is contained in:
parent
7e8fab65ff
commit
d81fad1ef1
2 changed files with 16 additions and 18 deletions
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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",
|
||||||
|
|
Loading…
Add table
Reference in a new issue