From b8669bf4c131b3cdf80b66c97785d69af209d521 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Sat, 12 Jun 2021 21:40:32 +0200 Subject: [PATCH] Improve editing of device actions referencing non-added cover (#51748) --- .../components/cover/device_action.py | 8 +- tests/components/cover/test_device_action.py | 215 +++++------------- 2 files changed, 56 insertions(+), 167 deletions(-) diff --git a/homeassistant/components/cover/device_action.py b/homeassistant/components/cover/device_action.py index 24c8a0c8b3b..13ef4523f5b 100644 --- a/homeassistant/components/cover/device_action.py +++ b/homeassistant/components/cover/device_action.py @@ -5,7 +5,6 @@ import voluptuous as vol from homeassistant.const import ( ATTR_ENTITY_ID, - ATTR_SUPPORTED_FEATURES, CONF_DEVICE_ID, CONF_DOMAIN, CONF_ENTITY_ID, @@ -21,6 +20,7 @@ from homeassistant.const import ( from homeassistant.core import Context, HomeAssistant from homeassistant.helpers import entity_registry import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.entity import get_supported_features from . import ( ATTR_POSITION, @@ -68,11 +68,7 @@ async def async_get_actions(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) # Add actions for each entity that belongs to this integration base_action = { diff --git a/tests/components/cover/test_device_action.py b/tests/components/cover/test_device_action.py index 60bb4e5401b..0dc76aa7e61 100644 --- a/tests/components/cover/test_device_action.py +++ b/tests/components/cover/test_device_action.py @@ -2,7 +2,16 @@ import pytest import homeassistant.components.automation as automation -from homeassistant.components.cover import DOMAIN +from homeassistant.components.cover import ( + DOMAIN, + SUPPORT_CLOSE, + SUPPORT_CLOSE_TILT, + SUPPORT_OPEN, + SUPPORT_OPEN_TILT, + SUPPORT_SET_POSITION, + SUPPORT_SET_TILT_POSITION, + SUPPORT_STOP, +) from homeassistant.const import CONF_PLATFORM from homeassistant.helpers import device_registry from homeassistant.setup import async_setup_component @@ -31,56 +40,37 @@ def entity_reg(hass): return mock_registry(hass) -async def test_get_actions(hass, device_reg, entity_reg, enable_custom_integrations): - """Test we get the expected actions 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"}}) - await hass.async_block_till_done() - - expected_actions = [ - { - "domain": DOMAIN, - "type": "open", - "device_id": device_entry.id, - "entity_id": ent.entity_id, - }, - { - "domain": DOMAIN, - "type": "close", - "device_id": device_entry.id, - "entity_id": ent.entity_id, - }, - { - "domain": DOMAIN, - "type": "stop", - "device_id": device_entry.id, - "entity_id": ent.entity_id, - }, - ] - actions = await async_get_device_automations(hass, "action", device_entry.id) - assert_lists_same(actions, expected_actions) - - -async def test_get_actions_tilt( - hass, device_reg, entity_reg, enable_custom_integrations +@pytest.mark.parametrize( + "set_state,features_reg,features_state,expected_action_types", + [ + (False, 0, 0, []), + (False, SUPPORT_CLOSE_TILT, 0, ["close_tilt"]), + (False, SUPPORT_CLOSE, 0, ["close"]), + (False, SUPPORT_OPEN_TILT, 0, ["open_tilt"]), + (False, SUPPORT_OPEN, 0, ["open"]), + (False, SUPPORT_SET_POSITION, 0, ["set_position"]), + (False, SUPPORT_SET_TILT_POSITION, 0, ["set_tilt_position"]), + (False, SUPPORT_STOP, 0, ["stop"]), + (True, 0, 0, []), + (True, 0, SUPPORT_CLOSE_TILT, ["close_tilt"]), + (True, 0, SUPPORT_CLOSE, ["close"]), + (True, 0, SUPPORT_OPEN_TILT, ["open_tilt"]), + (True, 0, SUPPORT_OPEN, ["open"]), + (True, 0, SUPPORT_SET_POSITION, ["set_position"]), + (True, 0, SUPPORT_SET_TILT_POSITION, ["set_tilt_position"]), + (True, 0, SUPPORT_STOP, ["stop"]), + ], +) +async def test_get_actions( + hass, + device_reg, + entity_reg, + set_state, + features_reg, + features_state, + expected_action_types, ): """Test we get the expected actions from a cover.""" - platform = getattr(hass.components, f"test.{DOMAIN}") - platform.init() - ent = platform.ENTITIES[3] - config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) device_entry = device_reg.async_get_or_create( @@ -88,124 +78,27 @@ async def test_get_actions_tilt( 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_actions = [ + expected_actions = [] + expected_actions += [ { "domain": DOMAIN, - "type": "open", + "type": action, "device_id": device_entry.id, - "entity_id": ent.entity_id, - }, - { - "domain": DOMAIN, - "type": "close", - "device_id": device_entry.id, - "entity_id": ent.entity_id, - }, - { - "domain": DOMAIN, - "type": "stop", - "device_id": device_entry.id, - "entity_id": ent.entity_id, - }, - { - "domain": DOMAIN, - "type": "open_tilt", - "device_id": device_entry.id, - "entity_id": ent.entity_id, - }, - { - "domain": DOMAIN, - "type": "close_tilt", - "device_id": device_entry.id, - "entity_id": ent.entity_id, - }, - ] - actions = await async_get_device_automations(hass, "action", device_entry.id) - assert_lists_same(actions, expected_actions) - - -async def test_get_actions_set_pos( - hass, device_reg, entity_reg, enable_custom_integrations -): - """Test we get the expected actions 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( - 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"}}) - await hass.async_block_till_done() - - expected_actions = [ - { - "domain": DOMAIN, - "type": "set_position", - "device_id": device_entry.id, - "entity_id": ent.entity_id, - }, - ] - actions = await async_get_device_automations(hass, "action", device_entry.id) - assert_lists_same(actions, expected_actions) - - -async def test_get_actions_set_tilt_pos( - hass, device_reg, entity_reg, enable_custom_integrations -): - """Test we get the expected actions 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"}}) - await hass.async_block_till_done() - - expected_actions = [ - { - "domain": DOMAIN, - "type": "open", - "device_id": device_entry.id, - "entity_id": ent.entity_id, - }, - { - "domain": DOMAIN, - "type": "close", - "device_id": device_entry.id, - "entity_id": ent.entity_id, - }, - { - "domain": DOMAIN, - "type": "stop", - "device_id": device_entry.id, - "entity_id": ent.entity_id, - }, - { - "domain": DOMAIN, - "type": "set_tilt_position", - "device_id": device_entry.id, - "entity_id": ent.entity_id, - }, + "entity_id": f"{DOMAIN}.test_5678", + } + for action in expected_action_types ] actions = await async_get_device_automations(hass, "action", device_entry.id) assert_lists_same(actions, expected_actions)