mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Bump Todoist SDK to fix project view style deserialization (#192431)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
bd6fab57b3
commit
1d401ec393
@@ -5,8 +5,8 @@ import logging
|
||||
|
||||
from todoist_api_python.api_async import TodoistAPIAsync
|
||||
|
||||
from homeassistant.const import CONF_TOKEN, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.const import CONF_TOKEN, EVENT_HOMEASSISTANT_STOP, Platform
|
||||
from homeassistant.core import Event, HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
@@ -34,15 +34,28 @@ async def async_setup_entry(hass: HomeAssistant, entry: TodoistConfigEntry) -> N
|
||||
"""Set up todoist from a config entry."""
|
||||
|
||||
token = entry.data[CONF_TOKEN]
|
||||
api = TodoistAPIAsync(token)
|
||||
# The SDK creates an httpx client on init, which loads SSL certs from disk.
|
||||
api = await hass.async_add_executor_job(TodoistAPIAsync, token)
|
||||
coordinator = TodoistCoordinator(hass, _LOGGER, entry, SCAN_INTERVAL, api, token)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
try:
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
entry.runtime_data = coordinator
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
except BaseException:
|
||||
await api.close()
|
||||
raise
|
||||
|
||||
entry.runtime_data = coordinator
|
||||
# Home Assistant shutdown does not unload config entries, so close the
|
||||
# SDK's HTTP client explicitly when Home Assistant stops.
|
||||
async def _close_api(_: Event) -> None:
|
||||
await api.close()
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
entry.async_on_unload(
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _close_api)
|
||||
)
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: TodoistConfigEntry) -> None:
|
||||
"""Unload a config entry."""
|
||||
await hass.config_entries.async_unload_platforms_or_raise(entry, PLATFORMS)
|
||||
await entry.runtime_data.api.close()
|
||||
|
||||
@@ -115,7 +115,7 @@ async def async_setup_platform(
|
||||
# Look up IDs based on (lowercase) names.
|
||||
project_id_lookup = {}
|
||||
|
||||
api = TodoistAPIAsync(token)
|
||||
api = await hass.async_add_executor_job(TodoistAPIAsync, token)
|
||||
coordinator = TodoistCoordinator(hass, _LOGGER, None, SCAN_INTERVAL, api, token)
|
||||
await coordinator.async_refresh()
|
||||
|
||||
@@ -125,6 +125,7 @@ async def async_setup_platform(
|
||||
|
||||
async def _shutdown_coordinator(_: Event) -> None:
|
||||
await coordinator.async_shutdown()
|
||||
await api.close()
|
||||
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown_coordinator)
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ from http import HTTPStatus
|
||||
import logging
|
||||
from typing import Any, override
|
||||
|
||||
from httpx import HTTPStatusError # noqa: TID251 # SDK 4.0.1 uses httpx
|
||||
import probatio
|
||||
from requests.exceptions import HTTPError
|
||||
from todoist_api_python.api_async import TodoistAPIAsync
|
||||
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||
@@ -43,10 +43,12 @@ class TodoistConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
|
||||
errors: dict[str, str] = {}
|
||||
if user_input is not None:
|
||||
api = TodoistAPIAsync(user_input[CONF_TOKEN])
|
||||
api = await self.hass.async_add_executor_job(
|
||||
TodoistAPIAsync, user_input[CONF_TOKEN]
|
||||
)
|
||||
try:
|
||||
await api.get_tasks()
|
||||
except HTTPError as err:
|
||||
except HTTPStatusError as err:
|
||||
if err.response.status_code == HTTPStatus.UNAUTHORIZED:
|
||||
errors["base"] = "invalid_api_key"
|
||||
else:
|
||||
@@ -56,6 +58,8 @@ class TodoistConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
return self.async_create_entry(title="Todoist", data=user_input)
|
||||
finally:
|
||||
await api.close()
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""DataUpdateCoordinator for the Todoist component."""
|
||||
|
||||
import asyncio
|
||||
from collections.abc import AsyncGenerator
|
||||
from collections.abc import AsyncIterator
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import override
|
||||
@@ -19,7 +19,7 @@ type TodoistConfigEntry = ConfigEntry[TodoistCoordinator]
|
||||
|
||||
|
||||
async def flatten_async_pages[T](
|
||||
pages: AsyncGenerator[list[T]],
|
||||
pages: AsyncIterator[list[T]],
|
||||
) -> list[T]:
|
||||
"""Flatten paginated results from an async generator."""
|
||||
all_items: list[T] = []
|
||||
|
||||
@@ -9,5 +9,5 @@
|
||||
"integration_type": "service",
|
||||
"iot_class": "cloud_polling",
|
||||
"loggers": ["todoist"],
|
||||
"requirements": ["todoist-api-python==3.1.0"]
|
||||
"requirements": ["todoist-api-python==4.0.1"]
|
||||
}
|
||||
|
||||
Generated
+1
-1
@@ -3361,7 +3361,7 @@ tmb==0.0.4
|
||||
tmodbus==1.1.0
|
||||
|
||||
# homeassistant.components.todoist
|
||||
todoist-api-python==3.1.0
|
||||
todoist-api-python==4.0.1
|
||||
|
||||
# homeassistant.components.togrill
|
||||
togrill-bluetooth==0.9.0
|
||||
|
||||
@@ -5,10 +5,9 @@ from http import HTTPStatus
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from httpx import HTTPStatusError, Request, Response # SDK 4.0.1 uses httpx
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
from requests.exceptions import HTTPError
|
||||
from requests.models import Response
|
||||
from todoist_api_python.api_async import TodoistAPIAsync
|
||||
from todoist_api_python.models import Collaborator, Due, Label, Project, Section, Task
|
||||
|
||||
@@ -179,9 +178,11 @@ def mock_api_side_effect(
|
||||
) -> MockConfigEntry:
|
||||
"""Mock todoist configuration."""
|
||||
if todoist_api_status:
|
||||
response = Response()
|
||||
response.status_code = todoist_api_status
|
||||
api.get_tasks.side_effect = HTTPError(response=response)
|
||||
request = Request("GET", "https://api.todoist.com/api/v1/tasks")
|
||||
response = Response(todoist_api_status, request=request)
|
||||
api.get_tasks.side_effect = HTTPStatusError(
|
||||
"API error", request=request, response=response
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(name="todoist_config_entry")
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
"""Unit tests for the Todoist integration."""
|
||||
|
||||
from http import HTTPStatus
|
||||
from unittest.mock import AsyncMock
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
from todoist_api_python.models import Project
|
||||
|
||||
from homeassistant.components.todoist.const import DOMAIN
|
||||
from homeassistant.components.todoist.services import (
|
||||
@@ -12,14 +13,17 @@ from homeassistant.components.todoist.services import (
|
||||
TodoistServiceArgument,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from .conftest import PROJECT_ID
|
||||
from .conftest import PROJECT_ID, make_api_response
|
||||
|
||||
|
||||
async def test_load_unload(
|
||||
hass: HomeAssistant,
|
||||
setup_integration: None,
|
||||
api: AsyncMock,
|
||||
todoist_config_entry: MockConfigEntry | None,
|
||||
) -> None:
|
||||
"""Test loading and unloading of the config entry."""
|
||||
@@ -30,6 +34,23 @@ async def test_load_unload(
|
||||
|
||||
assert await hass.config_entries.async_unload(todoist_config_entry.entry_id)
|
||||
assert todoist_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
api.close.assert_awaited_once()
|
||||
|
||||
|
||||
async def test_shutdown_closes_api(
|
||||
hass: HomeAssistant,
|
||||
setup_integration: None,
|
||||
api: AsyncMock,
|
||||
todoist_config_entry: MockConfigEntry | None,
|
||||
) -> None:
|
||||
"""Test the API client is closed when Home Assistant stops."""
|
||||
assert todoist_config_entry.state is ConfigEntryState.LOADED
|
||||
api.close.assert_not_awaited()
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
api.close.assert_awaited_once()
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("setup_integration")
|
||||
@@ -64,3 +85,48 @@ async def test_init_failure(
|
||||
) -> None:
|
||||
"""Test an initialization error on integration load."""
|
||||
assert todoist_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"view_style", ["list", "board", "calendar", "table", "future_layout"]
|
||||
)
|
||||
async def test_project_deserialization_setup(
|
||||
hass: HomeAssistant,
|
||||
api: AsyncMock,
|
||||
todoist_config_entry: MockConfigEntry,
|
||||
view_style: str,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Deserialize API projects before setting up both platforms."""
|
||||
project = Project.from_dict(
|
||||
{
|
||||
"id": PROJECT_ID,
|
||||
"name": "Synthetic project",
|
||||
"description": "",
|
||||
"child_order": 1,
|
||||
"color": "blue",
|
||||
"collapsed": False,
|
||||
"shared": False,
|
||||
"is_favorite": False,
|
||||
"is_archived": False,
|
||||
"can_assign_tasks": False,
|
||||
"view_style": view_style,
|
||||
"created_at": "2026-01-01T00:00:00.000000Z",
|
||||
"updated_at": "2026-01-01T00:00:00.000000Z",
|
||||
}
|
||||
)
|
||||
api.get_projects.side_effect = make_api_response([project])
|
||||
todoist_config_entry.add_to_hass(hass)
|
||||
with patch("homeassistant.components.todoist.TodoistAPIAsync", return_value=api):
|
||||
assert await hass.config_entries.async_setup(todoist_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert todoist_config_entry.state is ConfigEntryState.LOADED
|
||||
assert hass.states.get("todo.synthetic_project") is not None
|
||||
assert hass.states.get("calendar.synthetic_project") is not None
|
||||
assert entity_registry.async_get("todo.synthetic_project").unique_id == (
|
||||
f"{todoist_config_entry.entry_id}-{PROJECT_ID}"
|
||||
)
|
||||
assert (
|
||||
entity_registry.async_get("calendar.synthetic_project").unique_id == PROJECT_ID
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user