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

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

View File

@ -4,7 +4,12 @@ from datetime import timedelta
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_OPEN,
SUPPORT_SET_POSITION,
SUPPORT_SET_TILT_POSITION,
)
from homeassistant.const import ( from homeassistant.const import (
CONF_PLATFORM, CONF_PLATFORM,
STATE_CLOSED, STATE_CLOSED,
@ -47,65 +52,47 @@ def calls(hass):
return async_mock_service(hass, "test", "automation") return async_mock_service(hass, "test", "automation")
async def test_get_triggers(hass, device_reg, entity_reg, enable_custom_integrations): @pytest.mark.parametrize(
"""Test we get the expected triggers from a cover.""" "set_state,features_reg,features_state,expected_trigger_types",
platform = getattr(hass.components, f"test.{DOMAIN}") [
platform.init() (False, SUPPORT_OPEN, 0, ["opened", "closed", "opening", "closing"]),
ent = platform.ENTITIES[0] (
False,
config_entry = MockConfigEntry(domain="test", data={}) SUPPORT_OPEN | SUPPORT_SET_POSITION,
config_entry.add_to_hass(hass) 0,
device_entry = device_reg.async_get_or_create( ["opened", "closed", "opening", "closing", "position"],
config_entry_id=config_entry.entry_id, ),
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, (
) False,
entity_reg.async_get_or_create( SUPPORT_OPEN | SUPPORT_SET_TILT_POSITION,
DOMAIN, "test", ent.unique_id, device_id=device_entry.id 0,
) ["opened", "closed", "opening", "closing", "tilt_position"],
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) ),
(True, 0, SUPPORT_OPEN, ["opened", "closed", "opening", "closing"]),
expected_triggers = [ (
{ True,
"platform": "device", 0,
"domain": DOMAIN, SUPPORT_OPEN | SUPPORT_SET_POSITION,
"type": "opened", ["opened", "closed", "opening", "closing", "position"],
"device_id": device_entry.id, ),
"entity_id": f"{DOMAIN}.test_{ent.unique_id}", (
}, True,
{ 0,
"platform": "device", SUPPORT_OPEN | SUPPORT_SET_TILT_POSITION,
"domain": DOMAIN, ["opened", "closed", "opening", "closing", "tilt_position"],
"type": "closed", ),
"device_id": device_entry.id, ],
"entity_id": f"{DOMAIN}.test_{ent.unique_id}", )
}, async def test_get_triggers(
{ hass,
"platform": "device", device_reg,
"domain": DOMAIN, entity_reg,
"type": "opening", set_state,
"device_id": device_entry.id, features_reg,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}", features_state,
}, expected_trigger_types,
{
"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
): ):
"""Test we get the expected triggers from a cover.""" """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 = 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(
@ -113,106 +100,30 @@ async def test_get_triggers_set_pos(
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,
) )
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 = [ 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 = [
{ {
"platform": "device", "platform": "device",
"domain": DOMAIN, "domain": DOMAIN,
"type": "opened", "type": trigger,
"device_id": device_entry.id, "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
"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}",
},
] ]
triggers = await async_get_device_automations(hass, "trigger", device_entry.id) triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
assert_lists_same(triggers, expected_triggers) assert_lists_same(triggers, expected_triggers)