Add support for attribute caching to the switch platform (#106258)

This commit is contained in:
J. Nick Koston 2023-12-23 09:22:02 -10:00 committed by GitHub
parent ebdf7b9c8c
commit 83e1ba338a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,7 @@ from datetime import timedelta
from enum import StrEnum
from functools import partial
import logging
from typing import TYPE_CHECKING
import voluptuous as vol
@ -32,6 +33,11 @@ from homeassistant.loader import bind_hass
from .const import DOMAIN
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 + ".{}"
@ -108,13 +114,18 @@ class SwitchEntityDescription(ToggleEntityDescription, frozen_or_thawed=True):
device_class: SwitchDeviceClass | None = None
class SwitchEntity(ToggleEntity):
CACHED_PROPERTIES_WITH_ATTR_ = {
"device_class",
}
class SwitchEntity(ToggleEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
"""Base class for switch entities."""
entity_description: SwitchEntityDescription
_attr_device_class: SwitchDeviceClass | None
@property
@cached_property
def device_class(self) -> SwitchDeviceClass | None:
"""Return the class of this entity."""
if hasattr(self, "_attr_device_class"):