Skip to content

Commit

Permalink
Add support for attribute caching to the alarm_control_panel platform
Browse files Browse the repository at this point in the history
Builds on #100601
  • Loading branch information
bdraco committed Dec 22, 2023
1 parent 3a744d3 commit 6e5f1b2
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions homeassistant/components/alarm_control_panel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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

Expand Down Expand Up @@ -47,6 +47,11 @@
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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -217,7 +230,7 @@ async def async_alarm_arm_custom_bypass(self, code: str | None = None) -> None:
"""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
Expand Down

0 comments on commit 6e5f1b2

Please sign in to comment.