From 90b54cd1e22c647dccfd96e1c4bb7c7b7f8e08b7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 22 Dec 2023 09:21:39 -1000 Subject: [PATCH] Add support for attribute caching to the button platform Builds on https://github.com/home-assistant/core/pull/100601 --- homeassistant/components/button/__init__.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/button/__init__.py b/homeassistant/components/button/__init__.py index 4ebe1df68a2c5..358348a807763 100644 --- a/homeassistant/components/button/__init__.py +++ b/homeassistant/components/button/__init__.py @@ -4,7 +4,7 @@ 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 .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 @@ def _default_to_device_class_name(self) -> bool: """ 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"):