Deprecate deprecated alarm control panel constants (#106058)

pull/105552/head
Robert Resch 2023-12-20 18:41:17 +01:00 committed by GitHub
parent 491a50a2f1
commit ea28b74fe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 123 additions and 61 deletions

View File

@ -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)

View File

@ -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"

View File

@ -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

View File

@ -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}
)

View File

@ -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,

View File

@ -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"
)

View File

@ -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"
)

View File

@ -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):