Improve editing of device triggers referencing non-added cover (#51703)

pull/51705/head
Erik Montnemery 2021-06-10 12:46:28 +02:00 committed by GitHub
parent 181a4519b8
commit c1bc99890d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 157 deletions

View File

@ -10,7 +10,6 @@ from homeassistant.components.homeassistant.triggers import (
state as state_trigger,
)
from homeassistant.const import (
ATTR_SUPPORTED_FEATURES,
CONF_ABOVE,
CONF_BELOW,
CONF_DEVICE_ID,
@ -27,6 +26,7 @@ from homeassistant.const import (
)
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers import config_validation as cv, entity_registry
from homeassistant.helpers.entity import get_supported_features
from homeassistant.helpers.typing import ConfigType
from . import (
@ -77,11 +77,7 @@ async def async_get_triggers(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)
supports_open_close = supported_features & (SUPPORT_OPEN | SUPPORT_CLOSE)
# Add triggers for each entity that belongs to this integration

View File

@ -4,7 +4,12 @@ from datetime import timedelta
import pytest
import homeassistant.components.automation as automation
from homeassistant.components.cover import DOMAIN
from homeassistant.components.cover import (
DOMAIN,
SUPPORT_OPEN,
SUPPORT_SET_POSITION,
SUPPORT_SET_TILT_POSITION,
)
from homeassistant.const import (
CONF_PLATFORM,
STATE_CLOSED,
@ -47,65 +52,47 @@ def calls(hass):
return async_mock_service(hass, "test", "automation")
async def test_get_triggers(hass, device_reg, entity_reg, enable_custom_integrations):
"""Test we get the expected triggers 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")},
)
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"}})
expected_triggers = [
{
"platform": "device",
"domain": DOMAIN,
"type": "opened",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"platform": "device",
"domain": DOMAIN,
"type": "closed",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"platform": "device",
"domain": DOMAIN,
"type": "opening",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"platform": "device",
"domain": DOMAIN,
"type": "closing",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
]
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
assert_lists_same(triggers, expected_triggers)
async def test_get_triggers_set_pos(
hass, device_reg, entity_reg, enable_custom_integrations
@pytest.mark.parametrize(
"set_state,features_reg,features_state,expected_trigger_types",
[
(False, SUPPORT_OPEN, 0, ["opened", "closed", "opening", "closing"]),
(
False,
SUPPORT_OPEN | SUPPORT_SET_POSITION,
0,
["opened", "closed", "opening", "closing", "position"],
),
(
False,
SUPPORT_OPEN | SUPPORT_SET_TILT_POSITION,
0,
["opened", "closed", "opening", "closing", "tilt_position"],
),
(True, 0, SUPPORT_OPEN, ["opened", "closed", "opening", "closing"]),
(
True,
0,
SUPPORT_OPEN | SUPPORT_SET_POSITION,
["opened", "closed", "opening", "closing", "position"],
),
(
True,
0,
SUPPORT_OPEN | SUPPORT_SET_TILT_POSITION,
["opened", "closed", "opening", "closing", "tilt_position"],
),
],
)
async def test_get_triggers(
hass,
device_reg,
entity_reg,
set_state,
features_reg,
features_state,
expected_trigger_types,
):
"""Test we get the expected triggers 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(
@ -113,106 +100,30 @@ async def test_get_triggers_set_pos(
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,
)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
if set_state:
hass.states.async_set(
f"{DOMAIN}.test_5678",
"attributes",
{"supported_features": features_state},
)
expected_triggers = [
{
"platform": "device",
"domain": DOMAIN,
"type": "opened",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"platform": "device",
"domain": DOMAIN,
"type": "closed",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"platform": "device",
"domain": DOMAIN,
"type": "opening",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"platform": "device",
"domain": DOMAIN,
"type": "closing",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"platform": "device",
"domain": DOMAIN,
"type": "position",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
]
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
assert_lists_same(triggers, expected_triggers)
expected_triggers = []
async def test_get_triggers_set_tilt_pos(
hass, device_reg, entity_reg, enable_custom_integrations
):
"""Test we get the expected triggers 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"}})
expected_triggers = [
expected_triggers += [
{
"platform": "device",
"domain": DOMAIN,
"type": "opened",
"type": trigger,
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"platform": "device",
"domain": DOMAIN,
"type": "closed",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"platform": "device",
"domain": DOMAIN,
"type": "opening",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"platform": "device",
"domain": DOMAIN,
"type": "closing",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"platform": "device",
"domain": DOMAIN,
"type": "tilt_position",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
"entity_id": f"{DOMAIN}.test_5678",
}
for trigger in expected_trigger_types
]
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
assert_lists_same(triggers, expected_triggers)