mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add support for property attribute shorthand in Fan entity (#59649)
This commit is contained in:
parent
0228d11546
commit
771922b871
@ -235,6 +235,13 @@ class FanEntity(ToggleEntity):
|
|||||||
"""Base class for fan entities."""
|
"""Base class for fan entities."""
|
||||||
|
|
||||||
entity_description: FanEntityDescription
|
entity_description: FanEntityDescription
|
||||||
|
_attr_current_direction: str | None = None
|
||||||
|
_attr_oscillating: bool | None = None
|
||||||
|
_attr_percentage: int | None
|
||||||
|
_attr_preset_mode: str | None
|
||||||
|
_attr_preset_modes: list[str] | None
|
||||||
|
_attr_speed_count: int
|
||||||
|
_attr_supported_features: int = 0
|
||||||
|
|
||||||
@_fan_native
|
@_fan_native
|
||||||
def set_speed(self, speed: str) -> None:
|
def set_speed(self, speed: str) -> None:
|
||||||
@ -469,6 +476,9 @@ class FanEntity(ToggleEntity):
|
|||||||
@property
|
@property
|
||||||
def percentage(self) -> int | None:
|
def percentage(self) -> int | None:
|
||||||
"""Return the current speed as a percentage."""
|
"""Return the current speed as a percentage."""
|
||||||
|
if hasattr(self, "_attr_percentage"):
|
||||||
|
return self._attr_percentage
|
||||||
|
|
||||||
if (
|
if (
|
||||||
not self._implemented_preset_mode
|
not self._implemented_preset_mode
|
||||||
and self.preset_modes
|
and self.preset_modes
|
||||||
@ -482,6 +492,9 @@ class FanEntity(ToggleEntity):
|
|||||||
@property
|
@property
|
||||||
def speed_count(self) -> int:
|
def speed_count(self) -> int:
|
||||||
"""Return the number of speeds the fan supports."""
|
"""Return the number of speeds the fan supports."""
|
||||||
|
if hasattr(self, "_attr_speed_count"):
|
||||||
|
return self._attr_speed_count
|
||||||
|
|
||||||
speed_list = speed_list_without_preset_modes(self.speed_list)
|
speed_list = speed_list_without_preset_modes(self.speed_list)
|
||||||
if speed_list:
|
if speed_list:
|
||||||
return len(speed_list)
|
return len(speed_list)
|
||||||
@ -505,12 +518,12 @@ class FanEntity(ToggleEntity):
|
|||||||
@property
|
@property
|
||||||
def current_direction(self) -> str | None:
|
def current_direction(self) -> str | None:
|
||||||
"""Return the current direction of the fan."""
|
"""Return the current direction of the fan."""
|
||||||
return None
|
return self._attr_current_direction
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def oscillating(self):
|
def oscillating(self) -> bool | None:
|
||||||
"""Return whether or not the fan is currently oscillating."""
|
"""Return whether or not the fan is currently oscillating."""
|
||||||
return None
|
return self._attr_oscillating
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def capability_attributes(self):
|
def capability_attributes(self):
|
||||||
@ -629,7 +642,7 @@ class FanEntity(ToggleEntity):
|
|||||||
@property
|
@property
|
||||||
def supported_features(self) -> int:
|
def supported_features(self) -> int:
|
||||||
"""Flag supported features."""
|
"""Flag supported features."""
|
||||||
return 0
|
return self._attr_supported_features
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def preset_mode(self) -> str | None:
|
def preset_mode(self) -> str | None:
|
||||||
@ -637,6 +650,9 @@ class FanEntity(ToggleEntity):
|
|||||||
|
|
||||||
Requires SUPPORT_SET_SPEED.
|
Requires SUPPORT_SET_SPEED.
|
||||||
"""
|
"""
|
||||||
|
if hasattr(self, "_attr_preset_mode"):
|
||||||
|
return self._attr_preset_mode
|
||||||
|
|
||||||
speed = self.speed
|
speed = self.speed
|
||||||
if self.preset_modes and speed in self.preset_modes:
|
if self.preset_modes and speed in self.preset_modes:
|
||||||
return speed
|
return speed
|
||||||
@ -648,6 +664,9 @@ class FanEntity(ToggleEntity):
|
|||||||
|
|
||||||
Requires SUPPORT_SET_SPEED.
|
Requires SUPPORT_SET_SPEED.
|
||||||
"""
|
"""
|
||||||
|
if hasattr(self, "_attr_preset_modes"):
|
||||||
|
return self._attr_preset_modes
|
||||||
|
|
||||||
return preset_modes_from_speed_list(self.speed_list)
|
return preset_modes_from_speed_list(self.speed_list)
|
||||||
|
|
||||||
|
|
||||||
|
@ -65,3 +65,22 @@ async def test_async_fanentity(hass):
|
|||||||
await fan.async_increase_speed()
|
await fan.async_increase_speed()
|
||||||
with pytest.raises(NotImplementedError):
|
with pytest.raises(NotImplementedError):
|
||||||
await fan.async_decrease_speed()
|
await fan.async_decrease_speed()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"attribute_name, attribute_value",
|
||||||
|
[
|
||||||
|
("current_direction", "forward"),
|
||||||
|
("oscillating", True),
|
||||||
|
("percentage", 50),
|
||||||
|
("preset_mode", "medium"),
|
||||||
|
("preset_modes", ["low", "medium", "high"]),
|
||||||
|
("speed_count", 50),
|
||||||
|
("supported_features", 1),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_fanentity_attributes(attribute_name, attribute_value):
|
||||||
|
"""Test fan entity attribute shorthand."""
|
||||||
|
fan = BaseFan()
|
||||||
|
setattr(fan, f"_attr_{attribute_name}", attribute_value)
|
||||||
|
assert getattr(fan, attribute_name) == attribute_value
|
||||||
|
Loading…
x
Reference in New Issue
Block a user