Add support for attribute caching to the vacuum platform (#106332)

This commit is contained in:
J. Nick Koston 2023-12-23 15:36:45 -10:00 committed by GitHub
parent c7cb508896
commit 3d6fb60e93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,7 +7,7 @@ from datetime import timedelta
from enum import IntFlag from enum import IntFlag
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
@ -45,6 +45,11 @@ from homeassistant.loader import (
bind_hass, bind_hass,
) )
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 = "vacuum" DOMAIN = "vacuum"
@ -225,7 +230,16 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return await component.async_unload_entry(entry) return await component.async_unload_entry(entry)
class _BaseVacuum(Entity): BASE_CACHED_PROPERTIES_WITH_ATTR_ = {
"supported_features",
"battery_level",
"battery_icon",
"fan_speed",
"fan_speed_list",
}
class _BaseVacuum(Entity, cached_properties=BASE_CACHED_PROPERTIES_WITH_ATTR_):
"""Representation of a base vacuum. """Representation of a base vacuum.
Contains common properties and functions for all vacuum devices. Contains common properties and functions for all vacuum devices.
@ -239,27 +253,27 @@ class _BaseVacuum(Entity):
_attr_fan_speed_list: list[str] _attr_fan_speed_list: list[str]
_attr_supported_features: VacuumEntityFeature = VacuumEntityFeature(0) _attr_supported_features: VacuumEntityFeature = VacuumEntityFeature(0)
@property @cached_property
def supported_features(self) -> VacuumEntityFeature: def supported_features(self) -> VacuumEntityFeature:
"""Flag vacuum cleaner features that are supported.""" """Flag vacuum cleaner features that are supported."""
return self._attr_supported_features return self._attr_supported_features
@property @cached_property
def battery_level(self) -> int | None: def battery_level(self) -> int | None:
"""Return the battery level of the vacuum cleaner.""" """Return the battery level of the vacuum cleaner."""
return self._attr_battery_level return self._attr_battery_level
@property @cached_property
def battery_icon(self) -> str: def battery_icon(self) -> str:
"""Return the battery icon for the vacuum cleaner.""" """Return the battery icon for the vacuum cleaner."""
return self._attr_battery_icon return self._attr_battery_icon
@property @cached_property
def fan_speed(self) -> str | None: def fan_speed(self) -> str | None:
"""Return the fan speed of the vacuum cleaner.""" """Return the fan speed of the vacuum cleaner."""
return self._attr_fan_speed return self._attr_fan_speed
@property @cached_property
def fan_speed_list(self) -> list[str]: def fan_speed_list(self) -> list[str]:
"""Get the list of available fan speed steps of the vacuum cleaner.""" """Get the list of available fan speed steps of the vacuum cleaner."""
return self._attr_fan_speed_list return self._attr_fan_speed_list
@ -370,7 +384,14 @@ class VacuumEntityDescription(ToggleEntityDescription, frozen_or_thawed=True):
"""A class that describes vacuum entities.""" """A class that describes vacuum entities."""
class VacuumEntity(_BaseVacuum, ToggleEntity): VACUUM_CACHED_PROPERTIES_WITH_ATTR_ = {
"status",
}
class VacuumEntity(
_BaseVacuum, ToggleEntity, cached_properties=VACUUM_CACHED_PROPERTIES_WITH_ATTR_
):
"""Representation of a vacuum cleaner robot.""" """Representation of a vacuum cleaner robot."""
@callback @callback
@ -428,7 +449,7 @@ class VacuumEntity(_BaseVacuum, ToggleEntity):
entity_description: VacuumEntityDescription entity_description: VacuumEntityDescription
_attr_status: str | None = None _attr_status: str | None = None
@property @cached_property
def status(self) -> str | None: def status(self) -> str | None:
"""Return the status of the vacuum cleaner.""" """Return the status of the vacuum cleaner."""
return self._attr_status return self._attr_status
@ -492,13 +513,20 @@ class StateVacuumEntityDescription(EntityDescription, frozen_or_thawed=True):
"""A class that describes vacuum entities.""" """A class that describes vacuum entities."""
class StateVacuumEntity(_BaseVacuum): STATE_VACUUM_CACHED_PROPERTIES_WITH_ATTR_ = {
"state",
}
class StateVacuumEntity(
_BaseVacuum, cached_properties=STATE_VACUUM_CACHED_PROPERTIES_WITH_ATTR_
):
"""Representation of a vacuum cleaner robot that supports states.""" """Representation of a vacuum cleaner robot that supports states."""
entity_description: StateVacuumEntityDescription entity_description: StateVacuumEntityDescription
_attr_state: str | None = None _attr_state: str | None = None
@property @cached_property
def state(self) -> str | None: def state(self) -> str | None:
"""Return the state of the vacuum cleaner.""" """Return the state of the vacuum cleaner."""
return self._attr_state return self._attr_state