diff --git a/homeassistant/components/cover/device_condition.py b/homeassistant/components/cover/device_condition.py index 148e2fb48b0..bd433dbd93d 100644 --- a/homeassistant/components/cover/device_condition.py +++ b/homeassistant/components/cover/device_condition.py @@ -7,7 +7,6 @@ import voluptuous as vol from homeassistant.const import ( ATTR_ENTITY_ID, - ATTR_SUPPORTED_FEATURES, CONF_ABOVE, CONF_BELOW, CONF_CONDITION, @@ -28,6 +27,7 @@ from homeassistant.helpers import ( template, ) 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 ( @@ -77,11 +77,7 @@ async def async_get_conditions(hass: HomeAssistant, device_id: str) -> list[dict if entry.domain != DOMAIN: continue - state = hass.states.get(entry.entity_id) - if not state or ATTR_SUPPORTED_FEATURES not in state.attributes: - continue - - supported_features = state.attributes[ATTR_SUPPORTED_FEATURES] + supported_features = get_supported_features(hass, entry.entity_id) supports_open_close = supported_features & (SUPPORT_OPEN | SUPPORT_CLOSE) # Add conditions for each entity that belongs to this integration diff --git a/tests/components/cover/test_device_condition.py b/tests/components/cover/test_device_condition.py index 8e8d92e5a1c..cf05a112a0a 100644 --- a/tests/components/cover/test_device_condition.py +++ b/tests/components/cover/test_device_condition.py @@ -2,7 +2,13 @@ import pytest import homeassistant.components.automation as automation -from homeassistant.components.cover import DOMAIN +from homeassistant.components.cover import ( + DOMAIN, + SUPPORT_CLOSE, + SUPPORT_OPEN, + SUPPORT_SET_POSITION, + SUPPORT_SET_TILT_POSITION, +) from homeassistant.const import ( CONF_PLATFORM, STATE_CLOSED, @@ -43,65 +49,31 @@ def calls(hass): return async_mock_service(hass, "test", "automation") -async def test_get_conditions(hass, device_reg, entity_reg, enable_custom_integrations): - """Test we get the expected conditions from a cover.""" - platform = getattr(hass.components, f"test.{DOMAIN}") - platform.init() - ent = platform.ENTITIES[0] - - 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", ent.unique_id, device_id=device_entry.id - ) - assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) - - expected_conditions = [ - { - "condition": "device", - "domain": DOMAIN, - "type": "is_open", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_id}", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_closed", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_id}", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_opening", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_id}", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_closing", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_id}", - }, - ] - conditions = await async_get_device_automations(hass, "condition", device_entry.id) - assert_lists_same(conditions, expected_conditions) - - -async def test_get_conditions_set_pos( - hass, device_reg, entity_reg, enable_custom_integrations +@pytest.mark.parametrize( + "set_state,features_reg,features_state,expected_condition_types", + [ + (False, 0, 0, []), + (False, SUPPORT_CLOSE, 0, ["is_open", "is_closed", "is_opening", "is_closing"]), + (False, SUPPORT_OPEN, 0, ["is_open", "is_closed", "is_opening", "is_closing"]), + (False, SUPPORT_SET_POSITION, 0, ["is_position"]), + (False, SUPPORT_SET_TILT_POSITION, 0, ["is_tilt_position"]), + (True, 0, 0, []), + (True, 0, SUPPORT_CLOSE, ["is_open", "is_closed", "is_opening", "is_closing"]), + (True, 0, SUPPORT_OPEN, ["is_open", "is_closed", "is_opening", "is_closing"]), + (True, 0, SUPPORT_SET_POSITION, ["is_position"]), + (True, 0, SUPPORT_SET_TILT_POSITION, ["is_tilt_position"]), + ], +) +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 cover.""" - platform = getattr(hass.components, f"test.{DOMAIN}") - platform.init() - ent = platform.ENTITIES[1] - config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) device_entry = device_reg.async_get_or_create( @@ -109,106 +81,28 @@ async def test_get_conditions_set_pos( connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) entity_reg.async_get_or_create( - DOMAIN, "test", ent.unique_id, device_id=device_entry.id + DOMAIN, + "test", + "5678", + device_id=device_entry.id, + supported_features=features_reg, ) - assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) + if set_state: + hass.states.async_set( + f"{DOMAIN}.test_5678", "attributes", {"supported_features": features_state} + ) + await hass.async_block_till_done() - expected_conditions = [ + expected_conditions = [] + expected_conditions += [ { "condition": "device", "domain": DOMAIN, - "type": "is_open", + "type": condition, "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_id}", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_closed", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_id}", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_opening", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_id}", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_closing", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_id}", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_position", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_id}", - }, - ] - conditions = await async_get_device_automations(hass, "condition", device_entry.id) - assert_lists_same(conditions, expected_conditions) - - -async def test_get_conditions_set_tilt_pos( - hass, device_reg, entity_reg, enable_custom_integrations -): - """Test we get the expected conditions from a cover.""" - platform = getattr(hass.components, f"test.{DOMAIN}") - platform.init() - ent = platform.ENTITIES[2] - - 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", ent.unique_id, device_id=device_entry.id - ) - assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) - - expected_conditions = [ - { - "condition": "device", - "domain": DOMAIN, - "type": "is_open", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_id}", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_closed", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_id}", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_opening", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_id}", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_closing", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_id}", - }, - { - "condition": "device", - "domain": DOMAIN, - "type": "is_tilt_position", - "device_id": device_entry.id, - "entity_id": f"{DOMAIN}.test_{ent.unique_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)