mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 00:07:10 +00:00
Add support for attribute caching to the datetime platform (#106340)
This commit is contained in:
parent
b5e1074062
commit
2a52453f5d
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from datetime import UTC, datetime, timedelta
|
from datetime import UTC, datetime, 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,11 @@ from homeassistant.util import dt as dt_util
|
|||||||
|
|
||||||
from .const import ATTR_DATETIME, DOMAIN, SERVICE_SET_VALUE
|
from .const import ATTR_DATETIME, 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 + ".{}"
|
||||||
@ -74,7 +79,12 @@ class DateTimeEntityDescription(EntityDescription, frozen_or_thawed=True):
|
|||||||
"""A class that describes date/time entities."""
|
"""A class that describes date/time entities."""
|
||||||
|
|
||||||
|
|
||||||
class DateTimeEntity(Entity):
|
CACHED_PROPERTIES_WITH_ATTR_ = {
|
||||||
|
"native_value",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class DateTimeEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
|
||||||
"""Representation of a Date/time entity."""
|
"""Representation of a Date/time entity."""
|
||||||
|
|
||||||
entity_description: DateTimeEntityDescription
|
entity_description: DateTimeEntityDescription
|
||||||
@ -82,13 +92,13 @@ class DateTimeEntity(Entity):
|
|||||||
_attr_state: None = None
|
_attr_state: None = None
|
||||||
_attr_native_value: datetime | None
|
_attr_native_value: datetime | None
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
@final
|
@final
|
||||||
def device_class(self) -> None:
|
def device_class(self) -> None:
|
||||||
"""Return entity device class."""
|
"""Return entity device class."""
|
||||||
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."""
|
||||||
@ -108,7 +118,7 @@ class DateTimeEntity(Entity):
|
|||||||
|
|
||||||
return value.astimezone(UTC).isoformat(timespec="seconds")
|
return value.astimezone(UTC).isoformat(timespec="seconds")
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def native_value(self) -> datetime | None:
|
def native_value(self) -> datetime | None:
|
||||||
"""Return the value reported by the datetime."""
|
"""Return the value reported by the datetime."""
|
||||||
return self._attr_native_value
|
return self._attr_native_value
|
||||||
|
Loading…
x
Reference in New Issue
Block a user