From c9c072ff3e5ca98eba24a8fe53632ad56bcf4406 Mon Sep 17 00:00:00 2001
From: Robert Resch <robert@resch.dev>
Date: Wed, 20 Dec 2023 17:54:43 +0100
Subject: [PATCH] Deprecate deprecated fan constants (#106111)

---
 homeassistant/components/fan/__init__.py | 25 ++++++++++++++++++++----
 tests/components/fan/test_init.py        | 11 +++++++++++
 tests/components/tasmota/test_fan.py     |  2 +-
 3 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/homeassistant/components/fan/__init__.py b/homeassistant/components/fan/__init__.py
index 23261c4d944..ec6fc1aad7e 100644
--- a/homeassistant/components/fan/__init__.py
+++ b/homeassistant/components/fan/__init__.py
@@ -24,6 +24,11 @@ from homeassistant.helpers.config_validation import (  # noqa: F401
     PLATFORM_SCHEMA,
     PLATFORM_SCHEMA_BASE,
 )
+from homeassistant.helpers.deprecation import (
+    DeprecatedConstantEnum,
+    check_if_deprecated_constant,
+    dir_with_deprecated_constants,
+)
 from homeassistant.helpers.entity import ToggleEntity, ToggleEntityDescription
 from homeassistant.helpers.entity_component import EntityComponent
 from homeassistant.helpers.typing import ConfigType
@@ -52,10 +57,22 @@ class FanEntityFeature(IntFlag):
 
 # These SUPPORT_* constants are deprecated as of Home Assistant 2022.5.
 # Please use the FanEntityFeature enum instead.
-SUPPORT_SET_SPEED = 1
-SUPPORT_OSCILLATE = 2
-SUPPORT_DIRECTION = 4
-SUPPORT_PRESET_MODE = 8
+_DEPRECATED_SUPPORT_SET_SPEED = DeprecatedConstantEnum(
+    FanEntityFeature.SET_SPEED, "2025.1"
+)
+_DEPRECATED_SUPPORT_OSCILLATE = DeprecatedConstantEnum(
+    FanEntityFeature.OSCILLATE, "2025.1"
+)
+_DEPRECATED_SUPPORT_DIRECTION = DeprecatedConstantEnum(
+    FanEntityFeature.DIRECTION, "2025.1"
+)
+_DEPRECATED_SUPPORT_PRESET_MODE = DeprecatedConstantEnum(
+    FanEntityFeature.PRESET_MODE, "2025.1"
+)
+
+# Both can be removed if no deprecated constant are in this module anymore
+__getattr__ = ft.partial(check_if_deprecated_constant, module_globals=globals())
+__dir__ = ft.partial(dir_with_deprecated_constants, module_globals=globals())
 
 SERVICE_INCREASE_SPEED = "increase_speed"
 SERVICE_DECREASE_SPEED = "decrease_speed"
diff --git a/tests/components/fan/test_init.py b/tests/components/fan/test_init.py
index ec421141768..e6a3ab546cc 100644
--- a/tests/components/fan/test_init.py
+++ b/tests/components/fan/test_init.py
@@ -1,6 +1,7 @@
 """Tests for fan platforms."""
 import pytest
 
+from homeassistant.components import fan
 from homeassistant.components.fan import (
     ATTR_PRESET_MODE,
     ATTR_PRESET_MODES,
@@ -13,6 +14,7 @@ from homeassistant.core import HomeAssistant
 import homeassistant.helpers.entity_registry as er
 from homeassistant.setup import async_setup_component
 
+from tests.common import import_and_test_deprecated_constant_enum
 from tests.testing_config.custom_components.test.fan import MockFan
 
 
@@ -145,3 +147,12 @@ async def test_preset_mode_validation(
     with pytest.raises(NotValidPresetModeError) as exc:
         await test_fan._valid_preset_mode_or_raise("invalid")
     assert exc.value.translation_key == "not_valid_preset_mode"
+
+
+@pytest.mark.parametrize(("enum"), list(fan.FanEntityFeature))
+def test_deprecated_constants(
+    caplog: pytest.LogCaptureFixture,
+    enum: fan.FanEntityFeature,
+) -> None:
+    """Test deprecated constants."""
+    import_and_test_deprecated_constant_enum(caplog, fan, enum, "SUPPORT_", "2025.1")
diff --git a/tests/components/tasmota/test_fan.py b/tests/components/tasmota/test_fan.py
index 05e3151be2e..727fddc9bd3 100644
--- a/tests/components/tasmota/test_fan.py
+++ b/tests/components/tasmota/test_fan.py
@@ -60,7 +60,7 @@ async def test_controlling_state_via_mqtt(
     state = hass.states.get("fan.tasmota")
     assert state.state == STATE_OFF
     assert state.attributes["percentage"] is None
-    assert state.attributes["supported_features"] == fan.SUPPORT_SET_SPEED
+    assert state.attributes["supported_features"] == fan.FanEntityFeature.SET_SPEED
     assert not state.attributes.get(ATTR_ASSUMED_STATE)
 
     async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/STATE", '{"FanSpeed":1}')
-- 
GitLab