Add support for attribute caching to the siren platform (#106337)

This commit is contained in:
J. Nick Koston 2023-12-23 14:27:38 -10:00 committed by GitHub
parent 85e9bc6f5a
commit 68974a849f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from datetime import timedelta from datetime import timedelta
from functools import partial from functools import partial
import logging import logging
from typing import Any, TypedDict, cast, final from typing import TYPE_CHECKING, Any, TypedDict, cast, final
import voluptuous as vol import voluptuous as vol
@ -38,6 +38,11 @@ from .const import ( # noqa: F401
SirenEntityFeature, SirenEntityFeature,
) )
if TYPE_CHECKING:
from functools import cached_property
else:
from homeassistant.backports.functools import cached_property
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=60) SCAN_INTERVAL = timedelta(seconds=60)
@ -165,7 +170,13 @@ class SirenEntityDescription(ToggleEntityDescription, frozen_or_thawed=True):
available_tones: list[int | str] | dict[int, str] | None = None available_tones: list[int | str] | dict[int, str] | None = None
class SirenEntity(ToggleEntity): CACHED_PROPERTIES_WITH_ATTR_ = {
"available_tones",
"supported_features",
}
class SirenEntity(ToggleEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
"""Representation of a siren device.""" """Representation of a siren device."""
_entity_component_unrecorded_attributes = frozenset({ATTR_AVAILABLE_TONES}) _entity_component_unrecorded_attributes = frozenset({ATTR_AVAILABLE_TONES})
@ -186,7 +197,7 @@ class SirenEntity(ToggleEntity):
return None return None
@property @cached_property
def available_tones(self) -> list[int | str] | dict[int, str] | None: def available_tones(self) -> list[int | str] | dict[int, str] | None:
"""Return a list of available tones. """Return a list of available tones.
@ -198,7 +209,7 @@ class SirenEntity(ToggleEntity):
return self.entity_description.available_tones return self.entity_description.available_tones
return None return None
@property @cached_property
def supported_features(self) -> SirenEntityFeature: def supported_features(self) -> SirenEntityFeature:
"""Return the list of supported features.""" """Return the list of supported features."""
return self._attr_supported_features return self._attr_supported_features