1
mirror of https://github.com/home-assistant/core synced 2024-08-28 03:36:46 +02:00

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 datetime
import logging
from typing import Any, final
from typing import TYPE_CHECKING, Any, final
import voluptuous as vol
@ -41,6 +41,12 @@ from .const import (
TodoListEntityFeature,
)
if TYPE_CHECKING:
from functools import cached_property
else:
from homeassistant.backports.functools import cached_property
_LOGGER = logging.getLogger(__name__)
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."""
_attr_todo_items: list[TodoItem] | None = None
@ -240,7 +251,7 @@ class TodoListEntity(Entity):
return None
return sum([item.status == TodoItemStatus.NEEDS_ACTION for item in items])
@property
@cached_property
def todo_items(self) -> list[TodoItem] | None:
"""Return the To-do items in the To-do list."""
return self._attr_todo_items