From be7fc35dfa5c9bcf7d4ec67927ec53f641750c3e Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 1 Apr 2022 13:54:03 +0200 Subject: [PATCH] Add EntityFeature enum to Alarm Control Panel (#69044) --- .../alarm_control_panel/__init__.py | 17 +++--- .../components/alarm_control_panel/const.py | 15 ++++++ .../components/manual/alarm_control_panel.py | 19 +++---- .../alarm_control_panel/test_device_action.py | 53 +++++++++++++++---- .../test_device_condition.py | 40 ++++++++++---- 5 files changed, 105 insertions(+), 39 deletions(-) diff --git a/homeassistant/components/alarm_control_panel/__init__.py b/homeassistant/components/alarm_control_panel/__init__.py index 082327fdec8..d27091ad535 100644 --- a/homeassistant/components/alarm_control_panel/__init__.py +++ b/homeassistant/components/alarm_control_panel/__init__.py @@ -27,13 +27,14 @@ from homeassistant.helpers.entity import Entity, EntityDescription from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.typing import ConfigType -from .const import ( +from .const import ( # noqa: F401 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, ) _LOGGER: Final = logging.getLogger(__name__) @@ -58,7 +59,7 @@ PLATFORM_SCHEMA_BASE: Final = cv.PLATFORM_SCHEMA_BASE async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Track states and offer events for sensors.""" component = hass.data[DOMAIN] = EntityComponent( - logging.getLogger(__name__), DOMAIN, hass, SCAN_INTERVAL + _LOGGER, DOMAIN, hass, SCAN_INTERVAL ) await component.async_setup(config) @@ -70,37 +71,37 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: SERVICE_ALARM_ARM_HOME, ALARM_SERVICE_SCHEMA, "async_alarm_arm_home", - [SUPPORT_ALARM_ARM_HOME], + [AlarmControlPanelEntityFeature.ARM_HOME], ) component.async_register_entity_service( SERVICE_ALARM_ARM_AWAY, ALARM_SERVICE_SCHEMA, "async_alarm_arm_away", - [SUPPORT_ALARM_ARM_AWAY], + [AlarmControlPanelEntityFeature.ARM_AWAY], ) component.async_register_entity_service( SERVICE_ALARM_ARM_NIGHT, ALARM_SERVICE_SCHEMA, "async_alarm_arm_night", - [SUPPORT_ALARM_ARM_NIGHT], + [AlarmControlPanelEntityFeature.ARM_NIGHT], ) component.async_register_entity_service( SERVICE_ALARM_ARM_VACATION, ALARM_SERVICE_SCHEMA, "async_alarm_arm_vacation", - [SUPPORT_ALARM_ARM_VACATION], + [AlarmControlPanelEntityFeature.ARM_VACATION], ) component.async_register_entity_service( SERVICE_ALARM_ARM_CUSTOM_BYPASS, ALARM_SERVICE_SCHEMA, "async_alarm_arm_custom_bypass", - [SUPPORT_ALARM_ARM_CUSTOM_BYPASS], + [AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS], ) component.async_register_entity_service( SERVICE_ALARM_TRIGGER, ALARM_SERVICE_SCHEMA, "async_alarm_trigger", - [SUPPORT_ALARM_TRIGGER], + [AlarmControlPanelEntityFeature.TRIGGER], ) return True diff --git a/homeassistant/components/alarm_control_panel/const.py b/homeassistant/components/alarm_control_panel/const.py index f3688a27958..c3ed914126e 100644 --- a/homeassistant/components/alarm_control_panel/const.py +++ b/homeassistant/components/alarm_control_panel/const.py @@ -1,7 +1,22 @@ """Provides the constants needed for component.""" +from enum import IntEnum from typing import Final + +class AlarmControlPanelEntityFeature(IntEnum): + """Supported features of the alarm control panel entity.""" + + ARM_HOME = 1 + ARM_AWAY = 2 + ARM_NIGHT = 4 + TRIGGER = 8 + ARM_CUSTOM_BYPASS = 16 + ARM_VACATION = 32 + + +# These constants are deprecated as of Home Assistant 2022.5 +# Pleease use the AlarmControlPanelEntityFeature enum instead. SUPPORT_ALARM_ARM_HOME: Final = 1 SUPPORT_ALARM_ARM_AWAY: Final = 2 SUPPORT_ALARM_ARM_NIGHT: Final = 4 diff --git a/homeassistant/components/manual/alarm_control_panel.py b/homeassistant/components/manual/alarm_control_panel.py index 8f86f3cdbdf..b9ac0bdcf5c 100644 --- a/homeassistant/components/manual/alarm_control_panel.py +++ b/homeassistant/components/manual/alarm_control_panel.py @@ -10,12 +10,7 @@ import voluptuous as vol import homeassistant.components.alarm_control_panel as alarm from homeassistant.components.alarm_control_panel.const import ( - 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, ) from homeassistant.const import ( CONF_ARMING_TIME, @@ -262,12 +257,12 @@ class ManualAlarm(alarm.AlarmControlPanelEntity, RestoreEntity): def supported_features(self) -> int: """Return the list of supported features.""" return ( - SUPPORT_ALARM_ARM_HOME - | SUPPORT_ALARM_ARM_AWAY - | SUPPORT_ALARM_ARM_NIGHT - | SUPPORT_ALARM_ARM_VACATION - | SUPPORT_ALARM_TRIGGER - | SUPPORT_ALARM_ARM_CUSTOM_BYPASS + AlarmControlPanelEntityFeature.ARM_HOME + | AlarmControlPanelEntityFeature.ARM_AWAY + | AlarmControlPanelEntityFeature.ARM_NIGHT + | AlarmControlPanelEntityFeature.ARM_VACATION + | AlarmControlPanelEntityFeature.TRIGGER + | AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS ) @property diff --git a/tests/components/alarm_control_panel/test_device_action.py b/tests/components/alarm_control_panel/test_device_action.py index 5cbe9f256ba..52e60967131 100644 --- a/tests/components/alarm_control_panel/test_device_action.py +++ b/tests/components/alarm_control_panel/test_device_action.py @@ -44,16 +44,51 @@ def entity_reg(hass): "set_state,features_reg,features_state,expected_action_types", [ (False, 0, 0, ["disarm"]), - (False, const.SUPPORT_ALARM_ARM_AWAY, 0, ["disarm", "arm_away"]), - (False, const.SUPPORT_ALARM_ARM_HOME, 0, ["disarm", "arm_home"]), - (False, const.SUPPORT_ALARM_ARM_NIGHT, 0, ["disarm", "arm_night"]), - (False, const.SUPPORT_ALARM_TRIGGER, 0, ["disarm", "trigger"]), + ( + False, + const.AlarmControlPanelEntityFeature.ARM_AWAY, + 0, + ["disarm", "arm_away"], + ), + ( + False, + const.AlarmControlPanelEntityFeature.ARM_HOME, + 0, + ["disarm", "arm_home"], + ), + ( + False, + const.AlarmControlPanelEntityFeature.ARM_NIGHT, + 0, + ["disarm", "arm_night"], + ), + (False, const.AlarmControlPanelEntityFeature.TRIGGER, 0, ["disarm", "trigger"]), (True, 0, 0, ["disarm"]), - (True, 0, const.SUPPORT_ALARM_ARM_AWAY, ["disarm", "arm_away"]), - (True, 0, const.SUPPORT_ALARM_ARM_HOME, ["disarm", "arm_home"]), - (True, 0, const.SUPPORT_ALARM_ARM_NIGHT, ["disarm", "arm_night"]), - (True, 0, const.SUPPORT_ALARM_ARM_VACATION, ["disarm", "arm_vacation"]), - (True, 0, const.SUPPORT_ALARM_TRIGGER, ["disarm", "trigger"]), + ( + True, + 0, + const.AlarmControlPanelEntityFeature.ARM_AWAY, + ["disarm", "arm_away"], + ), + ( + True, + 0, + const.AlarmControlPanelEntityFeature.ARM_HOME, + ["disarm", "arm_home"], + ), + ( + True, + 0, + const.AlarmControlPanelEntityFeature.ARM_NIGHT, + ["disarm", "arm_night"], + ), + ( + True, + 0, + const.AlarmControlPanelEntityFeature.ARM_VACATION, + ["disarm", "arm_vacation"], + ), + (True, 0, const.AlarmControlPanelEntityFeature.TRIGGER, ["disarm", "trigger"]), ], ) async def test_get_actions( diff --git a/tests/components/alarm_control_panel/test_device_condition.py b/tests/components/alarm_control_panel/test_device_condition.py index b44e6ba7e8b..83f9ff79dca 100644 --- a/tests/components/alarm_control_panel/test_device_condition.py +++ b/tests/components/alarm_control_panel/test_device_condition.py @@ -49,17 +49,37 @@ def calls(hass): "set_state,features_reg,features_state,expected_condition_types", [ (False, 0, 0, []), - (False, const.SUPPORT_ALARM_ARM_AWAY, 0, ["is_armed_away"]), - (False, const.SUPPORT_ALARM_ARM_HOME, 0, ["is_armed_home"]), - (False, const.SUPPORT_ALARM_ARM_NIGHT, 0, ["is_armed_night"]), - (False, const.SUPPORT_ALARM_ARM_VACATION, 0, ["is_armed_vacation"]), - (False, const.SUPPORT_ALARM_ARM_CUSTOM_BYPASS, 0, ["is_armed_custom_bypass"]), + (False, const.AlarmControlPanelEntityFeature.ARM_AWAY, 0, ["is_armed_away"]), + (False, const.AlarmControlPanelEntityFeature.ARM_HOME, 0, ["is_armed_home"]), + (False, const.AlarmControlPanelEntityFeature.ARM_NIGHT, 0, ["is_armed_night"]), + ( + False, + const.AlarmControlPanelEntityFeature.ARM_VACATION, + 0, + ["is_armed_vacation"], + ), + ( + False, + const.AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS, + 0, + ["is_armed_custom_bypass"], + ), (True, 0, 0, []), - (True, 0, const.SUPPORT_ALARM_ARM_AWAY, ["is_armed_away"]), - (True, 0, const.SUPPORT_ALARM_ARM_HOME, ["is_armed_home"]), - (True, 0, const.SUPPORT_ALARM_ARM_NIGHT, ["is_armed_night"]), - (True, 0, const.SUPPORT_ALARM_ARM_VACATION, ["is_armed_vacation"]), - (True, 0, const.SUPPORT_ALARM_ARM_CUSTOM_BYPASS, ["is_armed_custom_bypass"]), + (True, 0, const.AlarmControlPanelEntityFeature.ARM_AWAY, ["is_armed_away"]), + (True, 0, const.AlarmControlPanelEntityFeature.ARM_HOME, ["is_armed_home"]), + (True, 0, const.AlarmControlPanelEntityFeature.ARM_NIGHT, ["is_armed_night"]), + ( + True, + 0, + const.AlarmControlPanelEntityFeature.ARM_VACATION, + ["is_armed_vacation"], + ), + ( + True, + 0, + const.AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS, + ["is_armed_custom_bypass"], + ), ], ) async def test_get_conditions(