diff --git a/homeassistant/components/humidifier/__init__.py b/homeassistant/components/humidifier/__init__.py index 39150126b7abe579f76b37eaaf012276b9a82d42..d9c804279b2794df1565315724d9855958fbf8cd 100644 --- a/homeassistant/components/humidifier/__init__.py +++ b/homeassistant/components/humidifier/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations from datetime import timedelta from enum import StrEnum +from functools import partial import logging from typing import Any, final @@ -22,12 +23,19 @@ from homeassistant.helpers.config_validation import ( # noqa: F401 PLATFORM_SCHEMA, PLATFORM_SCHEMA_BASE, ) +from homeassistant.helpers.deprecation import ( + 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 from homeassistant.loader import bind_hass from .const import ( # noqa: F401 + _DEPRECATED_DEVICE_CLASS_DEHUMIDIFIER, + _DEPRECATED_DEVICE_CLASS_HUMIDIFIER, + _DEPRECATED_SUPPORT_MODES, ATTR_ACTION, ATTR_AVAILABLE_MODES, ATTR_CURRENT_HUMIDITY, @@ -36,15 +44,12 @@ from .const import ( # noqa: F401 ATTR_MIN_HUMIDITY, DEFAULT_MAX_HUMIDITY, DEFAULT_MIN_HUMIDITY, - DEVICE_CLASS_DEHUMIDIFIER, - DEVICE_CLASS_HUMIDIFIER, DOMAIN, MODE_AUTO, MODE_AWAY, MODE_NORMAL, SERVICE_SET_HUMIDITY, SERVICE_SET_MODE, - SUPPORT_MODES, HumidifierAction, HumidifierEntityFeature, ) @@ -70,6 +75,12 @@ DEVICE_CLASSES_SCHEMA = vol.All(vol.Lower, vol.Coerce(HumidifierDeviceClass)) # use the HumidifierDeviceClass enum instead. DEVICE_CLASSES = [cls.value for cls in HumidifierDeviceClass] +# As we import deprecated constants from the const module, we need to add these two functions +# otherwise this module will be logged for using deprecated constants and not the custom component +# Both can be removed if no deprecated constant are in this module anymore +__getattr__ = partial(check_if_deprecated_constant, module_globals=globals()) +__dir__ = partial(dir_with_deprecated_constants, module_globals=globals()) + # mypy: disallow-any-generics diff --git a/homeassistant/components/humidifier/const.py b/homeassistant/components/humidifier/const.py index 09c0714cbebc2a72cf367d8fee271546b6b0322d..a1a219ddce764985644269e5badb409150ab6978 100644 --- a/homeassistant/components/humidifier/const.py +++ b/homeassistant/components/humidifier/const.py @@ -1,5 +1,13 @@ """Provides the constants needed for component.""" from enum import IntFlag, StrEnum +from functools import partial + +from homeassistant.helpers.deprecation import ( + DeprecatedConstant, + DeprecatedConstantEnum, + check_if_deprecated_constant, + dir_with_deprecated_constants, +) MODE_NORMAL = "normal" MODE_ECO = "eco" @@ -35,8 +43,12 @@ DOMAIN = "humidifier" # DEVICE_CLASS_* below are deprecated as of 2021.12 # use the HumidifierDeviceClass enum instead. -DEVICE_CLASS_HUMIDIFIER = "humidifier" -DEVICE_CLASS_DEHUMIDIFIER = "dehumidifier" +_DEPRECATED_DEVICE_CLASS_HUMIDIFIER = DeprecatedConstant( + "humidifier", "HumidifierDeviceClass.HUMIDIFIER", "2025.1" +) +_DEPRECATED_DEVICE_CLASS_DEHUMIDIFIER = DeprecatedConstant( + "dehumidifier", "HumidifierDeviceClass.DEHUMIDIFIER", "2025.1" +) SERVICE_SET_MODE = "set_mode" SERVICE_SET_HUMIDITY = "set_humidity" @@ -50,4 +62,10 @@ class HumidifierEntityFeature(IntFlag): # The SUPPORT_MODES constant is deprecated as of Home Assistant 2022.5. # Please use the HumidifierEntityFeature enum instead. -SUPPORT_MODES = 1 +_DEPRECATED_SUPPORT_MODES = DeprecatedConstantEnum( + HumidifierEntityFeature.MODES, "2025.1" +) + +# Both can be removed if no deprecated constant are in this module anymore +__getattr__ = partial(check_if_deprecated_constant, module_globals=globals()) +__dir__ = partial(dir_with_deprecated_constants, module_globals=globals()) diff --git a/tests/components/humidifier/test_init.py b/tests/components/humidifier/test_init.py index a80f3956f203a91db68a981b8b041df68cb7f1a4..da45e1f1661fd0bdad4f2dd0a29bf29f81dfc199 100644 --- a/tests/components/humidifier/test_init.py +++ b/tests/components/humidifier/test_init.py @@ -1,9 +1,16 @@ """The tests for the humidifier component.""" +from enum import Enum +from types import ModuleType from unittest.mock import MagicMock +import pytest + +from homeassistant.components import humidifier from homeassistant.components.humidifier import HumidifierEntity from homeassistant.core import HomeAssistant +from tests.common import import_and_test_deprecated_constant_enum + class MockHumidifierEntity(HumidifierEntity): """Mock Humidifier device to use in tests.""" @@ -34,3 +41,28 @@ async def test_sync_turn_off(hass: HomeAssistant) -> None: await humidifier.async_turn_off() assert humidifier.turn_off.called + + +def _create_tuples(enum: Enum, constant_prefix: str) -> list[tuple[Enum, str]]: + result = [] + for enum in enum: + result.append((enum, constant_prefix)) + return result + + +@pytest.mark.parametrize( + ("enum", "constant_prefix"), + _create_tuples(humidifier.HumidifierEntityFeature, "SUPPORT_") + + _create_tuples(humidifier.HumidifierDeviceClass, "DEVICE_CLASS_"), +) +@pytest.mark.parametrize(("module"), [humidifier, humidifier.const]) +def test_deprecated_constants( + caplog: pytest.LogCaptureFixture, + enum: Enum, + constant_prefix: str, + module: ModuleType, +) -> None: + """Test deprecated constants.""" + import_and_test_deprecated_constant_enum( + caplog, module, enum, constant_prefix, "2025.1" + )