Add support for attribute caching to the alarm_control_panel platform (#106265)

pull/106314/head
J. Nick Koston 2023-12-23 08:04:11 -10:00 committed by GitHub
parent 4ee961cd51
commit 1631a52b09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 6 deletions

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from datetime import timedelta
from functools import partial
import logging
from typing import Any, Final, final
from typing import TYPE_CHECKING, Any, Final, final
import voluptuous as vol
@ -47,6 +47,11 @@ from .const import ( # noqa: F401
CodeFormat,
)
if TYPE_CHECKING:
from functools import cached_property
else:
from homeassistant.backports.functools import cached_property
# As we import constants of the cost module here, we need to add the following
# functions to check for deprecated constants again
# Both can be removed if no deprecated constant are in this module anymore
@ -135,7 +140,15 @@ class AlarmControlPanelEntityDescription(EntityDescription, frozen_or_thawed=Tru
"""A class that describes alarm control panel entities."""
class AlarmControlPanelEntity(Entity):
CACHED_PROPERTIES_WITH_ATTR_ = {
"code_format",
"changed_by",
"code_arm_required",
"supported_features",
}
class AlarmControlPanelEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
"""An abstract class for alarm control entities."""
entity_description: AlarmControlPanelEntityDescription
@ -146,17 +159,17 @@ class AlarmControlPanelEntity(Entity):
AlarmControlPanelEntityFeature(0)
)
@property
@cached_property
def code_format(self) -> CodeFormat | None:
"""Code format or None if no code is required."""
return self._attr_code_format
@property
@cached_property
def changed_by(self) -> str | None:
"""Last change triggered by."""
return self._attr_changed_by
@property
@cached_property
def code_arm_required(self) -> bool:
"""Whether the code is required for arm actions."""
return self._attr_code_arm_required
@ -217,7 +230,7 @@ class AlarmControlPanelEntity(Entity):
"""Send arm custom bypass command."""
await self.hass.async_add_executor_job(self.alarm_arm_custom_bypass, code)
@property
@cached_property
def supported_features(self) -> AlarmControlPanelEntityFeature:
"""Return the list of supported features."""
return self._attr_supported_features