Add support for attribute caching to the todo platform (#106341)

This commit is contained in:
J. Nick Koston 2023-12-23 15:15:09 -10:00 committed by GitHub
parent 2a52453f5d
commit 278c7ac2a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,7 @@ from collections.abc import Callable, Iterable
import dataclasses import dataclasses
import datetime import datetime
import logging import logging
from typing import Any, final from typing import TYPE_CHECKING, Any, final
import voluptuous as vol import voluptuous as vol
@ -41,6 +41,12 @@ from .const import (
TodoListEntityFeature, TodoListEntityFeature,
) )
if TYPE_CHECKING:
from functools import cached_property
else:
from homeassistant.backports.functools import cached_property
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = datetime.timedelta(seconds=60) SCAN_INTERVAL = datetime.timedelta(seconds=60)
@ -226,7 +232,12 @@ class TodoItem:
""" """
class TodoListEntity(Entity): CACHED_PROPERTIES_WITH_ATTR_ = {
"todo_items",
}
class TodoListEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
"""An entity that represents a To-do list.""" """An entity that represents a To-do list."""
_attr_todo_items: list[TodoItem] | None = None _attr_todo_items: list[TodoItem] | None = None
@ -240,7 +251,7 @@ class TodoListEntity(Entity):
return None return None
return sum([item.status == TodoItemStatus.NEEDS_ACTION for item in items]) return sum([item.status == TodoItemStatus.NEEDS_ACTION for item in items])
@property @cached_property
def todo_items(self) -> list[TodoItem] | None: def todo_items(self) -> list[TodoItem] | None:
"""Return the To-do items in the To-do list.""" """Return the To-do items in the To-do list."""
return self._attr_todo_items return self._attr_todo_items