Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for attribute caching to the button platform #106259

Merged
merged 1 commit into from Dec 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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