From ea28b74fe91a7fb2ec6636d9d0843b2c5e4f4363 Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Wed, 20 Dec 2023 18:41:17 +0100 Subject: [PATCH] Deprecate deprecated alarm control panel constants (#106058) --- .../alarm_control_panel/__init__.py | 27 +++++++---- .../components/alarm_control_panel/const.py | 41 ++++++++++++---- .../alarm_control_panel/device_action.py | 18 +++---- .../alarm_control_panel/device_condition.py | 16 +++---- .../alarm_control_panel/device_trigger.py | 15 ++---- .../alarm_control_panel/test_init.py | 47 +++++++++++++++++++ tests/components/automation/test_init.py | 2 +- .../test/alarm_control_panel.py | 18 +++---- 8 files changed, 123 insertions(+), 61 deletions(-) create mode 100644 tests/components/alarm_control_panel/test_init.py diff --git a/homeassistant/components/alarm_control_panel/__init__.py b/homeassistant/components/alarm_control_panel/__init__.py index c307e96e9f0..356a8a3164e 100644 --- a/homeassistant/components/alarm_control_panel/__init__.py +++ b/homeassistant/components/alarm_control_panel/__init__.py @@ -2,6 +2,7 @@ from __future__ import annotations from datetime import timedelta +from functools import partial import logging from typing import Any, Final, final @@ -22,26 +23,36 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant import homeassistant.helpers.config_validation as cv from homeassistant.helpers.config_validation import make_entity_service_schema +from homeassistant.helpers.deprecation import ( + check_if_deprecated_constant, + dir_with_deprecated_constants, +) from homeassistant.helpers.entity import Entity, EntityDescription from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.typing import ConfigType from .const import ( # noqa: F401 + _DEPRECATED_FORMAT_NUMBER, + _DEPRECATED_FORMAT_TEXT, + _DEPRECATED_SUPPORT_ALARM_ARM_AWAY, + _DEPRECATED_SUPPORT_ALARM_ARM_CUSTOM_BYPASS, + _DEPRECATED_SUPPORT_ALARM_ARM_HOME, + _DEPRECATED_SUPPORT_ALARM_ARM_NIGHT, + _DEPRECATED_SUPPORT_ALARM_ARM_VACATION, + _DEPRECATED_SUPPORT_ALARM_TRIGGER, ATTR_CHANGED_BY, ATTR_CODE_ARM_REQUIRED, DOMAIN, - FORMAT_NUMBER, - FORMAT_TEXT, - SUPPORT_ALARM_ARM_AWAY, - SUPPORT_ALARM_ARM_CUSTOM_BYPASS, - SUPPORT_ALARM_ARM_HOME, - SUPPORT_ALARM_ARM_NIGHT, - SUPPORT_ALARM_ARM_VACATION, - SUPPORT_ALARM_TRIGGER, AlarmControlPanelEntityFeature, CodeFormat, ) +# As we import constants of the cost module here, we need to add the following +# functions to check for deprecated constants again +# 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()) + _LOGGER: Final = logging.getLogger(__name__) SCAN_INTERVAL: Final = timedelta(seconds=30) diff --git a/homeassistant/components/alarm_control_panel/const.py b/homeassistant/components/alarm_control_panel/const.py index f14a1ce66e0..90bbcba1314 100644 --- a/homeassistant/components/alarm_control_panel/const.py +++ b/homeassistant/components/alarm_control_panel/const.py @@ -1,7 +1,14 @@ """Provides the constants needed for component.""" from enum import IntFlag, StrEnum +from functools import partial from typing import Final +from homeassistant.helpers.deprecation import ( + DeprecatedConstantEnum, + check_if_deprecated_constant, + dir_with_deprecated_constants, +) + DOMAIN: Final = "alarm_control_panel" ATTR_CHANGED_BY: Final = "changed_by" @@ -15,10 +22,10 @@ class CodeFormat(StrEnum): NUMBER = "number" -# These constants are deprecated as of Home Assistant 2022.5 +# These constants are deprecated as of Home Assistant 2022.5, can be removed in 2025.1 # Please use the CodeFormat enum instead. -FORMAT_TEXT: Final = "text" -FORMAT_NUMBER: Final = "number" +_DEPRECATED_FORMAT_TEXT: Final = DeprecatedConstantEnum(CodeFormat.TEXT, "2025.1") +_DEPRECATED_FORMAT_NUMBER: Final = DeprecatedConstantEnum(CodeFormat.NUMBER, "2025.1") class AlarmControlPanelEntityFeature(IntFlag): @@ -34,12 +41,28 @@ class AlarmControlPanelEntityFeature(IntFlag): # These constants are deprecated as of Home Assistant 2022.5 # Please use the AlarmControlPanelEntityFeature enum instead. -SUPPORT_ALARM_ARM_HOME: Final = 1 -SUPPORT_ALARM_ARM_AWAY: Final = 2 -SUPPORT_ALARM_ARM_NIGHT: Final = 4 -SUPPORT_ALARM_TRIGGER: Final = 8 -SUPPORT_ALARM_ARM_CUSTOM_BYPASS: Final = 16 -SUPPORT_ALARM_ARM_VACATION: Final = 32 +_DEPRECATED_SUPPORT_ALARM_ARM_HOME: Final = DeprecatedConstantEnum( + AlarmControlPanelEntityFeature.ARM_HOME, "2025.1" +) +_DEPRECATED_SUPPORT_ALARM_ARM_AWAY: Final = DeprecatedConstantEnum( + AlarmControlPanelEntityFeature.ARM_AWAY, "2025.1" +) +_DEPRECATED_SUPPORT_ALARM_ARM_NIGHT: Final = DeprecatedConstantEnum( + AlarmControlPanelEntityFeature.ARM_NIGHT, "2025.1" +) +_DEPRECATED_SUPPORT_ALARM_TRIGGER: Final = DeprecatedConstantEnum( + AlarmControlPanelEntityFeature.TRIGGER, "2025.1" +) +_DEPRECATED_SUPPORT_ALARM_ARM_CUSTOM_BYPASS: Final = DeprecatedConstantEnum( + AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS, "2025.1" +) +_DEPRECATED_SUPPORT_ALARM_ARM_VACATION: Final = DeprecatedConstantEnum( + AlarmControlPanelEntityFeature.ARM_VACATION, "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()) CONDITION_TRIGGERED: Final = "is_triggered" CONDITION_DISARMED: Final = "is_disarmed" diff --git a/homeassistant/components/alarm_control_panel/device_action.py b/homeassistant/components/alarm_control_panel/device_action.py index e453be88934..9c068bb3327 100644 --- a/homeassistant/components/alarm_control_panel/device_action.py +++ b/homeassistant/components/alarm_control_panel/device_action.py @@ -28,13 +28,7 @@ from homeassistant.helpers.entity import get_supported_features from homeassistant.helpers.typing import ConfigType, TemplateVarsType from . import ATTR_CODE_ARM_REQUIRED, DOMAIN -from .const import ( - SUPPORT_ALARM_ARM_AWAY, - SUPPORT_ALARM_ARM_HOME, - SUPPORT_ALARM_ARM_NIGHT, - SUPPORT_ALARM_ARM_VACATION, - SUPPORT_ALARM_TRIGGER, -) +from .const import AlarmControlPanelEntityFeature ACTION_TYPES: Final[set[str]] = { "arm_away", @@ -82,16 +76,16 @@ async def async_get_actions( } # Add actions for each entity that belongs to this integration - if supported_features & SUPPORT_ALARM_ARM_AWAY: + if supported_features & AlarmControlPanelEntityFeature.ARM_AWAY: actions.append({**base_action, CONF_TYPE: "arm_away"}) - if supported_features & SUPPORT_ALARM_ARM_HOME: + if supported_features & AlarmControlPanelEntityFeature.ARM_HOME: actions.append({**base_action, CONF_TYPE: "arm_home"}) - if supported_features & SUPPORT_ALARM_ARM_NIGHT: + if supported_features & AlarmControlPanelEntityFeature.ARM_NIGHT: actions.append({**base_action, CONF_TYPE: "arm_night"}) - if supported_features & SUPPORT_ALARM_ARM_VACATION: + if supported_features & AlarmControlPanelEntityFeature.ARM_VACATION: actions.append({**base_action, CONF_TYPE: "arm_vacation"}) actions.append({**base_action, CONF_TYPE: "disarm"}) - if supported_features & SUPPORT_ALARM_TRIGGER: + if supported_features & AlarmControlPanelEntityFeature.TRIGGER: actions.append({**base_action, CONF_TYPE: "trigger"}) return actions diff --git a/homeassistant/components/alarm_control_panel/device_condition.py b/homeassistant/components/alarm_control_panel/device_condition.py index ee8cb57f568..e3c627d17a3 100644 --- a/homeassistant/components/alarm_control_panel/device_condition.py +++ b/homeassistant/components/alarm_control_panel/device_condition.py @@ -39,11 +39,7 @@ from .const import ( CONDITION_ARMED_VACATION, CONDITION_DISARMED, CONDITION_TRIGGERED, - SUPPORT_ALARM_ARM_AWAY, - SUPPORT_ALARM_ARM_CUSTOM_BYPASS, - SUPPORT_ALARM_ARM_HOME, - SUPPORT_ALARM_ARM_NIGHT, - SUPPORT_ALARM_ARM_VACATION, + AlarmControlPanelEntityFeature, ) CONDITION_TYPES: Final[set[str]] = { @@ -90,15 +86,15 @@ async def async_get_conditions( {**base_condition, CONF_TYPE: CONDITION_DISARMED}, {**base_condition, CONF_TYPE: CONDITION_TRIGGERED}, ] - if supported_features & SUPPORT_ALARM_ARM_HOME: + if supported_features & AlarmControlPanelEntityFeature.ARM_HOME: conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_HOME}) - if supported_features & SUPPORT_ALARM_ARM_AWAY: + if supported_features & AlarmControlPanelEntityFeature.ARM_AWAY: conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_AWAY}) - if supported_features & SUPPORT_ALARM_ARM_NIGHT: + if supported_features & AlarmControlPanelEntityFeature.ARM_NIGHT: conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_NIGHT}) - if supported_features & SUPPORT_ALARM_ARM_VACATION: + if supported_features & AlarmControlPanelEntityFeature.ARM_VACATION: conditions.append({**base_condition, CONF_TYPE: CONDITION_ARMED_VACATION}) - if supported_features & SUPPORT_ALARM_ARM_CUSTOM_BYPASS: + if supported_features & AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS: conditions.append( {**base_condition, CONF_TYPE: CONDITION_ARMED_CUSTOM_BYPASS} ) diff --git a/homeassistant/components/alarm_control_panel/device_trigger.py b/homeassistant/components/alarm_control_panel/device_trigger.py index fc3850dce30..e5141a1dfd5 100644 --- a/homeassistant/components/alarm_control_panel/device_trigger.py +++ b/homeassistant/components/alarm_control_panel/device_trigger.py @@ -29,12 +29,7 @@ from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo from homeassistant.helpers.typing import ConfigType from . import DOMAIN -from .const import ( - SUPPORT_ALARM_ARM_AWAY, - SUPPORT_ALARM_ARM_HOME, - SUPPORT_ALARM_ARM_NIGHT, - SUPPORT_ALARM_ARM_VACATION, -) +from .const import AlarmControlPanelEntityFeature BASIC_TRIGGER_TYPES: Final[set[str]] = {"triggered", "disarmed", "arming"} TRIGGER_TYPES: Final[set[str]] = BASIC_TRIGGER_TYPES | { @@ -82,28 +77,28 @@ async def async_get_triggers( } for trigger in BASIC_TRIGGER_TYPES ] - if supported_features & SUPPORT_ALARM_ARM_HOME: + if supported_features & AlarmControlPanelEntityFeature.ARM_HOME: triggers.append( { **base_trigger, CONF_TYPE: "armed_home", } ) - if supported_features & SUPPORT_ALARM_ARM_AWAY: + if supported_features & AlarmControlPanelEntityFeature.ARM_AWAY: triggers.append( { **base_trigger, CONF_TYPE: "armed_away", } ) - if supported_features & SUPPORT_ALARM_ARM_NIGHT: + if supported_features & AlarmControlPanelEntityFeature.ARM_NIGHT: triggers.append( { **base_trigger, CONF_TYPE: "armed_night", } ) - if supported_features & SUPPORT_ALARM_ARM_VACATION: + if supported_features & AlarmControlPanelEntityFeature.ARM_VACATION: triggers.append( { **base_trigger, diff --git a/tests/components/alarm_control_panel/test_init.py b/tests/components/alarm_control_panel/test_init.py new file mode 100644 index 00000000000..c447119c119 --- /dev/null +++ b/tests/components/alarm_control_panel/test_init.py @@ -0,0 +1,47 @@ +"""Test for the alarm control panel const module.""" + +from types import ModuleType + +import pytest + +from homeassistant.components import alarm_control_panel + +from tests.common import import_and_test_deprecated_constant_enum + + +@pytest.mark.parametrize( + "code_format", + list(alarm_control_panel.CodeFormat), +) +@pytest.mark.parametrize( + "module", + [alarm_control_panel, alarm_control_panel.const], +) +def test_deprecated_constant_code_format( + caplog: pytest.LogCaptureFixture, + code_format: alarm_control_panel.CodeFormat, + module: ModuleType, +) -> None: + """Test deprecated format constants.""" + import_and_test_deprecated_constant_enum( + caplog, module, code_format, "FORMAT_", "2025.1" + ) + + +@pytest.mark.parametrize( + "entity_feature", + list(alarm_control_panel.AlarmControlPanelEntityFeature), +) +@pytest.mark.parametrize( + "module", + [alarm_control_panel, alarm_control_panel.const], +) +def test_deprecated_support_alarm_constants( + caplog: pytest.LogCaptureFixture, + entity_feature: alarm_control_panel.AlarmControlPanelEntityFeature, + module: ModuleType, +) -> None: + """Test deprecated support alarm constants.""" + import_and_test_deprecated_constant_enum( + caplog, module, entity_feature, "SUPPORT_ALARM_", "2025.1" + ) diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index a2f2dfbf907..235ca48f095 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -2582,7 +2582,7 @@ def test_deprecated_constants( constant_name: str, replacement: Any, ) -> None: - """Test deprecated binary sensor device classes.""" + """Test deprecated automation constants.""" import_and_test_deprecated_constant( caplog, automation, constant_name, replacement.__name__, replacement, "2025.1" ) diff --git a/tests/testing_config/custom_components/test/alarm_control_panel.py b/tests/testing_config/custom_components/test/alarm_control_panel.py index b39c2c71eda..7490a7703a4 100644 --- a/tests/testing_config/custom_components/test/alarm_control_panel.py +++ b/tests/testing_config/custom_components/test/alarm_control_panel.py @@ -4,11 +4,7 @@ Call init before using it in your tests to ensure clean test data. """ from homeassistant.components.alarm_control_panel import AlarmControlPanelEntity from homeassistant.components.alarm_control_panel.const import ( - SUPPORT_ALARM_ARM_AWAY, - SUPPORT_ALARM_ARM_HOME, - SUPPORT_ALARM_ARM_NIGHT, - SUPPORT_ALARM_ARM_VACATION, - SUPPORT_ALARM_TRIGGER, + AlarmControlPanelEntityFeature, ) from homeassistant.const import ( STATE_ALARM_ARMED_AWAY, @@ -73,14 +69,14 @@ class MockAlarm(MockEntity, AlarmControlPanelEntity): return self._state @property - def supported_features(self) -> int: + def supported_features(self) -> AlarmControlPanelEntityFeature: """Return the list of supported features.""" return ( - SUPPORT_ALARM_ARM_HOME - | SUPPORT_ALARM_ARM_AWAY - | SUPPORT_ALARM_ARM_NIGHT - | SUPPORT_ALARM_TRIGGER - | SUPPORT_ALARM_ARM_VACATION + AlarmControlPanelEntityFeature.ARM_HOME + | AlarmControlPanelEntityFeature.ARM_AWAY + | AlarmControlPanelEntityFeature.ARM_NIGHT + | AlarmControlPanelEntityFeature.TRIGGER + | AlarmControlPanelEntityFeature.ARM_VACATION ) def alarm_arm_away(self, code=None):