From d81fad1ef148b0ef8119465a12625db3dd823f46 Mon Sep 17 00:00:00 2001 From: "Mr. Bubbles" Date: Mon, 6 May 2024 15:02:54 +0200 Subject: [PATCH] Reduce API calls to fetch Habitica tasks (#116897) --- .../components/habitica/coordinator.py | 4 +-- tests/components/habitica/test_init.py | 30 +++++++++---------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/habitica/coordinator.py b/homeassistant/components/habitica/coordinator.py index 385652f710a..d190cd41d4e 100644 --- a/homeassistant/components/habitica/coordinator.py +++ b/homeassistant/components/habitica/coordinator.py @@ -47,9 +47,7 @@ class HabiticaDataUpdateCoordinator(DataUpdateCoordinator[HabiticaData]): try: user_response = await self.api.user.get(userFields=",".join(user_fields)) - tasks_response = [] - for task_type in ("todos", "dailys", "habits", "rewards"): - tasks_response.extend(await self.api.tasks.user.get(type=task_type)) + tasks_response = await self.api.tasks.user.get() except ClientResponseError as error: raise UpdateFailed(f"Error communicating with API: {error}") from error diff --git a/tests/components/habitica/test_init.py b/tests/components/habitica/test_init.py index 50c7e664cd4..244086a632e 100644 --- a/tests/components/habitica/test_init.py +++ b/tests/components/habitica/test_init.py @@ -13,7 +13,6 @@ from homeassistant.components.habitica.const import ( EVENT_API_CALL_SUCCESS, SERVICE_API_CALL, ) -from homeassistant.components.habitica.sensor import TASKS_TYPES from homeassistant.const import ATTR_NAME 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( - f"https://habitica.com/api/v3/tasks/user?type={task_type}", - json={ - "data": [ - { - "text": f"this is a mock {task_type} #{task}", - "id": f"{task}", - "type": TASKS_TYPES[task_type].path[0], - } - for task in range(n_tasks) - ] - }, - ) + + aioclient_mock.get( + "https://habitica.com/api/v3/tasks/user", + json={ + "data": [ + { + "text": f"this is a mock {task} #{i}", + "id": f"{i}", + "type": task, + "completed": False, + } + for i, task in enumerate(("habit", "daily", "todo", "reward"), start=1) + ] + }, + ) aioclient_mock.post( "https://habitica.com/api/v3/tasks/user",