Skip to content

Commit

Permalink
Fix missing id in Habitica completed todos API response (#124565)
Browse files Browse the repository at this point in the history
* Fix missing id in completed todos API response

* Copy id only if none

* Update homeassistant/components/habitica/coordinator.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>

---------

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
  • Loading branch information
tr4nt0r and balloob committed Aug 25, 2024
1 parent 1bdf9d6 commit a45c1a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
9 changes: 8 additions & 1 deletion homeassistant/components/habitica/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ async def _async_update_data(self) -> HabiticaData:
try:
user_response = await self.api.user.get()
tasks_response = await self.api.tasks.user.get()
tasks_response.extend(await self.api.tasks.user.get(type="completedTodos"))
tasks_response.extend(
[
{"id": task["_id"], **task}
for task in await self.api.tasks.user.get(type="completedTodos")
if task.get("_id")
]
)

except ClientResponseError as error:
raise UpdateFailed(f"Error communicating with API: {error}") from error

Expand Down
24 changes: 12 additions & 12 deletions tests/components/habitica/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,31 @@ def common_requests(aioclient_mock: AiohttpClientMocker) -> AiohttpClientMocker:
}
},
)

aioclient_mock.get(
"https://habitica.com/api/v3/tasks/user",
"https://habitica.com/api/v3/tasks/user?type=completedTodos",
json={
"data": [
{
"text": f"this is a mock {task} #{i}",
"id": f"{i}",
"type": task,
"completed": False,
"text": "this is a mock todo #5",
"id": 5,
"_id": 5,
"type": "todo",
"completed": True,
}
for i, task in enumerate(("habit", "daily", "todo", "reward"), start=1)
]
},
)
aioclient_mock.get(
"https://habitica.com/api/v3/tasks/user?type=completedTodos",
"https://habitica.com/api/v3/tasks/user",
json={
"data": [
{
"text": "this is a mock todo #5",
"id": 5,
"type": "todo",
"completed": True,
"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)
]
},
)
Expand Down

0 comments on commit a45c1a3

Please sign in to comment.