mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Vacuum platform back-compat for custom components without VacuumEntityFeature (#106614)
This commit is contained in:
parent
f03bb4a2da
commit
70842f197e
@ -258,6 +258,19 @@ class _BaseVacuum(Entity, cached_properties=BASE_CACHED_PROPERTIES_WITH_ATTR_):
|
|||||||
"""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
|
||||||
|
def supported_features_compat(self) -> VacuumEntityFeature:
|
||||||
|
"""Return the supported features as VacuumEntityFeature.
|
||||||
|
|
||||||
|
Remove this compatibility shim in 2025.1 or later.
|
||||||
|
"""
|
||||||
|
features = self.supported_features
|
||||||
|
if type(features) is int: # noqa: E721
|
||||||
|
new_features = VacuumEntityFeature(features)
|
||||||
|
self._report_deprecated_supported_features_values(new_features)
|
||||||
|
return new_features
|
||||||
|
return features
|
||||||
|
|
||||||
@cached_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."""
|
||||||
@ -281,7 +294,7 @@ class _BaseVacuum(Entity, cached_properties=BASE_CACHED_PROPERTIES_WITH_ATTR_):
|
|||||||
@property
|
@property
|
||||||
def capability_attributes(self) -> Mapping[str, Any] | None:
|
def capability_attributes(self) -> Mapping[str, Any] | None:
|
||||||
"""Return capability attributes."""
|
"""Return capability attributes."""
|
||||||
if VacuumEntityFeature.FAN_SPEED in self.supported_features:
|
if VacuumEntityFeature.FAN_SPEED in self.supported_features_compat:
|
||||||
return {ATTR_FAN_SPEED_LIST: self.fan_speed_list}
|
return {ATTR_FAN_SPEED_LIST: self.fan_speed_list}
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -289,7 +302,7 @@ class _BaseVacuum(Entity, cached_properties=BASE_CACHED_PROPERTIES_WITH_ATTR_):
|
|||||||
def state_attributes(self) -> dict[str, Any]:
|
def state_attributes(self) -> dict[str, Any]:
|
||||||
"""Return the state attributes of the vacuum cleaner."""
|
"""Return the state attributes of the vacuum cleaner."""
|
||||||
data: dict[str, Any] = {}
|
data: dict[str, Any] = {}
|
||||||
supported_features = self.supported_features
|
supported_features = self.supported_features_compat
|
||||||
|
|
||||||
if VacuumEntityFeature.BATTERY in supported_features:
|
if VacuumEntityFeature.BATTERY in supported_features:
|
||||||
data[ATTR_BATTERY_LEVEL] = self.battery_level
|
data[ATTR_BATTERY_LEVEL] = self.battery_level
|
||||||
@ -471,7 +484,7 @@ class VacuumEntity(
|
|||||||
"""Return the state attributes of the vacuum cleaner."""
|
"""Return the state attributes of the vacuum cleaner."""
|
||||||
data = super().state_attributes
|
data = super().state_attributes
|
||||||
|
|
||||||
if VacuumEntityFeature.STATUS in self.supported_features:
|
if VacuumEntityFeature.STATUS in self.supported_features_compat:
|
||||||
data[ATTR_STATUS] = self.status
|
data[ATTR_STATUS] = self.status
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
@ -5,7 +5,11 @@ from collections.abc import Generator
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.vacuum import DOMAIN as VACUUM_DOMAIN, VacuumEntity
|
from homeassistant.components.vacuum import (
|
||||||
|
DOMAIN as VACUUM_DOMAIN,
|
||||||
|
VacuumEntity,
|
||||||
|
VacuumEntityFeature,
|
||||||
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry, ConfigFlow
|
from homeassistant.config_entries import ConfigEntry, ConfigFlow
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import issue_registry as ir
|
from homeassistant.helpers import issue_registry as ir
|
||||||
@ -121,3 +125,23 @@ async def test_deprecated_base_class(
|
|||||||
issue.translation_placeholders
|
issue.translation_placeholders
|
||||||
== {"platform": "test"} | translation_placeholders_extra
|
== {"platform": "test"} | translation_placeholders_extra
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_deprecated_supported_features_ints(caplog: pytest.LogCaptureFixture) -> None:
|
||||||
|
"""Test deprecated supported features ints."""
|
||||||
|
|
||||||
|
class MockVacuumEntity(VacuumEntity):
|
||||||
|
@property
|
||||||
|
def supported_features(self) -> int:
|
||||||
|
"""Return supported features."""
|
||||||
|
return 1
|
||||||
|
|
||||||
|
entity = MockVacuumEntity()
|
||||||
|
assert entity.supported_features_compat is VacuumEntityFeature(1)
|
||||||
|
assert "MockVacuumEntity" in caplog.text
|
||||||
|
assert "is using deprecated supported features values" in caplog.text
|
||||||
|
assert "Instead it should use" in caplog.text
|
||||||
|
assert "VacuumEntityFeature.TURN_ON" in caplog.text
|
||||||
|
caplog.clear()
|
||||||
|
assert entity.supported_features_compat is VacuumEntityFeature(1)
|
||||||
|
assert "is using deprecated supported features values" not in caplog.text
|
||||||
|
Loading…
x
Reference in New Issue
Block a user