core/tests/components/cover/test_device_action.py

619 lines
22 KiB
Python

"""The tests for Cover device actions."""
import pytest
from pytest_unordered import unordered
from homeassistant.components import automation
from homeassistant.components.cover import DOMAIN, CoverEntityFeature
from homeassistant.components.device_automation import DeviceAutomationType
from homeassistant.const import CONF_PLATFORM, EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_registry import RegistryEntryHider
from homeassistant.setup import async_setup_component
from .common import MockCover
from tests.common import (
MockConfigEntry,
async_get_device_automation_capabilities,
async_get_device_automations,
async_mock_service,
setup_test_component_platform,
)
@pytest.fixture(autouse=True, name="stub_blueprint_populate")
def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
"""Stub copying the blueprints to the config folder."""
@pytest.mark.parametrize(
("set_state", "features_reg", "features_state", "expected_action_types"),
[
(False, 0, 0, []),
(False, CoverEntityFeature.CLOSE_TILT, 0, ["close_tilt"]),
(False, CoverEntityFeature.CLOSE, 0, ["close"]),
(False, CoverEntityFeature.OPEN_TILT, 0, ["open_tilt"]),
(False, CoverEntityFeature.OPEN, 0, ["open"]),
(False, CoverEntityFeature.SET_POSITION, 0, ["set_position"]),
(False, CoverEntityFeature.SET_TILT_POSITION, 0, ["set_tilt_position"]),
(False, CoverEntityFeature.STOP, 0, ["stop"]),
(True, 0, 0, []),
(True, 0, CoverEntityFeature.CLOSE_TILT, ["close_tilt"]),
(True, 0, CoverEntityFeature.CLOSE, ["close"]),
(True, 0, CoverEntityFeature.OPEN_TILT, ["open_tilt"]),
(True, 0, CoverEntityFeature.OPEN, ["open"]),
(True, 0, CoverEntityFeature.SET_POSITION, ["set_position"]),
(True, 0, CoverEntityFeature.SET_TILT_POSITION, ["set_tilt_position"]),
(True, 0, CoverEntityFeature.STOP, ["stop"]),
],
)
async def test_get_actions(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
set_state,
features_reg,
features_state,
expected_action_types,
) -> None:
"""Test we get the expected actions from a cover."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_entry = entity_registry.async_get_or_create(
DOMAIN,
"test",
"5678",
device_id=device_entry.id,
supported_features=features_reg,
)
if set_state:
hass.states.async_set(
entity_entry.entity_id, "attributes", {"supported_features": features_state}
)
await hass.async_block_till_done()
expected_actions = []
expected_actions += [
{
"domain": DOMAIN,
"type": action,
"device_id": device_entry.id,
"entity_id": entity_entry.id,
"metadata": {"secondary": False},
}
for action in expected_action_types
]
actions = await async_get_device_automations(
hass, DeviceAutomationType.ACTION, device_entry.id
)
assert actions == unordered(expected_actions)
@pytest.mark.parametrize(
("hidden_by", "entity_category"),
[
(RegistryEntryHider.INTEGRATION, None),
(RegistryEntryHider.USER, None),
(None, EntityCategory.CONFIG),
(None, EntityCategory.DIAGNOSTIC),
],
)
async def test_get_actions_hidden_auxiliary(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
hidden_by,
entity_category,
) -> None:
"""Test we get the expected actions from a hidden or auxiliary entity."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_entry = entity_registry.async_get_or_create(
DOMAIN,
"test",
"5678",
device_id=device_entry.id,
entity_category=entity_category,
hidden_by=hidden_by,
supported_features=CoverEntityFeature.CLOSE,
)
expected_actions = []
expected_actions += [
{
"domain": DOMAIN,
"type": action,
"device_id": device_entry.id,
"entity_id": entity_entry.id,
"metadata": {"secondary": True},
}
for action in ("close",)
]
actions = await async_get_device_automations(
hass, DeviceAutomationType.ACTION, device_entry.id
)
assert actions == unordered(expected_actions)
async def test_get_action_capabilities(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test we get the expected capabilities from a cover action."""
ent = MockCover(
name="Set position cover",
unique_id="unique_set_pos_cover",
current_cover_position=50,
supported_features=CoverEntityFeature.OPEN
| CoverEntityFeature.CLOSE
| CoverEntityFeature.STOP
| CoverEntityFeature.OPEN_TILT
| CoverEntityFeature.CLOSE_TILT
| CoverEntityFeature.STOP_TILT,
)
setup_test_component_platform(hass, DOMAIN, [ent])
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_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_registry.async_get_or_create(
DOMAIN, "test", ent.unique_id, device_id=device_entry.id
)
actions = await async_get_device_automations(
hass, DeviceAutomationType.ACTION, device_entry.id
)
assert len(actions) == 5 # open, close, open_tilt, close_tilt
action_types = {action["type"] for action in actions}
assert action_types == {"open", "close", "stop", "open_tilt", "close_tilt"}
for action in actions:
capabilities = await async_get_device_automation_capabilities(
hass, DeviceAutomationType.ACTION, action
)
assert capabilities == {"extra_fields": []}
async def test_get_action_capabilities_legacy(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test we get the expected capabilities from a cover action."""
ent = MockCover(
name="Set position cover",
unique_id="unique_set_pos_cover",
current_cover_position=50,
supported_features=CoverEntityFeature.OPEN
| CoverEntityFeature.CLOSE
| CoverEntityFeature.STOP
| CoverEntityFeature.OPEN_TILT
| CoverEntityFeature.CLOSE_TILT
| CoverEntityFeature.STOP_TILT,
)
setup_test_component_platform(hass, DOMAIN, [ent])
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_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_registry.async_get_or_create(
DOMAIN, "test", ent.unique_id, device_id=device_entry.id
)
actions = await async_get_device_automations(
hass, DeviceAutomationType.ACTION, device_entry.id
)
assert len(actions) == 5 # open, close, open_tilt, close_tilt
action_types = {action["type"] for action in actions}
assert action_types == {"open", "close", "stop", "open_tilt", "close_tilt"}
for action in actions:
action["entity_id"] = entity_registry.async_get(action["entity_id"]).entity_id
capabilities = await async_get_device_automation_capabilities(
hass, DeviceAutomationType.ACTION, action
)
assert capabilities == {"extra_fields": []}
async def test_get_action_capabilities_set_pos(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
mock_cover_entities: list[MockCover],
) -> None:
"""Test we get the expected capabilities from a cover action."""
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
ent = mock_cover_entities[1]
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_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_registry.async_get_or_create(
DOMAIN, "test", ent.unique_id, device_id=device_entry.id
)
expected_capabilities = {
"extra_fields": [
{
"name": "position",
"optional": True,
"type": "integer",
"default": 0,
"valueMax": 100,
"valueMin": 0,
}
]
}
actions = await async_get_device_automations(
hass, DeviceAutomationType.ACTION, device_entry.id
)
assert len(actions) == 4 # set_position, open, close, stop
action_types = {action["type"] for action in actions}
assert action_types == {"set_position", "open", "close", "stop"}
for action in actions:
capabilities = await async_get_device_automation_capabilities(
hass, DeviceAutomationType.ACTION, action
)
if action["type"] == "set_position":
assert capabilities == expected_capabilities
else:
assert capabilities == {"extra_fields": []}
async def test_get_action_capabilities_set_tilt_pos(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
mock_cover_entities: list[MockCover],
) -> None:
"""Test we get the expected capabilities from a cover action."""
setup_test_component_platform(hass, DOMAIN, mock_cover_entities)
ent = mock_cover_entities[3]
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_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_registry.async_get_or_create(
DOMAIN, "test", ent.unique_id, device_id=device_entry.id
)
expected_capabilities = {
"extra_fields": [
{
"name": "position",
"optional": True,
"type": "integer",
"default": 0,
"valueMax": 100,
"valueMin": 0,
}
]
}
actions = await async_get_device_automations(
hass, DeviceAutomationType.ACTION, device_entry.id
)
assert len(actions) == 5
action_types = {action["type"] for action in actions}
assert action_types == {
"open",
"close",
"set_tilt_position",
"open_tilt",
"close_tilt",
}
for action in actions:
capabilities = await async_get_device_automation_capabilities(
hass, DeviceAutomationType.ACTION, action
)
if action["type"] == "set_tilt_position":
assert capabilities == expected_capabilities
else:
assert capabilities == {"extra_fields": []}
async def test_action(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
mock_cover_entities: list[MockCover],
) -> None:
"""Test for cover actions."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entry = entity_registry.async_get_or_create(
DOMAIN, "test", "5678", device_id=device_entry.id
)
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {"platform": "event", "event_type": "test_event_open"},
"action": {
"domain": DOMAIN,
"device_id": device_entry.id,
"entity_id": entry.id,
"type": "open",
},
},
{
"trigger": {"platform": "event", "event_type": "test_event_close"},
"action": {
"domain": DOMAIN,
"device_id": device_entry.id,
"entity_id": entry.id,
"type": "close",
},
},
{
"trigger": {"platform": "event", "event_type": "test_event_stop"},
"action": {
"domain": DOMAIN,
"device_id": device_entry.id,
"entity_id": entry.id,
"type": "stop",
},
},
]
},
)
await hass.async_block_till_done()
open_calls = async_mock_service(hass, "cover", "open_cover")
close_calls = async_mock_service(hass, "cover", "close_cover")
stop_calls = async_mock_service(hass, "cover", "stop_cover")
hass.bus.async_fire("test_event_open")
await hass.async_block_till_done()
assert len(open_calls) == 1
assert len(close_calls) == 0
assert len(stop_calls) == 0
hass.bus.async_fire("test_event_close")
await hass.async_block_till_done()
assert len(open_calls) == 1
assert len(close_calls) == 1
assert len(stop_calls) == 0
hass.bus.async_fire("test_event_stop")
await hass.async_block_till_done()
assert len(open_calls) == 1
assert len(close_calls) == 1
assert len(stop_calls) == 1
assert open_calls[0].domain == DOMAIN
assert open_calls[0].service == "open_cover"
assert open_calls[0].data == {"entity_id": entry.entity_id}
assert close_calls[0].domain == DOMAIN
assert close_calls[0].service == "close_cover"
assert close_calls[0].data == {"entity_id": entry.entity_id}
assert stop_calls[0].domain == DOMAIN
assert stop_calls[0].service == "stop_cover"
assert stop_calls[0].data == {"entity_id": entry.entity_id}
async def test_action_legacy(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
mock_cover_entities: list[MockCover],
) -> None:
"""Test for cover actions."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entry = entity_registry.async_get_or_create(
DOMAIN, "test", "5678", device_id=device_entry.id
)
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {"platform": "event", "event_type": "test_event_open"},
"action": {
"domain": DOMAIN,
"device_id": device_entry.id,
"entity_id": entry.id,
"type": "open",
},
},
]
},
)
await hass.async_block_till_done()
open_calls = async_mock_service(hass, "cover", "open_cover")
hass.bus.async_fire("test_event_open")
await hass.async_block_till_done()
assert len(open_calls) == 1
assert open_calls[0].domain == DOMAIN
assert open_calls[0].service == "open_cover"
assert open_calls[0].data == {"entity_id": entry.entity_id}
async def test_action_tilt(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
mock_cover_entities: list[MockCover],
) -> None:
"""Test for cover tilt actions."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entry = entity_registry.async_get_or_create(
DOMAIN, "test", "5678", device_id=device_entry.id
)
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {"platform": "event", "event_type": "test_event_open"},
"action": {
"domain": DOMAIN,
"device_id": device_entry.id,
"entity_id": entry.id,
"type": "open_tilt",
},
},
{
"trigger": {"platform": "event", "event_type": "test_event_close"},
"action": {
"domain": DOMAIN,
"device_id": device_entry.id,
"entity_id": entry.id,
"type": "close_tilt",
},
},
]
},
)
await hass.async_block_till_done()
open_calls = async_mock_service(hass, "cover", "open_cover_tilt")
close_calls = async_mock_service(hass, "cover", "close_cover_tilt")
hass.bus.async_fire("test_event_open")
await hass.async_block_till_done()
assert len(open_calls) == 1
assert len(close_calls) == 0
hass.bus.async_fire("test_event_close")
await hass.async_block_till_done()
assert len(open_calls) == 1
assert len(close_calls) == 1
hass.bus.async_fire("test_event_stop")
await hass.async_block_till_done()
assert len(open_calls) == 1
assert len(close_calls) == 1
assert open_calls[0].domain == DOMAIN
assert open_calls[0].service == "open_cover_tilt"
assert open_calls[0].data == {"entity_id": entry.entity_id}
assert close_calls[0].domain == DOMAIN
assert close_calls[0].service == "close_cover_tilt"
assert close_calls[0].data == {"entity_id": entry.entity_id}
async def test_action_set_position(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
mock_cover_entities: list[MockCover],
) -> None:
"""Test for cover set position actions."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entry = entity_registry.async_get_or_create(
DOMAIN, "test", "5678", device_id=device_entry.id
)
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "event",
"event_type": "test_event_set_pos",
},
"action": {
"domain": DOMAIN,
"device_id": device_entry.id,
"entity_id": entry.id,
"type": "set_position",
"position": 25,
},
},
{
"trigger": {
"platform": "event",
"event_type": "test_event_set_tilt_pos",
},
"action": {
"domain": DOMAIN,
"device_id": device_entry.id,
"entity_id": entry.id,
"type": "set_tilt_position",
"position": 75,
},
},
]
},
)
await hass.async_block_till_done()
cover_pos_calls = async_mock_service(hass, "cover", "set_cover_position")
tilt_pos_calls = async_mock_service(hass, "cover", "set_cover_tilt_position")
hass.bus.async_fire("test_event_set_pos")
await hass.async_block_till_done()
assert len(cover_pos_calls) == 1
assert len(tilt_pos_calls) == 0
hass.bus.async_fire("test_event_set_tilt_pos")
await hass.async_block_till_done()
assert len(cover_pos_calls) == 1
assert len(tilt_pos_calls) == 1
assert cover_pos_calls[0].domain == DOMAIN
assert cover_pos_calls[0].service == "set_cover_position"
assert cover_pos_calls[0].data == {"entity_id": entry.entity_id, "position": 25}
assert tilt_pos_calls[0].domain == DOMAIN
assert tilt_pos_calls[0].service == "set_cover_tilt_position"
assert tilt_pos_calls[0].data == {"entity_id": entry.entity_id, "tilt_position": 75}