Deprecate deprecated siren constants (#106121)

This commit is contained in:
Robert Resch 2023-12-20 19:11:03 +01:00 committed by GitHub
parent 803e77bebd
commit 98f0ed1892
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 10 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
from datetime import timedelta from datetime import timedelta
from functools import partial
import logging import logging
from typing import Any, TypedDict, cast, final from typing import Any, TypedDict, cast, final
@ -15,21 +16,25 @@ from homeassistant.helpers.config_validation import ( # noqa: F401
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
PLATFORM_SCHEMA_BASE, 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 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
from .const import ( # noqa: F401 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_AVAILABLE_TONES,
ATTR_DURATION, ATTR_DURATION,
ATTR_TONE, ATTR_TONE,
ATTR_VOLUME_LEVEL, ATTR_VOLUME_LEVEL,
DOMAIN, DOMAIN,
SUPPORT_DURATION,
SUPPORT_TONES,
SUPPORT_TURN_OFF,
SUPPORT_TURN_ON,
SUPPORT_VOLUME_SET,
SirenEntityFeature, SirenEntityFeature,
) )
@ -43,6 +48,12 @@ TURN_ON_SCHEMA = {
vol.Optional(ATTR_VOLUME_LEVEL): cv.small_float, 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): class SirenTurnOnServiceParameters(TypedDict, total=False):
"""Represent possible parameters to siren.turn_on service data dict type.""" """Represent possible parameters to siren.turn_on service data dict type."""

View File

@ -1,8 +1,15 @@
"""Constants for the siren component.""" """Constants for the siren component."""
from enum import IntFlag from enum import IntFlag
from functools import partial
from typing import Final from typing import Final
from homeassistant.helpers.deprecation import (
DeprecatedConstantEnum,
check_if_deprecated_constant,
dir_with_deprecated_constants,
)
DOMAIN: Final = "siren" DOMAIN: Final = "siren"
ATTR_TONE: Final = "tone" ATTR_TONE: Final = "tone"
@ -24,8 +31,22 @@ class SirenEntityFeature(IntFlag):
# These constants are deprecated as of Home Assistant 2022.5 # These constants are deprecated as of Home Assistant 2022.5
# Please use the SirenEntityFeature enum instead. # Please use the SirenEntityFeature enum instead.
SUPPORT_TURN_ON: Final = 1 _DEPRECATED_SUPPORT_TURN_ON: Final = DeprecatedConstantEnum(
SUPPORT_TURN_OFF: Final = 2 SirenEntityFeature.TURN_ON, "2025.1"
SUPPORT_TONES: Final = 4 )
SUPPORT_VOLUME_SET: Final = 8 _DEPRECATED_SUPPORT_TURN_OFF: Final = DeprecatedConstantEnum(
SUPPORT_DURATION: Final = 16 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())

View File

@ -1,8 +1,10 @@
"""The tests for the siren component.""" """The tests for the siren component."""
from types import ModuleType
from unittest.mock import MagicMock from unittest.mock import MagicMock
import pytest import pytest
from homeassistant.components import siren
from homeassistant.components.siren import ( from homeassistant.components.siren import (
SirenEntity, SirenEntity,
SirenEntityDescription, SirenEntityDescription,
@ -11,6 +13,8 @@ from homeassistant.components.siren import (
from homeassistant.components.siren.const import SirenEntityFeature from homeassistant.components.siren.const import SirenEntityFeature
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from tests.common import import_and_test_deprecated_constant_enum
class MockSirenEntity(SirenEntity): class MockSirenEntity(SirenEntity):
"""Mock siren device to use in tests.""" """Mock siren device to use in tests."""
@ -104,3 +108,14 @@ async def test_missing_tones_dict(hass: HomeAssistant) -> None:
siren.hass = hass siren.hass = hass
with pytest.raises(ValueError): with pytest.raises(ValueError):
process_turn_on_params(siren, {"tone": 3}) 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")