mirror of
https://github.com/home-assistant/core.git
synced 2025-07-11 23:37:18 +00:00
Improve editing of device actions referencing non-added cover (#51748)
This commit is contained in:
parent
6ab37881c9
commit
b8669bf4c1
@ -5,7 +5,6 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
ATTR_SUPPORTED_FEATURES,
|
|
||||||
CONF_DEVICE_ID,
|
CONF_DEVICE_ID,
|
||||||
CONF_DOMAIN,
|
CONF_DOMAIN,
|
||||||
CONF_ENTITY_ID,
|
CONF_ENTITY_ID,
|
||||||
@ -21,6 +20,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import Context, HomeAssistant
|
from homeassistant.core import Context, HomeAssistant
|
||||||
from homeassistant.helpers import entity_registry
|
from homeassistant.helpers import entity_registry
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.entity import get_supported_features
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
ATTR_POSITION,
|
ATTR_POSITION,
|
||||||
@ -68,11 +68,7 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]:
|
|||||||
if entry.domain != DOMAIN:
|
if entry.domain != DOMAIN:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
state = hass.states.get(entry.entity_id)
|
supported_features = get_supported_features(hass, entry.entity_id)
|
||||||
if not state or ATTR_SUPPORTED_FEATURES not in state.attributes:
|
|
||||||
continue
|
|
||||||
|
|
||||||
supported_features = state.attributes[ATTR_SUPPORTED_FEATURES]
|
|
||||||
|
|
||||||
# Add actions for each entity that belongs to this integration
|
# Add actions for each entity that belongs to this integration
|
||||||
base_action = {
|
base_action = {
|
||||||
|
@ -2,7 +2,16 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import homeassistant.components.automation as automation
|
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.const import CONF_PLATFORM
|
||||||
from homeassistant.helpers import device_registry
|
from homeassistant.helpers import device_registry
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
@ -31,56 +40,37 @@ def entity_reg(hass):
|
|||||||
return mock_registry(hass)
|
return mock_registry(hass)
|
||||||
|
|
||||||
|
|
||||||
async def test_get_actions(hass, device_reg, entity_reg, enable_custom_integrations):
|
@pytest.mark.parametrize(
|
||||||
"""Test we get the expected actions from a cover."""
|
"set_state,features_reg,features_state,expected_action_types",
|
||||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
[
|
||||||
platform.init()
|
(False, 0, 0, []),
|
||||||
ent = platform.ENTITIES[0]
|
(False, SUPPORT_CLOSE_TILT, 0, ["close_tilt"]),
|
||||||
|
(False, SUPPORT_CLOSE, 0, ["close"]),
|
||||||
config_entry = MockConfigEntry(domain="test", data={})
|
(False, SUPPORT_OPEN_TILT, 0, ["open_tilt"]),
|
||||||
config_entry.add_to_hass(hass)
|
(False, SUPPORT_OPEN, 0, ["open"]),
|
||||||
device_entry = device_reg.async_get_or_create(
|
(False, SUPPORT_SET_POSITION, 0, ["set_position"]),
|
||||||
config_entry_id=config_entry.entry_id,
|
(False, SUPPORT_SET_TILT_POSITION, 0, ["set_tilt_position"]),
|
||||||
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
(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(
|
async def test_get_actions(
|
||||||
DOMAIN, "test", ent.unique_id, device_id=device_entry.id
|
hass,
|
||||||
)
|
device_reg,
|
||||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
entity_reg,
|
||||||
await hass.async_block_till_done()
|
set_state,
|
||||||
|
features_reg,
|
||||||
expected_actions = [
|
features_state,
|
||||||
{
|
expected_action_types,
|
||||||
"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
|
|
||||||
):
|
):
|
||||||
"""Test we get the expected actions from a cover."""
|
"""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 = MockConfigEntry(domain="test", data={})
|
||||||
config_entry.add_to_hass(hass)
|
config_entry.add_to_hass(hass)
|
||||||
device_entry = device_reg.async_get_or_create(
|
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")},
|
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||||
)
|
)
|
||||||
entity_reg.async_get_or_create(
|
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()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
expected_actions = [
|
expected_actions = []
|
||||||
|
expected_actions += [
|
||||||
{
|
{
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": "open",
|
"type": action,
|
||||||
"device_id": device_entry.id,
|
"device_id": device_entry.id,
|
||||||
"entity_id": ent.entity_id,
|
"entity_id": f"{DOMAIN}.test_5678",
|
||||||
},
|
}
|
||||||
{
|
for action in expected_action_types
|
||||||
"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,
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
actions = await async_get_device_automations(hass, "action", device_entry.id)
|
actions = await async_get_device_automations(hass, "action", device_entry.id)
|
||||||
assert_lists_same(actions, expected_actions)
|
assert_lists_same(actions, expected_actions)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user