1
mirror of https://github.com/home-assistant/core synced 2024-10-07 10:13:38 +02:00

Fix todoist state updates (#91915)

* Update event when coordinator updates data.

* Move function to make diff easier to view

* Actually make it easier to view

* Move all tasks calculation to extra_state_attributes.
This commit is contained in:
Aaron Godfrey 2023-05-24 05:46:13 -07:00 committed by GitHub
parent ef24e508f8
commit 89f1677307
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 12 deletions

View File

@ -18,7 +18,7 @@ from homeassistant.components.calendar import (
CalendarEvent, CalendarEvent,
) )
from homeassistant.const import CONF_ID, CONF_NAME, CONF_TOKEN, EVENT_HOMEASSISTANT_STOP from homeassistant.const import CONF_ID, CONF_NAME, CONF_TOKEN, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import Event, HomeAssistant, ServiceCall from homeassistant.core import Event, HomeAssistant, ServiceCall, callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -175,7 +175,7 @@ async def async_setup_platform(
) )
) )
async_add_entities(project_devices) async_add_entities(project_devices, update_before_add=True)
session = async_get_clientsession(hass) session = async_get_clientsession(hass)
@ -304,6 +304,12 @@ class TodoistProjectEntity(CoordinatorEntity[TodoistCoordinator], CalendarEntity
str(data[CONF_ID]) if data.get(CONF_ID) is not None else None str(data[CONF_ID]) if data.get(CONF_ID) is not None else None
) )
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self.data.update()
super()._handle_coordinator_update()
@property @property
def event(self) -> CalendarEvent | None: def event(self) -> CalendarEvent | None:
"""Return the next upcoming event.""" """Return the next upcoming event."""
@ -317,11 +323,7 @@ class TodoistProjectEntity(CoordinatorEntity[TodoistCoordinator], CalendarEntity
async def async_update(self) -> None: async def async_update(self) -> None:
"""Update all Todoist Calendars.""" """Update all Todoist Calendars."""
await super().async_update() await super().async_update()
await self.data.async_update() self.data.update()
# Set Todoist-specific data that can't easily be grabbed
self._cal_data["all_tasks"] = [
task[SUMMARY] for task in self.data.all_project_tasks
]
async def async_get_events( async def async_get_events(
self, self,
@ -342,7 +344,7 @@ class TodoistProjectEntity(CoordinatorEntity[TodoistCoordinator], CalendarEntity
return { return {
DUE_TODAY: self.data.event[DUE_TODAY], DUE_TODAY: self.data.event[DUE_TODAY],
OVERDUE: self.data.event[OVERDUE], OVERDUE: self.data.event[OVERDUE],
ALL_TASKS: self._cal_data[ALL_TASKS], ALL_TASKS: [task[SUMMARY] for task in self.data.all_project_tasks],
PRIORITY: self.data.event[PRIORITY], PRIORITY: self.data.event[PRIORITY],
LABELS: self.data.event[LABELS], LABELS: self.data.event[LABELS],
} }
@ -610,7 +612,7 @@ class TodoistProjectData:
events.append(event) events.append(event)
return events return events
async def async_update(self) -> None: def update(self) -> None:
"""Get the latest data.""" """Get the latest data."""
tasks = self._coordinator.data tasks = self._coordinator.data
if self._id is None: if self._id is None:

View File

@ -216,9 +216,6 @@ async def test_calendar_custom_project_unique_id(
entity = entity_registry.async_get("calendar.all_projects") entity = entity_registry.async_get("calendar.all_projects")
assert entity is None assert entity is None
state = hass.states.get("calendar.all_projects")
assert state.state == "off"
@pytest.mark.parametrize( @pytest.mark.parametrize(
("due", "start", "end", "expected_response"), ("due", "start", "end", "expected_response"),