Add support for attribute caching to the number platform (#106254)

This commit is contained in:
J. Nick Koston 2023-12-22 15:22:18 -10:00 committed by GitHub
parent 634551dae0
commit 5d2ddcb1d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,7 @@ import dataclasses
from datetime import timedelta from datetime import timedelta
import logging import logging
from math import ceil, floor from math import ceil, floor
from typing import Any, Self, final from typing import TYPE_CHECKING, Any, Self, final
import voluptuous as vol import voluptuous as vol
@ -42,6 +42,11 @@ from .const import ( # noqa: F401
) )
from .websocket_api import async_setup as async_setup_ws_api from .websocket_api import async_setup as async_setup_ws_api
if TYPE_CHECKING:
from functools import cached_property
else:
from homeassistant.backports.functools import cached_property
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ENTITY_ID_FORMAT = DOMAIN + ".{}" ENTITY_ID_FORMAT = DOMAIN + ".{}"
@ -153,7 +158,18 @@ def floor_decimal(value: float, precision: float = 0) -> float:
return floor(value * factor) / factor return floor(value * factor) / factor
class NumberEntity(Entity): CACHED_PROPERTIES_WITH_ATTR_ = {
"device_class",
"native_max_value",
"native_min_value",
"native_step",
"mode",
"native_unit_of_measurement",
"native_value",
}
class NumberEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
"""Representation of a Number entity.""" """Representation of a Number entity."""
_entity_component_unrecorded_attributes = frozenset( _entity_component_unrecorded_attributes = frozenset(
@ -238,7 +254,7 @@ class NumberEntity(Entity):
""" """
return self.device_class is not None return self.device_class is not None
@property @cached_property
def device_class(self) -> NumberDeviceClass | None: def device_class(self) -> NumberDeviceClass | 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"):
@ -247,7 +263,7 @@ class NumberEntity(Entity):
return self.entity_description.device_class return self.entity_description.device_class
return None return None
@property @cached_property
def native_min_value(self) -> float: def native_min_value(self) -> float:
"""Return the minimum value.""" """Return the minimum value."""
if hasattr(self, "_attr_native_min_value"): if hasattr(self, "_attr_native_min_value"):
@ -267,7 +283,7 @@ class NumberEntity(Entity):
self.native_min_value, floor_decimal, self.device_class self.native_min_value, floor_decimal, self.device_class
) )
@property @cached_property
def native_max_value(self) -> float: def native_max_value(self) -> float:
"""Return the maximum value.""" """Return the maximum value."""
if hasattr(self, "_attr_native_max_value"): if hasattr(self, "_attr_native_max_value"):
@ -287,7 +303,7 @@ class NumberEntity(Entity):
self.native_max_value, ceil_decimal, self.device_class self.native_max_value, ceil_decimal, self.device_class
) )
@property @cached_property
def native_step(self) -> float | None: def native_step(self) -> float | None:
"""Return the increment/decrement step.""" """Return the increment/decrement step."""
if ( if (
@ -316,7 +332,7 @@ class NumberEntity(Entity):
step /= 10.0 step /= 10.0
return step return step
@property @cached_property
def mode(self) -> NumberMode: def mode(self) -> NumberMode:
"""Return the mode of the entity.""" """Return the mode of the entity."""
if hasattr(self, "_attr_mode"): if hasattr(self, "_attr_mode"):
@ -334,7 +350,7 @@ class NumberEntity(Entity):
"""Return the entity state.""" """Return the entity state."""
return self.value return self.value
@property @cached_property
def native_unit_of_measurement(self) -> str | None: def native_unit_of_measurement(self) -> str | None:
"""Return the unit of measurement of the entity, if any.""" """Return the unit of measurement of the entity, if any."""
if hasattr(self, "_attr_native_unit_of_measurement"): if hasattr(self, "_attr_native_unit_of_measurement"):
@ -362,7 +378,7 @@ class NumberEntity(Entity):
return native_unit_of_measurement return native_unit_of_measurement
@property @cached_property
def native_value(self) -> float | None: def native_value(self) -> float | None:
"""Return the value reported by the number.""" """Return the value reported by the number."""
return self._attr_native_value return self._attr_native_value