mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Improve editing of device conditions referencing non-added cover (#51833)
This commit is contained in:
parent
af05543744
commit
59ef55c34f
@ -7,7 +7,6 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
ATTR_SUPPORTED_FEATURES,
|
|
||||||
CONF_ABOVE,
|
CONF_ABOVE,
|
||||||
CONF_BELOW,
|
CONF_BELOW,
|
||||||
CONF_CONDITION,
|
CONF_CONDITION,
|
||||||
@ -28,6 +27,7 @@ from homeassistant.helpers import (
|
|||||||
template,
|
template,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.config_validation import DEVICE_CONDITION_BASE_SCHEMA
|
from homeassistant.helpers.config_validation import DEVICE_CONDITION_BASE_SCHEMA
|
||||||
|
from homeassistant.helpers.entity import get_supported_features
|
||||||
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
|
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
@ -77,11 +77,7 @@ async def async_get_conditions(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 conditions for each entity that belongs to this integration
|
# Add conditions for each entity that belongs to this integration
|
||||||
|
@ -2,7 +2,13 @@
|
|||||||
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_OPEN,
|
||||||
|
SUPPORT_SET_POSITION,
|
||||||
|
SUPPORT_SET_TILT_POSITION,
|
||||||
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_PLATFORM,
|
CONF_PLATFORM,
|
||||||
STATE_CLOSED,
|
STATE_CLOSED,
|
||||||
@ -43,65 +49,31 @@ def calls(hass):
|
|||||||
return async_mock_service(hass, "test", "automation")
|
return async_mock_service(hass, "test", "automation")
|
||||||
|
|
||||||
|
|
||||||
async def test_get_conditions(hass, device_reg, entity_reg, enable_custom_integrations):
|
@pytest.mark.parametrize(
|
||||||
"""Test we get the expected conditions from a cover."""
|
"set_state,features_reg,features_state,expected_condition_types",
|
||||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
[
|
||||||
platform.init()
|
(False, 0, 0, []),
|
||||||
ent = platform.ENTITIES[0]
|
(False, SUPPORT_CLOSE, 0, ["is_open", "is_closed", "is_opening", "is_closing"]),
|
||||||
|
(False, SUPPORT_OPEN, 0, ["is_open", "is_closed", "is_opening", "is_closing"]),
|
||||||
config_entry = MockConfigEntry(domain="test", data={})
|
(False, SUPPORT_SET_POSITION, 0, ["is_position"]),
|
||||||
config_entry.add_to_hass(hass)
|
(False, SUPPORT_SET_TILT_POSITION, 0, ["is_tilt_position"]),
|
||||||
device_entry = device_reg.async_get_or_create(
|
(True, 0, 0, []),
|
||||||
config_entry_id=config_entry.entry_id,
|
(True, 0, SUPPORT_CLOSE, ["is_open", "is_closed", "is_opening", "is_closing"]),
|
||||||
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
(True, 0, SUPPORT_OPEN, ["is_open", "is_closed", "is_opening", "is_closing"]),
|
||||||
)
|
(True, 0, SUPPORT_SET_POSITION, ["is_position"]),
|
||||||
entity_reg.async_get_or_create(
|
(True, 0, SUPPORT_SET_TILT_POSITION, ["is_tilt_position"]),
|
||||||
DOMAIN, "test", ent.unique_id, device_id=device_entry.id
|
],
|
||||||
)
|
)
|
||||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
async def test_get_conditions(
|
||||||
|
hass,
|
||||||
expected_conditions = [
|
device_reg,
|
||||||
{
|
entity_reg,
|
||||||
"condition": "device",
|
set_state,
|
||||||
"domain": DOMAIN,
|
features_reg,
|
||||||
"type": "is_open",
|
features_state,
|
||||||
"device_id": device_entry.id,
|
expected_condition_types,
|
||||||
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_closed",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_opening",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_closing",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
|
|
||||||
assert_lists_same(conditions, expected_conditions)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_get_conditions_set_pos(
|
|
||||||
hass, device_reg, entity_reg, enable_custom_integrations
|
|
||||||
):
|
):
|
||||||
"""Test we get the expected conditions from a cover."""
|
"""Test we get the expected conditions 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(
|
||||||
@ -109,106 +81,28 @@ async def test_get_conditions_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}
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
expected_conditions = [
|
expected_conditions = []
|
||||||
|
expected_conditions += [
|
||||||
{
|
{
|
||||||
"condition": "device",
|
"condition": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": "is_open",
|
"type": condition,
|
||||||
"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 condition in expected_condition_types
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_closed",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_opening",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_closing",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_position",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
|
|
||||||
assert_lists_same(conditions, expected_conditions)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_get_conditions_set_tilt_pos(
|
|
||||||
hass, device_reg, entity_reg, enable_custom_integrations
|
|
||||||
):
|
|
||||||
"""Test we get the expected conditions 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_conditions = [
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_open",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_closed",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_opening",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_closing",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_tilt_position",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
|
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
|
||||||
assert_lists_same(conditions, expected_conditions)
|
assert_lists_same(conditions, expected_conditions)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user