From 6ab37881c9cc2d2e879c31b3d08eb59fc78a2339 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Sat, 12 Jun 2021 21:31:30 +0200 Subject: [PATCH] Improve editing of device actions referencing non-added lock (#51750) --- .../components/lock/device_action.py | 11 +- tests/components/lock/test_device_action.py | 101 +++++++----------- 2 files changed, 42 insertions(+), 70 deletions(-) diff --git a/homeassistant/components/lock/device_action.py b/homeassistant/components/lock/device_action.py index 06a133465aa..6c0eb2a41d4 100644 --- a/homeassistant/components/lock/device_action.py +++ b/homeassistant/components/lock/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, @@ -17,6 +16,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 DOMAIN, SUPPORT_OPEN @@ -40,6 +40,8 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: if entry.domain != DOMAIN: continue + supported_features = get_supported_features(hass, entry.entity_id) + # Add actions for each entity that belongs to this integration base_action = { CONF_DEVICE_ID: device_id, @@ -50,11 +52,8 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]: actions.append({**base_action, CONF_TYPE: "lock"}) actions.append({**base_action, CONF_TYPE: "unlock"}) - state = hass.states.get(entry.entity_id) - if state: - features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0) - if features & (SUPPORT_OPEN): - actions.append({**base_action, CONF_TYPE: "open"}) + if supported_features & (SUPPORT_OPEN): + actions.append({**base_action, CONF_TYPE: "open"}) return actions diff --git a/tests/components/lock/test_device_action.py b/tests/components/lock/test_device_action.py index a84555bdd42..c5a9b19d949 100644 --- a/tests/components/lock/test_device_action.py +++ b/tests/components/lock/test_device_action.py @@ -2,8 +2,7 @@ import pytest import homeassistant.components.automation as automation -from homeassistant.components.lock import DOMAIN -from homeassistant.const import CONF_PLATFORM +from homeassistant.components.lock import DOMAIN, SUPPORT_OPEN from homeassistant.helpers import device_registry from homeassistant.setup import async_setup_component @@ -30,15 +29,25 @@ def entity_reg(hass): return mock_registry(hass) -async def test_get_actions_support_open( - 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_OPEN, 0, ["open"]), + (True, 0, 0, []), + (True, 0, SUPPORT_OPEN, ["open"]), + ], +) +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 lock which supports open.""" - platform = getattr(hass.components, f"test.{DOMAIN}") - platform.init() - assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) - await hass.async_block_till_done() - + """Test we get the expected actions from a lock.""" config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) device_entry = device_reg.async_get_or_create( @@ -48,69 +57,33 @@ async def test_get_actions_support_open( entity_reg.async_get_or_create( DOMAIN, "test", - platform.ENTITIES["support_open"].unique_id, + "5678", device_id=device_entry.id, + supported_features=features_reg, ) - - expected_actions = [ + if set_state: + hass.states.async_set( + f"{DOMAIN}.test_5678", "attributes", {"supported_features": features_state} + ) + expected_actions = [] + basic_action_types = ["lock", "unlock"] + expected_actions += [ { "domain": DOMAIN, - "type": "lock", + "type": action, "device_id": device_entry.id, - "entity_id": "lock.support_open_lock", - }, - { - "domain": DOMAIN, - "type": "unlock", - "device_id": device_entry.id, - "entity_id": "lock.support_open_lock", - }, - { - "domain": DOMAIN, - "type": "open", - "device_id": device_entry.id, - "entity_id": "lock.support_open_lock", - }, + "entity_id": f"{DOMAIN}.test_5678", + } + for action in basic_action_types ] - actions = await async_get_device_automations(hass, "action", device_entry.id) - assert_lists_same(actions, expected_actions) - - -async def test_get_actions_not_support_open( - hass, device_reg, entity_reg, enable_custom_integrations -): - """Test we get the expected actions from a lock which doesn't support open.""" - platform = getattr(hass.components, f"test.{DOMAIN}") - platform.init() - assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) - await hass.async_block_till_done() - - 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", - platform.ENTITIES["no_support_open"].unique_id, - device_id=device_entry.id, - ) - - expected_actions = [ + expected_actions += [ { "domain": DOMAIN, - "type": "lock", + "type": action, "device_id": device_entry.id, - "entity_id": "lock.no_support_open_lock", - }, - { - "domain": DOMAIN, - "type": "unlock", - "device_id": device_entry.id, - "entity_id": "lock.no_support_open_lock", - }, + "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)