From 83e1ba338af00db6dda943c811664ff6d34f2e32 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 23 Dec 2023 09:22:02 -1000 Subject: [PATCH] Add support for attribute caching to the switch platform (#106258) --- homeassistant/components/switch/__init__.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/switch/__init__.py b/homeassistant/components/switch/__init__.py index 1d0654cd815..a318f763fcb 100644 --- a/homeassistant/components/switch/__init__.py +++ b/homeassistant/components/switch/__init__.py @@ -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"):