Skip to content

Commit

Permalink
Add support for attribute caching to the humidifer 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 e555378
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions homeassistant/components/humidifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from enum import StrEnum
from functools import partial
import logging
from typing import Any, final
from typing import TYPE_CHECKING, Any, final

import voluptuous as vol

Expand Down Expand Up @@ -54,6 +54,12 @@
HumidifierEntityFeature,
)

if TYPE_CHECKING:
from functools import cached_property
else:
from homeassistant.backports.functools import cached_property


_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -140,7 +146,20 @@ class HumidifierEntityDescription(ToggleEntityDescription, frozen_or_thawed=True
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."""

_entity_component_unrecorded_attributes = frozenset(
Expand Down Expand Up @@ -171,7 +190,7 @@ def capability_attributes(self) -> dict[str, Any]:

return data

@property
@cached_property
def device_class(self) -> HumidifierDeviceClass | None:
"""Return the class of this entity."""
if hasattr(self, "_attr_device_class"):
Expand Down Expand Up @@ -200,30 +219,30 @@ def state_attributes(self) -> dict[str, Any]:

return data

@property
@cached_property
def action(self) -> HumidifierAction | None:
"""Return the current action."""
return self._attr_action

@property
@cached_property
def current_humidity(self) -> int | None:
"""Return the current humidity."""
return self._attr_current_humidity

@property
@cached_property
def target_humidity(self) -> int | None:
"""Return the humidity we try to reach."""
return self._attr_target_humidity

@property
@cached_property
def mode(self) -> str | None:
"""Return the current mode, e.g., home, auto, baby.
Requires HumidifierEntityFeature.MODES.
"""
return self._attr_mode

@property
@cached_property
def available_modes(self) -> list[str] | None:
"""Return a list of available modes.
Expand All @@ -247,17 +266,17 @@ async def async_set_mode(self, mode: str) -> None:
"""Set new mode."""
await self.hass.async_add_executor_job(self.set_mode, mode)

@property
@cached_property
def min_humidity(self) -> int:
"""Return the minimum humidity."""
return self._attr_min_humidity

@property
@cached_property
def max_humidity(self) -> int:
"""Return the maximum humidity."""
return self._attr_max_humidity

@property
@cached_property
def supported_features(self) -> HumidifierEntityFeature:
"""Return the list of supported features."""
return self._attr_supported_features

0 comments on commit e555378

Please sign in to comment.