From 98f0ed1892598bf43d4f7ceab84c8f710541f301 Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Wed, 20 Dec 2023 19:11:03 +0100 Subject: [PATCH] Deprecate deprecated siren constants (#106121) --- homeassistant/components/siren/__init__.py | 21 +++++++++++---- homeassistant/components/siren/const.py | 31 ++++++++++++++++++---- tests/components/siren/test_init.py | 15 +++++++++++ 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/siren/__init__.py b/homeassistant/components/siren/__init__.py index d7e8843f54b..37bab7a995d 100644 --- a/homeassistant/components/siren/__init__.py +++ b/homeassistant/components/siren/__init__.py @@ -2,6 +2,7 @@ from __future__ import annotations from datetime import timedelta +from functools import partial import logging from typing import Any, TypedDict, cast, final @@ -15,21 +16,25 @@ 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 .const import ( # noqa: F401 + _DEPRECATED_SUPPORT_DURATION, + _DEPRECATED_SUPPORT_TONES, + _DEPRECATED_SUPPORT_TURN_OFF, + _DEPRECATED_SUPPORT_TURN_ON, + _DEPRECATED_SUPPORT_VOLUME_SET, ATTR_AVAILABLE_TONES, ATTR_DURATION, ATTR_TONE, ATTR_VOLUME_LEVEL, DOMAIN, - SUPPORT_DURATION, - SUPPORT_TONES, - SUPPORT_TURN_OFF, - SUPPORT_TURN_ON, - SUPPORT_VOLUME_SET, SirenEntityFeature, ) @@ -43,6 +48,12 @@ TURN_ON_SCHEMA = { vol.Optional(ATTR_VOLUME_LEVEL): cv.small_float, } +# 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()) + class SirenTurnOnServiceParameters(TypedDict, total=False): """Represent possible parameters to siren.turn_on service data dict type.""" diff --git a/homeassistant/components/siren/const.py b/homeassistant/components/siren/const.py index 374b1d59e2a..50c3af61c8d 100644 --- a/homeassistant/components/siren/const.py +++ b/homeassistant/components/siren/const.py @@ -1,8 +1,15 @@ """Constants for the siren component.""" from enum import IntFlag +from functools import partial from typing import Final +from homeassistant.helpers.deprecation import ( + DeprecatedConstantEnum, + check_if_deprecated_constant, + dir_with_deprecated_constants, +) + DOMAIN: Final = "siren" ATTR_TONE: Final = "tone" @@ -24,8 +31,22 @@ class SirenEntityFeature(IntFlag): # These constants are deprecated as of Home Assistant 2022.5 # Please use the SirenEntityFeature enum instead. -SUPPORT_TURN_ON: Final = 1 -SUPPORT_TURN_OFF: Final = 2 -SUPPORT_TONES: Final = 4 -SUPPORT_VOLUME_SET: Final = 8 -SUPPORT_DURATION: Final = 16 +_DEPRECATED_SUPPORT_TURN_ON: Final = DeprecatedConstantEnum( + SirenEntityFeature.TURN_ON, "2025.1" +) +_DEPRECATED_SUPPORT_TURN_OFF: Final = DeprecatedConstantEnum( + SirenEntityFeature.TURN_OFF, "2025.1" +) +_DEPRECATED_SUPPORT_TONES: Final = DeprecatedConstantEnum( + SirenEntityFeature.TONES, "2025.1" +) +_DEPRECATED_SUPPORT_VOLUME_SET: Final = DeprecatedConstantEnum( + SirenEntityFeature.VOLUME_SET, "2025.1" +) +_DEPRECATED_SUPPORT_DURATION: Final = DeprecatedConstantEnum( + SirenEntityFeature.DURATION, "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/siren/test_init.py b/tests/components/siren/test_init.py index 267b1c1e30d..ee007f6f1f5 100644 --- a/tests/components/siren/test_init.py +++ b/tests/components/siren/test_init.py @@ -1,8 +1,10 @@ """The tests for the siren component.""" +from types import ModuleType from unittest.mock import MagicMock import pytest +from homeassistant.components import siren from homeassistant.components.siren import ( SirenEntity, SirenEntityDescription, @@ -11,6 +13,8 @@ from homeassistant.components.siren import ( from homeassistant.components.siren.const import SirenEntityFeature from homeassistant.core import HomeAssistant +from tests.common import import_and_test_deprecated_constant_enum + class MockSirenEntity(SirenEntity): """Mock siren device to use in tests.""" @@ -104,3 +108,14 @@ async def test_missing_tones_dict(hass: HomeAssistant) -> None: siren.hass = hass with pytest.raises(ValueError): process_turn_on_params(siren, {"tone": 3}) + + +@pytest.mark.parametrize(("enum"), list(SirenEntityFeature)) +@pytest.mark.parametrize(("module"), [siren, siren.const]) +def test_deprecated_constants( + caplog: pytest.LogCaptureFixture, + enum: SirenEntityFeature, + module: ModuleType, +) -> None: + """Test deprecated constants.""" + import_and_test_deprecated_constant_enum(caplog, module, enum, "SUPPORT_", "2025.1")