Skip to content

Commit

Permalink
Add support for attribute caching to the cover platform (#106268)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 23, 2023
1 parent 28dccc3 commit 4ee961c
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions homeassistant/components/cover/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -267,23 +282,23 @@ 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.
None is unknown, 0 is closed, 100 is fully open.
"""
return self._attr_current_cover_position

@property
@cached_property
def current_cover_tilt_position(self) -> int | None:
"""Return current position of cover tilt.
None is unknown, 0 is closed, 100 is fully open.
"""
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"):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4ee961c

Please sign in to comment.