Improve editing of device actions referencing non-added cover (#51748)

This commit is contained in:
Erik Montnemery 2021-06-12 21:40:32 +02:00 committed by GitHub
parent 6ab37881c9
commit b8669bf4c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 167 deletions

View File

@ -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 = {

View File

@ -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")},
@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"]),
],
)
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
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,
)
if set_state:
hass.states.async_set(
f"{DOMAIN}.test_5678", "attributes", {"supported_features": features_state}
)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
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)