From 4d5bea7bcc9282b58fb5d1892a6380ca3bf31a32 Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Fri, 22 Dec 2023 11:23:21 +0100 Subject: [PATCH] Deprecate deprecated switch constants (#106225) --- homeassistant/components/switch/__init__.py | 18 ++++++++++++++++-- tests/components/switch/test_init.py | 13 ++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/switch/__init__.py b/homeassistant/components/switch/__init__.py index bdbb2b7701b..1d0654cd815 100644 --- a/homeassistant/components/switch/__init__.py +++ b/homeassistant/components/switch/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations from datetime import timedelta from enum import StrEnum +from functools import partial import logging import voluptuous as vol @@ -19,6 +20,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 @@ -47,8 +53,16 @@ DEVICE_CLASSES_SCHEMA = vol.All(vol.Lower, vol.Coerce(SwitchDeviceClass)) # DEVICE_CLASS* below are deprecated as of 2021.12 # use the SwitchDeviceClass enum instead. DEVICE_CLASSES = [cls.value for cls in SwitchDeviceClass] -DEVICE_CLASS_OUTLET = SwitchDeviceClass.OUTLET.value -DEVICE_CLASS_SWITCH = SwitchDeviceClass.SWITCH.value +_DEPRECATED_DEVICE_CLASS_OUTLET = DeprecatedConstantEnum( + SwitchDeviceClass.OUTLET, "2025.1" +) +_DEPRECATED_DEVICE_CLASS_SWITCH = DeprecatedConstantEnum( + SwitchDeviceClass.SWITCH, "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()) # mypy: disallow-any-generics diff --git a/tests/components/switch/test_init.py b/tests/components/switch/test_init.py index cbc91d24e41..7a43e0bf50e 100644 --- a/tests/components/switch/test_init.py +++ b/tests/components/switch/test_init.py @@ -9,7 +9,7 @@ from homeassistant.setup import async_setup_component from . import common -from tests.common import MockUser +from tests.common import MockUser, import_and_test_deprecated_constant_enum @pytest.fixture(autouse=True) @@ -80,3 +80,14 @@ async def test_switch_context( assert state2 is not None assert state.state != state2.state assert state2.context.user_id == hass_admin_user.id + + +@pytest.mark.parametrize(("enum"), list(switch.SwitchDeviceClass)) +def test_deprecated_constants( + caplog: pytest.LogCaptureFixture, + enum: switch.SwitchDeviceClass, +) -> None: + """Test deprecated constants.""" + import_and_test_deprecated_constant_enum( + caplog, switch, enum, "DEVICE_CLASS_", "2025.1" + )