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 date platform (#106338)

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

View File

@ -3,7 +3,7 @@ from __future__ import annotations
from datetime import date, timedelta from datetime import date, timedelta
import logging import logging
from typing import final from typing import TYPE_CHECKING, final
import voluptuous as vol import voluptuous as vol
@ -21,6 +21,12 @@ from homeassistant.helpers.typing import ConfigType
from .const import DOMAIN, SERVICE_SET_VALUE 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) SCAN_INTERVAL = timedelta(seconds=30)
ENTITY_ID_FORMAT = DOMAIN + ".{}" ENTITY_ID_FORMAT = DOMAIN + ".{}"
@ -65,7 +71,10 @@ class DateEntityDescription(EntityDescription, frozen_or_thawed=True):
"""A class that describes date entities.""" """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.""" """Representation of a Date entity."""
entity_description: DateEntityDescription entity_description: DateEntityDescription
@ -73,13 +82,13 @@ class DateEntity(Entity):
_attr_native_value: date | None _attr_native_value: date | None
_attr_state: None = None _attr_state: None = None
@property @cached_property
@final @final
def device_class(self) -> None: def device_class(self) -> None:
"""Return the device class for the entity.""" """Return the device class for the entity."""
return None return None
@property @cached_property
@final @final
def state_attributes(self) -> None: def state_attributes(self) -> None:
"""Return the state attributes.""" """Return the state attributes."""
@ -93,7 +102,7 @@ class DateEntity(Entity):
return None return None
return self.native_value.isoformat() return self.native_value.isoformat()
@property @cached_property
def native_value(self) -> date | None: def native_value(self) -> date | None:
"""Return the value reported by the date.""" """Return the value reported by the date."""
return self._attr_native_value return self._attr_native_value