Deprecate deprecated switch constants (#106225)

This commit is contained in:
Robert Resch 2023-12-22 11:23:21 +01:00 committed by GitHub
parent 06220849fc
commit 4d5bea7bcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from datetime import timedelta from datetime import timedelta
from enum import StrEnum from enum import StrEnum
from functools import partial
import logging import logging
import voluptuous as vol import voluptuous as vol
@ -19,6 +20,11 @@ from homeassistant.helpers.config_validation import ( # noqa: F401
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
PLATFORM_SCHEMA_BASE, 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 import ToggleEntity, ToggleEntityDescription
from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.typing import ConfigType 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 # DEVICE_CLASS* below are deprecated as of 2021.12
# use the SwitchDeviceClass enum instead. # use the SwitchDeviceClass enum instead.
DEVICE_CLASSES = [cls.value for cls in SwitchDeviceClass] DEVICE_CLASSES = [cls.value for cls in SwitchDeviceClass]
DEVICE_CLASS_OUTLET = SwitchDeviceClass.OUTLET.value _DEPRECATED_DEVICE_CLASS_OUTLET = DeprecatedConstantEnum(
DEVICE_CLASS_SWITCH = SwitchDeviceClass.SWITCH.value 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 # mypy: disallow-any-generics

View File

@ -9,7 +9,7 @@ from homeassistant.setup import async_setup_component
from . import common from . import common
from tests.common import MockUser from tests.common import MockUser, import_and_test_deprecated_constant_enum
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
@ -80,3 +80,14 @@ async def test_switch_context(
assert state2 is not None assert state2 is not None
assert state.state != state2.state assert state.state != state2.state
assert state2.context.user_id == hass_admin_user.id 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"
)