Add support for attribute caching to the time platform (#106339)
parent
b6c2842b01
commit
b5e1074062
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||
|
||||
from datetime import time, 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 TimeEntityDescription(EntityDescription, frozen_or_thawed=True):
|
|||
"""A class that describes time entities."""
|
||||
|
||||
|
||||
class TimeEntity(Entity):
|
||||
CACHED_PROPERTIES_WITH_ATTR_ = {"native_value"}
|
||||
|
||||
|
||||
class TimeEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
|
||||
"""Representation of a Time entity."""
|
||||
|
||||
entity_description: TimeEntityDescription
|
||||
|
@ -73,13 +82,13 @@ class TimeEntity(Entity):
|
|||
_attr_device_class: None = 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 TimeEntity(Entity):
|
|||
return None
|
||||
return self.native_value.isoformat()
|
||||
|
||||
@property
|
||||
@cached_property
|
||||
def native_value(self) -> time | None:
|
||||
"""Return the value reported by the time."""
|
||||
return self._attr_native_value
|
||||
|
|
Loading…
Reference in New Issue