From b6c2842b0194cf0de135efd1ef1f3eee414db7f4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 23 Dec 2023 15:14:09 -1000 Subject: [PATCH] Add support for attribute caching to the date platform (#106338) --- homeassistant/components/date/__init__.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/date/__init__.py b/homeassistant/components/date/__init__.py index 7426293cfb4a..00ec09043c96 100644 --- a/homeassistant/components/date/__init__.py +++ b/homeassistant/components/date/__init__.py @@ -3,7 +3,7 @@ from __future__ import annotations from datetime import date, timedelta import logging -from typing import final +from typing import TYPE_CHECKING, final import voluptuous as vol @@ -21,6 +21,12 @@ from homeassistant.helpers.typing import ConfigType from .const import DOMAIN, SERVICE_SET_VALUE +if TYPE_CHECKING: + from functools import cached_property +else: + from homeassistant.backports.functools import cached_property + + SCAN_INTERVAL = timedelta(seconds=30) ENTITY_ID_FORMAT = DOMAIN + ".{}" @@ -65,7 +71,10 @@ class DateEntityDescription(EntityDescription, frozen_or_thawed=True): """A class that describes date entities.""" -class DateEntity(Entity): +CACHED_PROPERTIES_WITH_ATTR_ = {"native_value"} + + +class DateEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): """Representation of a Date entity.""" entity_description: DateEntityDescription @@ -73,13 +82,13 @@ class DateEntity(Entity): _attr_native_value: date | None _attr_state: None = None - @property + @cached_property @final def device_class(self) -> None: """Return the device class for the entity.""" return None - @property + @cached_property @final def state_attributes(self) -> None: """Return the state attributes.""" @@ -93,7 +102,7 @@ class DateEntity(Entity): return None return self.native_value.isoformat() - @property + @cached_property def native_value(self) -> date | None: """Return the value reported by the date.""" return self._attr_native_value