Skip to content

Commit

Permalink
Add support for attribute caching to the button platform (home-assist…
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored and Bre77 committed Dec 23, 2023
1 parent 3c90bbf commit 6e07700
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions homeassistant/components/button/__init__.py
Expand Up @@ -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

Expand All @@ -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 + ".{}"
Expand Down Expand Up @@ -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
Expand All @@ -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"):
Expand Down

0 comments on commit 6e07700

Please sign in to comment.