diff --git a/homeassistant/components/alarm_control_panel/device_condition.py b/homeassistant/components/alarm_control_panel/device_condition.py index db010051010..3cbaa019ad0 100644 --- a/homeassistant/components/alarm_control_panel/device_condition.py +++ b/homeassistant/components/alarm_control_panel/device_condition.py @@ -13,7 +13,6 @@ from homeassistant.components.alarm_control_panel.const import ( ) from homeassistant.const import ( ATTR_ENTITY_ID, - ATTR_SUPPORTED_FEATURES, CONF_CONDITION, CONF_DEVICE_ID, CONF_DOMAIN, @@ -29,6 +28,7 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.helpers import condition, config_validation as cv, entity_registry from homeassistant.helpers.config_validation import DEVICE_CONDITION_BASE_SCHEMA +from homeassistant.helpers.entity import get_supported_features from homeassistant.helpers.typing import ConfigType, TemplateVarsType from . import DOMAIN @@ -70,13 +70,7 @@ async def async_get_conditions( if entry.domain != DOMAIN: continue - state = hass.states.get(entry.entity_id) - - # We need a state or else we can't populate the different armed conditions - if state is None: - continue - - supported_features = state.attributes[ATTR_SUPPORTED_FEATURES] + supported_features = get_supported_features(hass, entry.entity_id) # Add conditions for each entity that belongs to this integration base_condition = { diff --git a/tests/components/alarm_control_panel/test_device_condition.py b/tests/components/alarm_control_panel/test_device_condition.py index 450393135af..b1e2c171cea 100644 --- a/tests/components/alarm_control_panel/test_device_condition.py +++ b/tests/components/alarm_control_panel/test_device_condition.py @@ -1,7 +1,7 @@ """The tests for Alarm control panel device conditions.""" import pytest -from homeassistant.components.alarm_control_panel import DOMAIN +from homeassistant.components.alarm_control_panel import DOMAIN, const import homeassistant.components.automation as automation from homeassistant.const import ( STATE_ALARM_ARMED_AWAY, @@ -43,7 +43,30 @@ def calls(hass): return async_mock_service(hass, "test", "automation") -async def test_get_no_conditions(hass, device_reg, entity_reg): +@pytest.mark.parametrize( + "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_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_CUSTOM_BYPASS, ["is_armed_custom_bypass"]), + ], +) +async def test_get_conditions( + hass, + device_reg, + entity_reg, + set_state, + features_reg, + features_state, + expected_condition_types, +): """Test we get the expected conditions from a alarm_control_panel.""" config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) @@ -51,101 +74,41 @@ async def test_get_no_conditions(hass, device_reg, entity_reg): config_entry_id=config_entry.entry_id, connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) - entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id) - conditions = await async_get_device_automations(hass, "condition", device_entry.id) - assert_lists_same(conditions, []) - - -async def test_get_minimum_conditions(hass, device_reg, entity_reg): - """Test we get the expected conditions from a alarm_control_panel.""" - config_entry = MockConfigEntry(domain="test", data={}) - config_entry.add_to_hass(hass) - device_entry = device_reg.async_get_or_create( - config_entry_id=config_entry.entry_id, - connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + entity_reg.async_get_or_create( + DOMAIN, + "test", + "5678", + device_id=device_entry.id, + supported_features=features_reg, ) - entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id) - hass.states.async_set( - "alarm_control_panel.test_5678", "attributes", {"supported_features": 0} - ) - expected_conditions = [ + if set_state: + hass.states.async_set( + "alarm_control_panel.test_5678", + "attributes", + {"supported_features": features_state}, + ) + expected_conditions = [] + basic_condition_types = ["is_disarmed", "is_triggered"] + expected_conditions += [ { "condition": "device", "domain": DOMAIN, - "type": "is_disarmed", + "type": condition, "device_id": device_entry.id, "entity_id": f"{DOMAIN}.test_5678", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_triggered", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_5678", - }, + } + for condition in basic_condition_types ] - - conditions = await async_get_device_automations(hass, "condition", device_entry.id) - assert_lists_same(conditions, expected_conditions) - - -async def test_get_maximum_conditions(hass, device_reg, entity_reg): - """Test we get the expected conditions from a alarm_control_panel.""" - config_entry = MockConfigEntry(domain="test", data={}) - config_entry.add_to_hass(hass) - device_entry = device_reg.async_get_or_create( - config_entry_id=config_entry.entry_id, - connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, - ) - entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id) - hass.states.async_set( - "alarm_control_panel.test_5678", "attributes", {"supported_features": 31} - ) - expected_conditions = [ + expected_conditions += [ { "condition": "device", "domain": DOMAIN, - "type": "is_disarmed", + "type": condition, "device_id": device_entry.id, "entity_id": f"{DOMAIN}.test_5678", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_triggered", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_5678", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_armed_home", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_5678", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_armed_away", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_5678", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_armed_night", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_5678", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_armed_custom_bypass", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_5678", - }, + } + for condition in expected_condition_types ] - conditions = await async_get_device_automations(hass, "condition", device_entry.id) assert_lists_same(conditions, expected_conditions)