Add support for attribute caching to the humidifier platform (#106271)

This commit is contained in:
J. Nick Koston 2023-12-22 15:22:06 -10:00 committed by GitHub
parent 963347b9c5
commit 634551dae0
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 enum import StrEnum
from functools import partial from functools import partial
import logging import logging
from typing import Any, final from typing import TYPE_CHECKING, Any, final
import voluptuous as vol import voluptuous as vol
@ -54,6 +54,12 @@ from .const import ( # noqa: F401
HumidifierEntityFeature, HumidifierEntityFeature,
) )
if TYPE_CHECKING:
from functools import cached_property
else:
from homeassistant.backports.functools import cached_property
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -140,7 +146,20 @@ class HumidifierEntityDescription(ToggleEntityDescription, frozen_or_thawed=True
device_class: HumidifierDeviceClass | None = None device_class: HumidifierDeviceClass | None = None
class HumidifierEntity(ToggleEntity): CACHED_PROPERTIES_WITH_ATTR_ = {
"device_class",
"action",
"current_humidity",
"target_humidity",
"mode",
"available_modes",
"min_humidity",
"max_humidity",
"supported_features",
}
class HumidifierEntity(ToggleEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
"""Base class for humidifier entities.""" """Base class for humidifier entities."""
_entity_component_unrecorded_attributes = frozenset( _entity_component_unrecorded_attributes = frozenset(
@ -171,7 +190,7 @@ class HumidifierEntity(ToggleEntity):
return data return data
@property @cached_property
def device_class(self) -> HumidifierDeviceClass | None: def device_class(self) -> HumidifierDeviceClass | 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"):
@ -200,22 +219,22 @@ class HumidifierEntity(ToggleEntity):
return data return data
@property @cached_property
def action(self) -> HumidifierAction | None: def action(self) -> HumidifierAction | None:
"""Return the current action.""" """Return the current action."""
return self._attr_action return self._attr_action
@property @cached_property
def current_humidity(self) -> int | None: def current_humidity(self) -> int | None:
"""Return the current humidity.""" """Return the current humidity."""
return self._attr_current_humidity return self._attr_current_humidity
@property @cached_property
def target_humidity(self) -> int | None: def target_humidity(self) -> int | None:
"""Return the humidity we try to reach.""" """Return the humidity we try to reach."""
return self._attr_target_humidity return self._attr_target_humidity
@property @cached_property
def mode(self) -> str | None: def mode(self) -> str | None:
"""Return the current mode, e.g., home, auto, baby. """Return the current mode, e.g., home, auto, baby.
@ -223,7 +242,7 @@ class HumidifierEntity(ToggleEntity):
""" """
return self._attr_mode return self._attr_mode
@property @cached_property
def available_modes(self) -> list[str] | None: def available_modes(self) -> list[str] | None:
"""Return a list of available modes. """Return a list of available modes.
@ -247,17 +266,17 @@ class HumidifierEntity(ToggleEntity):
"""Set new mode.""" """Set new mode."""
await self.hass.async_add_executor_job(self.set_mode, mode) await self.hass.async_add_executor_job(self.set_mode, mode)
@property @cached_property
def min_humidity(self) -> int: def min_humidity(self) -> int:
"""Return the minimum humidity.""" """Return the minimum humidity."""
return self._attr_min_humidity return self._attr_min_humidity
@property @cached_property
def max_humidity(self) -> int: def max_humidity(self) -> int:
"""Return the maximum humidity.""" """Return the maximum humidity."""
return self._attr_max_humidity return self._attr_max_humidity
@property @cached_property
def supported_features(self) -> HumidifierEntityFeature: def supported_features(self) -> HumidifierEntityFeature:
"""Return the list of supported features.""" """Return the list of supported features."""
return self._attr_supported_features return self._attr_supported_features