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

This commit is contained in:
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

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, Final, final from typing import TYPE_CHECKING, Any, Final, final
import voluptuous as vol import voluptuous as vol
@ -47,6 +47,11 @@ from .const import ( # noqa: F401
CodeFormat, 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 # As we import constants of the cost module here, we need to add the following
# functions to check for deprecated constants again # functions to check for deprecated constants again
# Both can be removed if no deprecated constant are in this module anymore # 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.""" """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.""" """An abstract class for alarm control entities."""
entity_description: AlarmControlPanelEntityDescription entity_description: AlarmControlPanelEntityDescription
@ -146,17 +159,17 @@ class AlarmControlPanelEntity(Entity):
AlarmControlPanelEntityFeature(0) AlarmControlPanelEntityFeature(0)
) )
@property @cached_property
def code_format(self) -> CodeFormat | None: def code_format(self) -> CodeFormat | None:
"""Code format or None if no code is required.""" """Code format or None if no code is required."""
return self._attr_code_format return self._attr_code_format
@property @cached_property
def changed_by(self) -> str | None: def changed_by(self) -> str | None:
"""Last change triggered by.""" """Last change triggered by."""
return self._attr_changed_by return self._attr_changed_by
@property @cached_property
def code_arm_required(self) -> bool: def code_arm_required(self) -> bool:
"""Whether the code is required for arm actions.""" """Whether the code is required for arm actions."""
return self._attr_code_arm_required return self._attr_code_arm_required
@ -217,7 +230,7 @@ class AlarmControlPanelEntity(Entity):
"""Send arm custom bypass command.""" """Send arm custom bypass command."""
await self.hass.async_add_executor_job(self.alarm_arm_custom_bypass, code) await self.hass.async_add_executor_job(self.alarm_arm_custom_bypass, code)
@property @cached_property
def supported_features(self) -> AlarmControlPanelEntityFeature: def supported_features(self) -> AlarmControlPanelEntityFeature:
"""Return the list of supported features.""" """Return the list of supported features."""
return self._attr_supported_features return self._attr_supported_features