mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Add support for attribute caching to the binary_sensor platform (#106253)
This commit is contained in:
parent
1c8d961832
commit
97ed6570a7
@ -5,7 +5,7 @@ from datetime import timedelta
|
|||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import logging
|
import logging
|
||||||
from typing import Literal, final
|
from typing import TYPE_CHECKING, Literal, final
|
||||||
|
|
||||||
import voluptuous as vol
|
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.entity_component import EntityComponent
|
||||||
from homeassistant.helpers.typing import ConfigType
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
DOMAIN = "binary_sensor"
|
DOMAIN = "binary_sensor"
|
||||||
SCAN_INTERVAL = timedelta(seconds=30)
|
SCAN_INTERVAL = timedelta(seconds=30)
|
||||||
|
|
||||||
@ -247,7 +253,13 @@ class BinarySensorEntityDescription(EntityDescription, frozen_or_thawed=True):
|
|||||||
device_class: BinarySensorDeviceClass | None = None
|
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."""
|
"""Represent a binary sensor."""
|
||||||
|
|
||||||
entity_description: BinarySensorEntityDescription
|
entity_description: BinarySensorEntityDescription
|
||||||
@ -270,7 +282,7 @@ class BinarySensorEntity(Entity):
|
|||||||
"""
|
"""
|
||||||
return self.device_class is not None
|
return self.device_class is not None
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def device_class(self) -> BinarySensorDeviceClass | None:
|
def device_class(self) -> BinarySensorDeviceClass | None:
|
||||||
"""Return the class of this entity."""
|
"""Return the class of this entity."""
|
||||||
if hasattr(self, "_attr_device_class"):
|
if hasattr(self, "_attr_device_class"):
|
||||||
@ -279,7 +291,7 @@ class BinarySensorEntity(Entity):
|
|||||||
return self.entity_description.device_class
|
return self.entity_description.device_class
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def is_on(self) -> bool | None:
|
def is_on(self) -> bool | None:
|
||||||
"""Return true if the binary sensor is on."""
|
"""Return true if the binary sensor is on."""
|
||||||
return self._attr_is_on
|
return self._attr_is_on
|
||||||
|
Loading…
x
Reference in New Issue
Block a user