From 4ee961cd5154ba41fbbe986f09d2ed22aa3c4eea Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 23 Dec 2023 08:03:44 -1000 Subject: [PATCH] Add support for attribute caching to the cover platform (#106268) --- homeassistant/components/cover/__init__.py | 31 ++++++++++++++++------ 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/cover/__init__.py b/homeassistant/components/cover/__init__.py index 1f365b07099b92..1a21908860a3dd 100644 --- a/homeassistant/components/cover/__init__.py +++ b/homeassistant/components/cover/__init__.py @@ -6,7 +6,7 @@ from enum import IntFlag, StrEnum import functools as ft import logging -from typing import Any, ParamSpec, TypeVar, final +from typing import TYPE_CHECKING, Any, ParamSpec, TypeVar, final import voluptuous as vol @@ -42,6 +42,11 @@ from homeassistant.helpers.typing import ConfigType from homeassistant.loader import bind_hass +if TYPE_CHECKING: + from functools import cached_property +else: + from homeassistant.backports.functools import cached_property + _LOGGER = logging.getLogger(__name__) DOMAIN = "cover" @@ -252,7 +257,17 @@ class CoverEntityDescription(EntityDescription, frozen_or_thawed=True): device_class: CoverDeviceClass | None = None -class CoverEntity(Entity): +CACHED_PROPERTIES_WITH_ATTR_ = { + "current_cover_position", + "current_cover_tilt_position", + "device_class", + "is_opening", + "is_closing", + "is_closed", +} + + +class CoverEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): """Base class for cover entities.""" entity_description: CoverEntityDescription @@ -267,7 +282,7 @@ class CoverEntity(Entity): _cover_is_last_toggle_direction_open = True - @property + @cached_property def current_cover_position(self) -> int | None: """Return current position of cover. @@ -275,7 +290,7 @@ def current_cover_position(self) -> int | None: """ return self._attr_current_cover_position - @property + @cached_property def current_cover_tilt_position(self) -> int | None: """Return current position of cover tilt. @@ -283,7 +298,7 @@ def current_cover_tilt_position(self) -> int | None: """ return self._attr_current_cover_tilt_position - @property + @cached_property def device_class(self) -> CoverDeviceClass | None: """Return the class of this entity.""" if hasattr(self, "_attr_device_class"): @@ -345,17 +360,17 @@ def supported_features(self) -> CoverEntityFeature: return supported_features - @property + @cached_property def is_opening(self) -> bool | None: """Return if the cover is opening or not.""" return self._attr_is_opening - @property + @cached_property def is_closing(self) -> bool | None: """Return if the cover is closing or not.""" return self._attr_is_closing - @property + @cached_property def is_closed(self) -> bool | None: """Return if the cover is closed or not.""" return self._attr_is_closed