Add support for attribute caching to the button platform (#106259)
This commit is contained in:
parent
98dd69ba09
commit
ef0031cbcf
1 changed files with 13 additions and 3 deletions
|
@ -4,7 +4,7 @@ from __future__ import annotations
|
|||
from datetime import datetime, timedelta
|
||||
from enum import StrEnum
|
||||
import logging
|
||||
from typing import final
|
||||
from typing import TYPE_CHECKING, final
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
|
@ -22,6 +22,11 @@ from homeassistant.util import dt as dt_util
|
|||
|
||||
from .const import DOMAIN, SERVICE_PRESS
|
||||
|
||||
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 + ".{}"
|
||||
|
@ -78,7 +83,12 @@ class ButtonEntityDescription(EntityDescription, frozen_or_thawed=True):
|
|||
device_class: ButtonDeviceClass | None = None
|
||||
|
||||
|
||||
class ButtonEntity(RestoreEntity):
|
||||
CACHED_PROPERTIES_WITH_ATTR_ = {
|
||||
"device_class",
|
||||
}
|
||||
|
||||
|
||||
class ButtonEntity(RestoreEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
|
||||
"""Representation of a Button entity."""
|
||||
|
||||
entity_description: ButtonEntityDescription
|
||||
|
@ -94,7 +104,7 @@ class ButtonEntity(RestoreEntity):
|
|||
"""
|
||||
return self.device_class is not None
|
||||
|
||||
@property
|
||||
@cached_property
|
||||
def device_class(self) -> ButtonDeviceClass | None:
|
||||
"""Return the class of this entity."""
|
||||
if hasattr(self, "_attr_device_class"):
|
||||
|
|
Loading…
Add table
Reference in a new issue