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 cover platform #106268

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