Add support for attribute caching to the binary_sensor platform (#106253)

This commit is contained in:
J. Nick Koston 2023-12-23 00:11:50 -10:00 committed by GitHub
parent 1c8d961832
commit 97ed6570a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,7 +5,7 @@ from datetime import timedelta
from enum import StrEnum
from functools import partial
import logging
from typing import Literal, final
from typing import TYPE_CHECKING, Literal, final
import voluptuous as vol
@ -26,8 +26,14 @@ from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.typing import ConfigType
if TYPE_CHECKING:
from functools import cached_property
else:
from homeassistant.backports.functools import cached_property
_LOGGER = logging.getLogger(__name__)
DOMAIN = "binary_sensor"
SCAN_INTERVAL = timedelta(seconds=30)
@ -247,7 +253,13 @@ class BinarySensorEntityDescription(EntityDescription, frozen_or_thawed=True):
device_class: BinarySensorDeviceClass | None = None
class BinarySensorEntity(Entity):
CACHED_PROPERTIES_WITH_ATTR_ = {
"device_class",
"is_on",
}
class BinarySensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
"""Represent a binary sensor."""
entity_description: BinarySensorEntityDescription
@ -270,7 +282,7 @@ class BinarySensorEntity(Entity):
"""
return self.device_class is not None
@property
@cached_property
def device_class(self) -> BinarySensorDeviceClass | None:
"""Return the class of this entity."""
if hasattr(self, "_attr_device_class"):
@ -279,7 +291,7 @@ class BinarySensorEntity(Entity):
return self.entity_description.device_class
return None
@property
@cached_property
def is_on(self) -> bool | None:
"""Return true if the binary sensor is on."""
return self._attr_is_on