Add support for attribute caching to the remote platform (#106274)

This commit is contained in:
J. Nick Koston 2023-12-23 00:09:18 -10:00 committed by GitHub
parent d1174593f9
commit 98dd69ba09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@ from datetime import timedelta
from enum import IntFlag from enum import IntFlag
import functools as ft import functools as ft
import logging import logging
from typing import Any, final from typing import TYPE_CHECKING, Any, final
import voluptuous as vol import voluptuous as vol
@ -35,6 +35,12 @@ from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import bind_hass 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__) _LOGGER = logging.getLogger(__name__)
ATTR_ACTIVITY = "activity" ATTR_ACTIVITY = "activity"
@ -174,7 +180,14 @@ class RemoteEntityDescription(ToggleEntityDescription, frozen_or_thawed=True):
"""A class that describes remote entities.""" """A class that describes remote entities."""
class RemoteEntity(ToggleEntity): CACHED_PROPERTIES_WITH_ATTR_ = {
"supported_features",
"current_activity",
"activity_list",
}
class RemoteEntity(ToggleEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
"""Base class for remote entities.""" """Base class for remote entities."""
entity_description: RemoteEntityDescription entity_description: RemoteEntityDescription
@ -182,17 +195,17 @@ class RemoteEntity(ToggleEntity):
_attr_current_activity: str | None = None _attr_current_activity: str | None = None
_attr_supported_features: RemoteEntityFeature = RemoteEntityFeature(0) _attr_supported_features: RemoteEntityFeature = RemoteEntityFeature(0)
@property @cached_property
def supported_features(self) -> RemoteEntityFeature: def supported_features(self) -> RemoteEntityFeature:
"""Flag supported features.""" """Flag supported features."""
return self._attr_supported_features return self._attr_supported_features
@property @cached_property
def current_activity(self) -> str | None: def current_activity(self) -> str | None:
"""Active activity.""" """Active activity."""
return self._attr_current_activity return self._attr_current_activity
@property @cached_property
def activity_list(self) -> list[str] | None: def activity_list(self) -> list[str] | None:
"""List of available activities.""" """List of available activities."""
return self._attr_activity_list return self._attr_activity_list