From 43757ecea549f5f0f84a68f54b66d8e013613f67 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 23 Dec 2023 08:05:00 -1000 Subject: [PATCH] Add support for attribute caching to the select platform (#106255) --- homeassistant/components/select/__init__.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/select/__init__.py b/homeassistant/components/select/__init__.py index 9c978555dd5..459083cedd4 100644 --- a/homeassistant/components/select/__init__.py +++ b/homeassistant/components/select/__init__.py @@ -3,7 +3,7 @@ from __future__ import annotations from datetime import timedelta import logging -from typing import Any, final +from typing import TYPE_CHECKING, Any, final import voluptuous as vol @@ -30,6 +30,11 @@ from .const import ( SERVICE_SELECT_PREVIOUS, ) +if TYPE_CHECKING: + from functools import cached_property +else: + from homeassistant.backports.functools import cached_property + SCAN_INTERVAL = timedelta(seconds=30) ENTITY_ID_FORMAT = DOMAIN + ".{}" @@ -123,7 +128,13 @@ class SelectEntityDescription(EntityDescription, frozen_or_thawed=True): options: list[str] | None = None -class SelectEntity(Entity): +CACHED_PROPERTIES_WITH_ATTR_ = { + "current_option", + "options", +} + + +class SelectEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): """Representation of a Select entity.""" _entity_component_unrecorded_attributes = frozenset({ATTR_OPTIONS}) @@ -149,7 +160,7 @@ class SelectEntity(Entity): return None return current_option - @property + @cached_property def options(self) -> list[str]: """Return a set of selectable options.""" if hasattr(self, "_attr_options"): @@ -161,7 +172,7 @@ class SelectEntity(Entity): return self.entity_description.options raise AttributeError() - @property + @cached_property def current_option(self) -> str | None: """Return the selected entity option to represent the entity state.""" return self._attr_current_option