2019-10-18 00:20:10 +00:00
|
|
|
"""The tests for Lock device actions."""
|
|
|
|
import pytest
|
|
|
|
|
2019-12-09 11:19:11 +00:00
|
|
|
import homeassistant.components.automation as automation
|
2021-12-20 21:18:53 +00:00
|
|
|
from homeassistant.components.device_automation import DeviceAutomationType
|
2022-04-03 03:57:32 +00:00
|
|
|
from homeassistant.components.lock import DOMAIN, LockEntityFeature
|
2019-10-18 00:20:10 +00:00
|
|
|
from homeassistant.helpers import device_registry
|
2022-04-20 17:48:46 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
|
|
|
from homeassistant.helpers.entity_registry import RegistryEntryHider
|
2019-12-09 11:19:11 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2019-10-18 00:20:10 +00:00
|
|
|
|
|
|
|
from tests.common import (
|
|
|
|
MockConfigEntry,
|
|
|
|
assert_lists_same,
|
2019-12-09 11:19:11 +00:00
|
|
|
async_get_device_automations,
|
2019-10-18 00:20:10 +00:00
|
|
|
async_mock_service,
|
|
|
|
mock_device_registry,
|
|
|
|
mock_registry,
|
|
|
|
)
|
2021-03-02 08:02:04 +00:00
|
|
|
from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401
|
2019-10-18 00:20:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def device_reg(hass):
|
|
|
|
"""Return an empty, loaded, registry."""
|
|
|
|
return mock_device_registry(hass)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def entity_reg(hass):
|
|
|
|
"""Return an empty, loaded, registry."""
|
|
|
|
return mock_registry(hass)
|
|
|
|
|
|
|
|
|
2021-06-12 19:31:30 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"set_state,features_reg,features_state,expected_action_types",
|
|
|
|
[
|
|
|
|
(False, 0, 0, []),
|
2022-04-03 03:57:32 +00:00
|
|
|
(False, LockEntityFeature.OPEN, 0, ["open"]),
|
2021-06-12 19:31:30 +00:00
|
|
|
(True, 0, 0, []),
|
2022-04-03 03:57:32 +00:00
|
|
|
(True, 0, LockEntityFeature.OPEN, ["open"]),
|
2021-06-12 19:31:30 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_get_actions(
|
|
|
|
hass,
|
|
|
|
device_reg,
|
|
|
|
entity_reg,
|
|
|
|
set_state,
|
|
|
|
features_reg,
|
|
|
|
features_state,
|
|
|
|
expected_action_types,
|
2021-05-17 13:48:41 +00:00
|
|
|
):
|
2021-06-12 19:31:30 +00:00
|
|
|
"""Test we get the expected actions from a lock."""
|
2019-10-18 00:20:10 +00:00
|
|
|
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",
|
2021-06-12 19:31:30 +00:00
|
|
|
"5678",
|
2019-10-18 00:20:10 +00:00
|
|
|
device_id=device_entry.id,
|
2021-06-12 19:31:30 +00:00
|
|
|
supported_features=features_reg,
|
2019-10-18 00:20:10 +00:00
|
|
|
)
|
2021-06-12 19:31:30 +00:00
|
|
|
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 += [
|
2019-10-18 00:20:10 +00:00
|
|
|
{
|
|
|
|
"domain": DOMAIN,
|
2021-06-12 19:31:30 +00:00
|
|
|
"type": action,
|
2019-10-18 00:20:10 +00:00
|
|
|
"device_id": device_entry.id,
|
2021-06-12 19:31:30 +00:00
|
|
|
"entity_id": f"{DOMAIN}.test_5678",
|
2022-04-20 17:48:46 +00:00
|
|
|
"metadata": {"secondary": False},
|
2021-06-12 19:31:30 +00:00
|
|
|
}
|
|
|
|
for action in basic_action_types
|
2019-10-18 00:20:10 +00:00
|
|
|
]
|
2021-06-12 19:31:30 +00:00
|
|
|
expected_actions += [
|
2019-10-18 00:20:10 +00:00
|
|
|
{
|
|
|
|
"domain": DOMAIN,
|
2021-06-12 19:31:30 +00:00
|
|
|
"type": action,
|
2019-10-18 00:20:10 +00:00
|
|
|
"device_id": device_entry.id,
|
2021-06-12 19:31:30 +00:00
|
|
|
"entity_id": f"{DOMAIN}.test_5678",
|
2022-04-20 17:48:46 +00:00
|
|
|
"metadata": {"secondary": False},
|
2021-06-12 19:31:30 +00:00
|
|
|
}
|
|
|
|
for action in expected_action_types
|
2019-10-18 00:20:10 +00:00
|
|
|
]
|
2021-12-20 21:18:53 +00:00
|
|
|
actions = await async_get_device_automations(
|
|
|
|
hass, DeviceAutomationType.ACTION, device_entry.id
|
|
|
|
)
|
2019-10-18 00:20:10 +00:00
|
|
|
assert_lists_same(actions, expected_actions)
|
|
|
|
|
|
|
|
|
2022-04-20 17:48:46 +00:00
|
|
|
@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,
|
|
|
|
device_reg,
|
|
|
|
entity_reg,
|
|
|
|
hidden_by,
|
|
|
|
entity_category,
|
|
|
|
):
|
|
|
|
"""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_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",
|
|
|
|
"5678",
|
|
|
|
device_id=device_entry.id,
|
|
|
|
entity_category=entity_category,
|
|
|
|
hidden_by=hidden_by,
|
|
|
|
supported_features=0,
|
|
|
|
)
|
|
|
|
expected_actions = []
|
|
|
|
expected_actions += [
|
|
|
|
{
|
|
|
|
"domain": DOMAIN,
|
|
|
|
"type": action,
|
|
|
|
"device_id": device_entry.id,
|
|
|
|
"entity_id": f"{DOMAIN}.test_5678",
|
|
|
|
"metadata": {"secondary": True},
|
|
|
|
}
|
|
|
|
for action in ["lock", "unlock"]
|
|
|
|
]
|
|
|
|
actions = await async_get_device_automations(
|
|
|
|
hass, DeviceAutomationType.ACTION, device_entry.id
|
|
|
|
)
|
|
|
|
assert_lists_same(actions, expected_actions)
|
|
|
|
|
|
|
|
|
2019-10-18 00:20:10 +00:00
|
|
|
async def test_action(hass):
|
|
|
|
"""Test for lock actions."""
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
automation.DOMAIN,
|
|
|
|
{
|
|
|
|
automation.DOMAIN: [
|
|
|
|
{
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event_lock"},
|
|
|
|
"action": {
|
|
|
|
"domain": DOMAIN,
|
|
|
|
"device_id": "abcdefgh",
|
|
|
|
"entity_id": "lock.entity",
|
|
|
|
"type": "lock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event_unlock"},
|
|
|
|
"action": {
|
|
|
|
"domain": DOMAIN,
|
|
|
|
"device_id": "abcdefgh",
|
|
|
|
"entity_id": "lock.entity",
|
|
|
|
"type": "unlock",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"trigger": {"platform": "event", "event_type": "test_event_open"},
|
|
|
|
"action": {
|
|
|
|
"domain": DOMAIN,
|
|
|
|
"device_id": "abcdefgh",
|
|
|
|
"entity_id": "lock.entity",
|
|
|
|
"type": "open",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
2020-08-05 12:58:19 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-10-18 00:20:10 +00:00
|
|
|
|
|
|
|
lock_calls = async_mock_service(hass, "lock", "lock")
|
|
|
|
unlock_calls = async_mock_service(hass, "lock", "unlock")
|
|
|
|
open_calls = async_mock_service(hass, "lock", "open")
|
|
|
|
|
|
|
|
hass.bus.async_fire("test_event_lock")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(lock_calls) == 1
|
|
|
|
assert len(unlock_calls) == 0
|
|
|
|
assert len(open_calls) == 0
|
|
|
|
|
|
|
|
hass.bus.async_fire("test_event_unlock")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(lock_calls) == 1
|
|
|
|
assert len(unlock_calls) == 1
|
|
|
|
assert len(open_calls) == 0
|
|
|
|
|
|
|
|
hass.bus.async_fire("test_event_open")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(lock_calls) == 1
|
|
|
|
assert len(unlock_calls) == 1
|
|
|
|
assert len(open_calls) == 1
|